Completed
Push — master ( f33d54...c2112e )
by Tobias
01:32
created

SerializerRouter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Happyr\MessageSerializer;
6
7
use Happyr\MessageSerializer\Transformer\Exception\TransformerNotFoundException;
8
use Symfony\Component\Messenger\Envelope;
9
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
10
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
11
12
/**
13
 * @author Radoje Albijanic <[email protected]>
14
 */
15
final class SerializerRouter implements SerializerInterface
16
{
17
    private $happyrSerializer;
18
    private $symfonySerializer;
19
20 8
    public function __construct(SerializerInterface $happyrSerializer, SerializerInterface $symfonySerializer)
21
    {
22 8
        $this->happyrSerializer = $happyrSerializer;
23 8
        $this->symfonySerializer = $symfonySerializer;
24 8
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 8
    public function decode(array $encodedEnvelope): Envelope
30
    {
31 8
        if (empty($encodedEnvelope['body'])) {
32 1
            throw new MessageDecodingFailedException('Encoded envelope should have at least a "body".');
33
        }
34
35
        try {
36 7
            $envelopeBodyArray = \json_decode($encodedEnvelope['body'], true, 512, JSON_THROW_ON_ERROR);
37 1
        } catch (\JsonException $exception) {
0 ignored issues
show
Bug introduced by
The class JsonException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
38 1
            return $this->symfonySerializer->decode($encodedEnvelope);
39
        }
40
41
        if (
42 6
            array_key_exists('version', $envelopeBodyArray)
43 6
            && array_key_exists('identifier', $envelopeBodyArray)
44 6
            && array_key_exists('timestamp', $envelopeBodyArray)
45 6
            && array_key_exists('payload', $envelopeBodyArray)
46
        ) {
47 1
            return $this->happyrSerializer->decode($encodedEnvelope);
48
        }
49
50 5
        return $this->symfonySerializer->decode($encodedEnvelope);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function encode(Envelope $envelope): array
57
    {
58
        try {
59
            return $this->happyrSerializer->encode($envelope);
60
        } catch (TransformerNotFoundException $e) {
61
            return $this->symfonySerializer->encode($envelope);
62
        }
63
    }
64
}
65