|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Queue\Message; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use JsonException; |
|
9
|
|
|
|
|
10
|
|
|
final class JsonMessageSerializer implements MessageSerializerInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @throws JsonException |
|
14
|
|
|
*/ |
|
15
|
4 |
|
public function serialize(MessageInterface $message): string |
|
16
|
|
|
{ |
|
17
|
4 |
|
$payload = [ |
|
18
|
4 |
|
'name' => $message->getHandlerName(), |
|
19
|
4 |
|
'data' => $message->getData(), |
|
20
|
4 |
|
'meta' => $message->getMetadata(), |
|
21
|
4 |
|
]; |
|
22
|
4 |
|
if (!isset($payload['meta']['message-class'])) { |
|
23
|
4 |
|
$payload['meta']['message-class'] = $message instanceof EnvelopeInterface |
|
24
|
2 |
|
? $message->getMessage()::class |
|
25
|
2 |
|
: $message::class; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
4 |
|
return json_encode($payload, JSON_THROW_ON_ERROR); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @throws JsonException |
|
33
|
|
|
* @throws InvalidArgumentException |
|
34
|
|
|
*/ |
|
35
|
13 |
|
public function unserialize(string $value): MessageInterface |
|
36
|
|
|
{ |
|
37
|
13 |
|
$payload = json_decode($value, true, 512, JSON_THROW_ON_ERROR); |
|
38
|
13 |
|
if (!is_array($payload)) { |
|
39
|
4 |
|
throw new InvalidArgumentException('Payload must be array. Got ' . get_debug_type($payload) . '.'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
9 |
|
$name = $payload['name'] ?? null; |
|
43
|
9 |
|
if (!isset($name) || !is_string($name)) { |
|
44
|
|
|
throw new InvalidArgumentException('Handler name must be a string. Got ' . get_debug_type($name) . '.'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
9 |
|
$meta = $payload['meta'] ?? []; |
|
48
|
9 |
|
if (!is_array($meta)) { |
|
49
|
3 |
|
throw new InvalidArgumentException('Metadata must be an array. Got ' . get_debug_type($meta) . '.'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
6 |
|
$envelopes = []; |
|
53
|
6 |
|
if (isset($meta[EnvelopeInterface::ENVELOPE_STACK_KEY]) && is_array($meta[EnvelopeInterface::ENVELOPE_STACK_KEY])) { |
|
54
|
3 |
|
$envelopes = $meta[EnvelopeInterface::ENVELOPE_STACK_KEY]; |
|
55
|
|
|
} |
|
56
|
6 |
|
$meta[EnvelopeInterface::ENVELOPE_STACK_KEY] = []; |
|
57
|
|
|
|
|
58
|
6 |
|
$class = $payload['meta']['message-class'] ?? Message::class; |
|
59
|
6 |
|
if (!is_subclass_of($class, MessageInterface::class)) { |
|
60
|
|
|
$class = Message::class; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var class-string<MessageInterface> $class |
|
65
|
|
|
*/ |
|
66
|
6 |
|
$message = $class::fromData($name, $payload['data'] ?? null, $meta); |
|
67
|
|
|
|
|
68
|
6 |
|
foreach ($envelopes as $envelope) { |
|
69
|
3 |
|
if (is_string($envelope) && class_exists($envelope) && is_subclass_of($envelope, EnvelopeInterface::class)) { |
|
70
|
3 |
|
$message = $envelope::fromMessage($message); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
6 |
|
return $message; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|