Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
30.00% |
3 / 10 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| AddTextEncrypterContextBuilder | |
30.00% |
3 / 10 |
|
50.00% |
1 / 2 |
13.57 | |
0.00% |
0 / 1 |
| __construct | |
12.50% |
1 / 8 |
|
0.00% |
0 / 1 |
14.72 | |||
| process | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Common\ContextBuilders; |
| 3 | |
| 4 | use Apie\Common\Wrappers\TextEncrypter; |
| 5 | use Apie\Core\Context\ApieContext; |
| 6 | use Apie\Core\ContextBuilders\ContextBuilderInterface; |
| 7 | use Apie\Core\Identifiers\UuidV4; |
| 8 | use Psr\Cache\CacheItemPoolInterface; |
| 9 | use Psr\Log\LoggerInterface; |
| 10 | use SensitiveParameter; |
| 11 | |
| 12 | class AddTextEncrypterContextBuilder implements ContextBuilderInterface |
| 13 | { |
| 14 | public function __construct( |
| 15 | private CacheItemPoolInterface $cache, |
| 16 | private LoggerInterface $logger, |
| 17 | #[SensitiveParameter] private ?string $encryptionKey = null |
| 18 | ) { |
| 19 | if ($encryptionKey === null) { |
| 20 | $this->encryptionKey = $this->cache->getItem('apie.encryption_key')->get(); |
| 21 | if (!$this->encryptionKey) { |
| 22 | $this->encryptionKey = UuidV4::createRandom()->toNative(); |
| 23 | if (!$this->cache->save( |
| 24 | $this->cache->getItem('apie.encryption_key')->set($this->encryptionKey) |
| 25 | )) { |
| 26 | // no exception because of some bootstrap shenanigans in laravel |
| 27 | $this->logger->warning('Could not store encryption key in cache, the key will be regenerated on each request'); |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | public function process(ApieContext $context): ApieContext |
| 34 | { |
| 35 | return $context |
| 36 | ->withContext(TextEncrypter::class, new TextEncrypter($this->encryptionKey)); |
| 37 | } |
| 38 | } |