Completed
Push — master ( 7775e8...f33d54 )
by Tobias
03:10
created

Serializer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 78.79%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 5
dl 0
loc 76
ccs 26
cts 33
cp 0.7879
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A decode() 0 24 5
A encode() 0 12 1
A getMetaFromEnvelope() 0 11 2
A addMetaToEnvelope() 0 8 2
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\Stamp\RedeliveryStamp;
14
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
15
16
final class Serializer implements SerializerInterface
17
{
18
    private $transformer;
19
    private $hydrator;
20
21 2
    public function __construct(MessageToArrayInterface $transformer, ArrayToMessageInterface $hydrator)
22
    {
23 2
        $this->transformer = $transformer;
24 2
        $this->hydrator = $hydrator;
25 2
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 1
    public function decode(array $encodedEnvelope): Envelope
31
    {
32 1
        if (empty($encodedEnvelope['body'])) {
33
            throw new MessageDecodingFailedException('Encoded envelope should have at least a "body".');
34
        }
35
36
        try {
37 1
            $array = \json_decode($encodedEnvelope['body'], true, 512, \JSON_THROW_ON_ERROR);
38
        } 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...
39
            throw new MessageDecodingFailedException(\sprintf('Error when trying to json_decode message: "%s"', $encodedEnvelope['body']), 0, $e);
40
        }
41
42 1
        $meta = $array['_meta'] ?? [];
43 1
        unset($array['_meta']);
44
45
        try {
46 1
            $message = $this->hydrator->toMessage($array);
47 1
            $envelope = $message instanceof Envelope ? $message : new Envelope($message);
48
        } catch (HydratorException $e) {
49
            throw new MessageDecodingFailedException('Failed to decode message', 0, $e);
50
        }
51
52 1
        return $this->addMetaToEnvelope($meta, $envelope);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 1
    public function encode(Envelope $envelope): array
59
    {
60 1
        $envelope = $envelope->withoutStampsOfType(NonSendableStampInterface::class);
61
62 1
        $message = $this->transformer->toArray($envelope);
63 1
        $message['_meta'] = $this->getMetaFromEnvelope($envelope);
64
65
        return [
66 1
            'headers' => ['Content-Type' => 'application/json'],
67 1
            'body' => \json_encode($message),
68
        ];
69
    }
70
71 1
    private function getMetaFromEnvelope(Envelope $envelope): array
72
    {
73 1
        $meta = [];
74
75 1
        $redeliveryStamp = $envelope->last(RedeliveryStamp::class);
76 1
        if ($redeliveryStamp instanceof RedeliveryStamp) {
77
            $meta['retry-count'] = $redeliveryStamp->getRetryCount();
78
        }
79
80 1
        return $meta;
81
    }
82
83 1
    private function addMetaToEnvelope(array $meta, Envelope $envelope): Envelope
84
    {
85 1
        if (isset($meta['retry-count'])) {
86
            $envelope = $envelope->with(new RedeliveryStamp((int) $meta['retry-count']));
87
        }
88
89 1
        return $envelope;
90
    }
91
}
92