Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
33.33% covered (danger)
33.33%
3 / 9
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AddTextEncrypterContextBuilder
33.33% covered (danger)
33.33%
3 / 9
50.00% covered (danger)
50.00%
1 / 2
8.74
0.00% covered (danger)
0.00%
0 / 1
 __construct
14.29% covered (danger)
14.29%
1 / 7
0.00% covered (danger)
0.00%
0 / 1
8.67
 process
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\Common\ContextBuilders;
3
4use Apie\Common\Wrappers\TextEncrypter;
5use Apie\Core\Context\ApieContext;
6use Apie\Core\ContextBuilders\ContextBuilderInterface;
7use Apie\Core\Identifiers\UuidV4;
8use Psr\Cache\CacheItemPoolInterface;
9use SensitiveParameter;
10
11class AddTextEncrypterContextBuilder implements ContextBuilderInterface
12{
13    public function __construct(
14        private CacheItemPoolInterface $cache,
15        #[SensitiveParameter] private ?string $encryptionKey = null
16    ) {
17        if ($encryptionKey === null) {
18            $this->encryptionKey = $this->cache->getItem('apie.encryption_key')->get();
19            if (!$this->encryptionKey) {
20                $this->encryptionKey = UuidV4::createRandom()->toNative();
21                $this->cache->save(
22                    $this->cache->getItem('apie.encryption_key')->set($this->encryptionKey)
23                );
24            }
25        }
26    }
27
28    public function process(ApieContext $context): ApieContext
29    {
30        return $context
31            ->withContext(TextEncrypter::class, new TextEncrypter($this->encryptionKey));
32    }
33}