Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
23.81% |
5 / 21 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
ConsoleLoginContextBuilder | |
23.81% |
5 / 21 |
|
0.00% |
0 / 1 |
44.83 | |
0.00% |
0 / 1 |
process | |
23.81% |
5 / 21 |
|
0.00% |
0 / 1 |
44.83 |
1 | <?php |
2 | namespace Apie\Console\ContextBuilders; |
3 | |
4 | use Apie\Common\Interfaces\CheckLoginStatusInterface; |
5 | use Apie\Common\ValueObjects\DecryptedAuthenticatedUser; |
6 | use Apie\Common\Wrappers\TextEncrypter; |
7 | use Apie\Console\ConsoleCliStorage; |
8 | use Apie\Core\Context\ApieContext; |
9 | use Apie\Core\ContextBuilders\ContextBuilderInterface; |
10 | use Apie\Core\ContextConstants; |
11 | use Apie\Core\Datalayers\ApieDatalayer; |
12 | use Apie\Core\Exceptions\EntityNotFoundException; |
13 | use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException; |
14 | |
15 | class ConsoleLoginContextBuilder implements ContextBuilderInterface |
16 | { |
17 | |
18 | |
19 | public function process(ApieContext $context): ApieContext |
20 | { |
21 | $textEncrypter = $context->getContext(TextEncrypter::class, false); |
22 | $apieDatalayer = $context->getContext(ApieDatalayer::class, false); |
23 | $cliStorage = $context->getContext(ConsoleCliStorage::class, false); |
24 | if (!$cliStorage || !$textEncrypter || !$apieDatalayer) { |
25 | // Storage only available in apie/console |
26 | return $context; |
27 | } |
28 | $authenticated = $cliStorage->restore('_APIE_AUTHENTICATED'); |
29 | if ($authenticated === null) { |
30 | return $context; |
31 | } |
32 | try { |
33 | $decrypted = DecryptedAuthenticatedUser::fromNative($textEncrypter->decrypt($authenticated)); |
34 | if ($decrypted->isExpired()) { |
35 | $cliStorage->remove('_APIE_AUTHENTICATED'); |
36 | return $context; |
37 | } |
38 | $entity = $apieDatalayer->find($decrypted->getId(), $decrypted->getBoundedContextId()); |
39 | if ($entity instanceof CheckLoginStatusInterface && $entity->isDisabled()) { |
40 | $cliStorage->remove('_APIE_AUTHENTICATED'); |
41 | return $context; |
42 | } |
43 | return $context->withContext(ContextConstants::AUTHENTICATED_USER, $entity) |
44 | ->withContext(DecryptedAuthenticatedUser::class, $decrypted); |
45 | } catch (InvalidStringForValueObjectException|EntityNotFoundException) { |
46 | $cliStorage->remove('_APIE_AUTHENTICATED'); |
47 | return $context; |
48 | } |
49 | } |
50 | } |