/
InMemoryTransport.php
156 lines (130 loc) · 3.62 KB
/
InMemoryTransport.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Messenger\Transport;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\LogicException;
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Contracts\Service\ResetInterface;
/**
* Transport that stays in memory. Useful for testing purpose.
*
* @author Gary PEGEOT <garypegeot@gmail.com>
*/
class InMemoryTransport implements TransportInterface, ResetInterface
{
/**
* @var Envelope[]
*/
private array $sent = [];
/**
* @var Envelope[]
*/
private array $acknowledged = [];
/**
* @var Envelope[]
*/
private array $rejected = [];
/**
* @var Envelope[]
*/
private array $queue = [];
private int $nextId = 1;
private ?SerializerInterface $serializer;
public function __construct(SerializerInterface $serializer = null)
{
$this->serializer = $serializer;
}
/**
* {@inheritdoc}
*/
public function get(): iterable
{
return array_values($this->decode($this->queue));
}
/**
* {@inheritdoc}
*/
public function ack(Envelope $envelope): void
{
$this->acknowledged[] = $this->encode($envelope);
if (!$transportMessageIdStamp = $envelope->last(TransportMessageIdStamp::class)) {
throw new LogicException('No TransportMessageIdStamp found on the Envelope.');
}
unset($this->queue[$transportMessageIdStamp->getId()]);
}
/**
* {@inheritdoc}
*/
public function reject(Envelope $envelope): void
{
$this->rejected[] = $this->encode($envelope);
if (!$transportMessageIdStamp = $envelope->last(TransportMessageIdStamp::class)) {
throw new LogicException('No TransportMessageIdStamp found on the Envelope.');
}
unset($this->queue[$transportMessageIdStamp->getId()]);
}
/**
* {@inheritdoc}
*/
public function send(Envelope $envelope): Envelope
{
$id = $this->nextId++;
$envelope = $envelope->with(new TransportMessageIdStamp($id));
$encodedEnvelope = $this->encode($envelope);
$this->sent[] = $encodedEnvelope;
$this->queue[$id] = $encodedEnvelope;
return $envelope;
}
public function reset()
{
$this->sent = $this->queue = $this->rejected = $this->acknowledged = [];
}
/**
* @return Envelope[]
*/
public function getAcknowledged(): array
{
return $this->decode($this->acknowledged);
}
/**
* @return Envelope[]
*/
public function getRejected(): array
{
return $this->decode($this->rejected);
}
/**
* @return Envelope[]
*/
public function getSent(): array
{
return $this->decode($this->sent);
}
private function encode(Envelope $envelope): Envelope|array
{
if (null === $this->serializer) {
return $envelope;
}
return $this->serializer->encode($envelope);
}
/**
* @param array<mixed> $messagesEncoded
*
* @return Envelope[]
*/
private function decode(array $messagesEncoded): array
{
if (null === $this->serializer) {
return $messagesEncoded;
}
return array_map($this->serializer->decode(...), $messagesEncoded);
}
}