forked from telephantast/bunny-transport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBunnyMessageEncoder.php
98 lines (82 loc) · 2.97 KB
/
BunnyMessageEncoder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
declare(strict_types=1);
namespace Telephantast\BunnyTransport;
use Telephantast\Message\Message;
use Telephantast\MessageBus\Async\Delay;
use Telephantast\MessageBus\Async\ObjectNormalizer;
use Telephantast\MessageBus\CreatedAt\CreatedAt;
use Telephantast\MessageBus\Envelope;
use Telephantast\MessageBus\MessageId\CorrelationId;
use Telephantast\MessageBus\MessageId\MessageId;
use Telephantast\MessageBus\Stamp;
/**
* @internal
* @psalm-internal Telephantast\BunnyTransport
* @psalm-type BunnyHeaders = array{
* type: class-string<Message>,
* content-type?: string,
* content-encoding?: string,
* delivery-mode?: self::DELIVERY_MODE_*,
* priority?: int,
* message-id?: non-empty-string,
* correlation-id?: non-empty-string,
* reply-to?: string,
* user-id?: string,
* expiration?: int,
* timestamp?: \DateTimeInterface,
* x-delay?: numeric-string,
* app-id?: string,
* stamps?: array<class-string<Stamp>, mixed>,
* }
*/
final readonly class BunnyMessageEncoder
{
private const ENCODING = 'UTF-8';
private const CONTENT_TYPE = 'application/json';
private const DELIVERY_MODE_PERSISTENT = 2;
public function __construct(
private ObjectNormalizer $objectNormalizer,
) {}
/**
* @return array{
* body: string,
* headers?: BunnyHeaders,
* mandatory?: bool,
* immediate?: bool,
* }
*/
public function encode(Envelope $envelope): array
{
$headers = [
'type' => $envelope->message::class,
'content-type' => self::CONTENT_TYPE,
'content-encoding' => self::ENCODING,
'delivery-mode' => self::DELIVERY_MODE_PERSISTENT,
];
$messageIdStamp = $envelope->getStamp(MessageId::class);
if ($messageIdStamp !== null) {
$headers['message-id'] = $messageIdStamp->messageId;
$envelope = $envelope->withoutStamp(MessageId::class);
}
$correlationIdStamp = $envelope->getStamp(CorrelationId::class);
if ($correlationIdStamp !== null) {
$headers['correlation-id'] = $correlationIdStamp->correlationId;
$envelope = $envelope->withoutStamp(CorrelationId::class);
}
$createdAt = $envelope->getStamp(CreatedAt::class);
if ($createdAt !== null) {
$headers['timestamp'] = $createdAt->time;
$envelope = $envelope->withoutStamp(CreatedAt::class);
}
$delay = $envelope->getStamp(Delay::class)?->milliseconds ?? 0;
if ($delay > 0) {
$headers['x-delay'] = (string) $delay;
$envelope = $envelope->withoutStamp(Delay::class);
}
$headers['stamps'] = array_map($this->objectNormalizer->normalize(...), $envelope->stamps);
return [
'body' => json_encode($this->objectNormalizer->normalize($envelope->message), JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR),
'headers' => $headers,
];
}
}