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