Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
75.00% |
18 / 24 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Apie | |
75.00% |
18 / 24 |
|
50.00% |
2 / 4 |
10.27 | |
0.00% |
0 / 1 |
| getFacadeAccessor | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| loginAs | |
77.78% |
14 / 18 |
|
0.00% |
0 / 1 |
6.40 | |||
| logout | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getCurrentUser | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Apie\LaravelApie; |
| 4 | |
| 5 | use Apie\Common\ValueObjects\DecryptedAuthenticatedUser; |
| 6 | use Apie\Core\Actions\ActionInterface; |
| 7 | use Apie\Core\Actions\ApieFacadeInterface; |
| 8 | use Apie\Core\BoundedContext\BoundedContext; |
| 9 | use Apie\Core\BoundedContext\BoundedContextId; |
| 10 | use Apie\Core\Context\ApieContext; |
| 11 | use Apie\Core\Datalayers\Lists\EntityListInterface; |
| 12 | use Apie\Core\Entities\EntityInterface; |
| 13 | use Apie\Core\Identifiers\IdentifierInterface; |
| 14 | use Apie\Core\Lists\ItemHashmap; |
| 15 | use Apie\Core\Lists\ItemList; |
| 16 | use Apie\LaravelApie\ContextBuilders\ApieCurrentUserContextBuilder; |
| 17 | use Apie\LaravelApie\Wrappers\Core\BoundedContextSelected; |
| 18 | use Apie\LaravelApie\Wrappers\Security\ApieUserDecorator; |
| 19 | use Illuminate\Support\Facades\Auth; |
| 20 | use Illuminate\Support\Facades\Facade; |
| 21 | use ReflectionClass; |
| 22 | use ReflectionMethod; |
| 23 | |
| 24 | /** |
| 25 | * @method static EntityListInterface<EntityInterface> all(string|ReflectionClass<EntityInterface> $class, BoundedContext|BoundedContextId $boundedContext) |
| 26 | * @method static EntityInterface find(IdentifierInterface<EntityInterface> $identifier, BoundedContext|BoundedContextId $boundedContext) |
| 27 | * @method static EntityInterface persistNew(EntityInterface $entity, BoundedContext|BoundedContextId $boundedContext) |
| 28 | * @method static EntityInterface persistExisting(EntityInterface $entity, BoundedContext|BoundedContextId $boundedContext) |
| 29 | * @method static EntityInterface upsert(EntityInterface $entity, BoundedContext|BoundedContextId $boundedContext) |
| 30 | * @method static void removeExisting(EntityInterface $entity, BoundedContext|BoundedContextId $boundedContext) |
| 31 | * @method static string|int|float|bool|ItemList|ItemHashmap|null normalize(mixed $object, ApieContext $apieContext) |
| 32 | * @method static mixed denormalizeNewObject(string|int|float|bool|ItemList|ItemHashmap|array<mixed>|null $object, string $desiredType, ApieContext $apieContext) |
| 33 | * @method static mixed denormalizeOnExistingObject(ItemHashmap $object, object $existingObject, ApieContext $apieContext) |
| 34 | * @method static mixed denormalizeOnMethodCall(string|int|float|bool|ItemList|ItemHashmap|array<mixed>|null $input, ?object $object, ReflectionMethod $method, ApieContext $apieContext) |
| 35 | * @method static ActionInterface createAction(ApieContext $apieContext) |
| 36 | * |
| 37 | * @see ApieFacadeInterface |
| 38 | */ |
| 39 | class Apie extends Facade |
| 40 | { |
| 41 | /** @var DecryptedAuthenticatedUser<EntityInterface>|null */ |
| 42 | private static ?DecryptedAuthenticatedUser $currentUser = null; |
| 43 | protected static function getFacadeAccessor(): string |
| 44 | { |
| 45 | return 'apie'; |
| 46 | } |
| 47 | |
| 48 | public static function loginAs(EntityInterface $entity, BoundedContext|BoundedContextId|string|null $boundedContext): void |
| 49 | { |
| 50 | if (is_string($boundedContext)) { |
| 51 | $boundedContext = new BoundedContextId($boundedContext); |
| 52 | } |
| 53 | $boundedContextId = $boundedContext instanceof BoundedContext ? $boundedContext->getId() : $boundedContext; |
| 54 | if ($boundedContextId === null) { |
| 55 | $boundedContextId = resolve(BoundedContextSelected::class)->getBoundedContextFromRequest()?->getId(); |
| 56 | } |
| 57 | if ($boundedContextId === null) { |
| 58 | $className = $entity->getId()->getReferenceFor()->name; |
| 59 | $boundedContextId = resolve(BoundedContextSelected::class)->getBoundedContextFromClassName($className)?->getId(); |
| 60 | } |
| 61 | if ($boundedContextId === null) { |
| 62 | throw new \RuntimeException('Could not determine bounded context for the given entity or request.'); |
| 63 | } |
| 64 | |
| 65 | // Auto-login the new user |
| 66 | $decryptedUserId = DecryptedAuthenticatedUser::createFromEntity( |
| 67 | $entity, |
| 68 | $boundedContextId, |
| 69 | time() + 3600 |
| 70 | ); |
| 71 | $decoratedUser = new ApieUserDecorator($decryptedUserId, $entity); |
| 72 | Auth::login($decoratedUser); |
| 73 | self::$currentUser = $decryptedUserId; |
| 74 | } |
| 75 | |
| 76 | public static function logout(): void |
| 77 | { |
| 78 | Auth::logout(); |
| 79 | self::$currentUser = null; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @internal |
| 84 | * @see ApieCurrentUserContextBuilder |
| 85 | * @return DecryptedAuthenticatedUser<EntityInterface>|null |
| 86 | */ |
| 87 | public static function getCurrentUser(): ?DecryptedAuthenticatedUser |
| 88 | { |
| 89 | return tap(self::$currentUser, function () { |
| 90 | self::$currentUser = null; |
| 91 | }); |
| 92 | } |
| 93 | } |