InternalCryptoSerializer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 4
dl 0
loc 8
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\Cryptography\Integration\Symfony\Messenger\Serializer;
6
7
use FRZB\Component\Cryptography\Service\CryptographyInterface as CryptographyService;
8
use Symfony\Component\Messenger\Envelope;
9
use Symfony\Component\Messenger\Transport\Serialization\Serializer as BaseSerializer;
10
use Symfony\Component\Serializer\SerializerInterface as Serializer;
11
12
class InternalCryptoSerializer extends BaseSerializer
13
{
14
    private CryptographyService $crypto;
15
16 3
    public function __construct(
17
        CryptographyService $crypto,
18
        ?Serializer $serializer = null,
19
        string $format = 'json',
20
        array $context = []
21
    ) {
22 3
        parent::__construct($serializer, $format, $context);
23 3
        $this->crypto = $crypto;
24
    }
25
26 2
    public function decode(array $encodedEnvelope): Envelope
27
    {
28 2
        return parent::decode([
29 2
            'body' => $this->crypto->decrypt(self::getBody($encodedEnvelope)),
30 2
            'headers' => self::getHeaders($encodedEnvelope),
31 2
        ]);
32
    }
33
34 2
    public function encode(Envelope $envelope): array
35
    {
36 2
        $encodedEnvelope = parent::encode($envelope);
37
38 2
        return [
39 2
            'body' => $this->crypto->encrypt(self::getBody($encodedEnvelope)),
40 2
            'headers' => self::getHeaders($encodedEnvelope),
41 2
        ];
42
    }
43
44 3
    private static function getBody(array $decodedEnvelope): ?string
45
    {
46 3
        return $decodedEnvelope['body'] ?? '{}';
47
    }
48
49 3
    protected static function getHeaders(array $decodedEnvelope): array
50
    {
51 3
        return $decodedEnvelope['headers'] ?? [];
52
    }
53
}
54