Completed
Push — master ( 66b56c...e32c2e )
by Tobias
02:10
created

Serializer::encode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Happyr\MessageSerializer;
6
7
use Happyr\MessageSerializer\Hydrator\ArrayToMessageInterface;
8
use Happyr\MessageSerializer\Hydrator\Exception\HydratorException;
9
use Happyr\MessageSerializer\Transformer\MessageToArrayInterface;
10
use Symfony\Component\Messenger\Envelope;
11
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
12
use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
13
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
14
15
final class Serializer implements SerializerInterface
16
{
17
    private $transformer;
18
    private $hydrator;
19
20 2
    public function __construct(MessageToArrayInterface $transformer, ArrayToMessageInterface $hydrator)
21
    {
22 2
        $this->transformer = $transformer;
23 2
        $this->hydrator = $hydrator;
24 2
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 1
    public function decode(array $encodedEnvelope): Envelope
30
    {
31 1
        if (empty($encodedEnvelope['body'])) {
32
            throw new MessageDecodingFailedException('Encoded envelope should have at least a "body".');
33
        }
34
35
        try {
36 1
            $array = json_decode($encodedEnvelope['body'], true, 512, JSON_THROW_ON_ERROR);
37
        } catch (\JsonException $e) {
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
            throw new MessageDecodingFailedException(sprintf('Error when trying to json_decode message: "%s"', $encodedEnvelope['body']), 0, $e);
39
        }
40
41
        try {
42 1
            $object = $this->hydrator->toMessage($array);
43
44 1
            return $object instanceof Envelope ? $object : new Envelope($object);
45
        } catch (HydratorException $e) {
46
            throw new MessageDecodingFailedException('Failed to decode message', 0, $e);
47
        }
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 1
    public function encode(Envelope $envelope): array
54
    {
55 1
        $envelope = $envelope->withoutStampsOfType(NonSendableStampInterface::class);
56
57
        return [
58 1
            'headers' => ['Content-Type' => 'application/json'],
59 1
            'body' => json_encode($this->transformer->toArray($envelope)),
60
        ];
61
    }
62
}
63