1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\AMQP\Transport\Protocol\v091; |
5
|
|
|
|
6
|
|
|
use Innmind\AMQP\{ |
7
|
|
|
Model\Basic\Ack, |
8
|
|
|
Model\Basic\Cancel, |
9
|
|
|
Model\Basic\Consume, |
10
|
|
|
Model\Basic\Get, |
11
|
|
|
Model\Basic\Publish, |
12
|
|
|
Model\Basic\Qos, |
13
|
|
|
Model\Basic\Recover, |
14
|
|
|
Model\Basic\Reject, |
15
|
|
|
Model\Basic\Message, |
16
|
|
|
Model\Connection\MaxFrameSize, |
17
|
|
|
Transport\Frame, |
18
|
|
|
Transport\Frame\Type, |
19
|
|
|
Transport\Frame\Channel as FrameChannel, |
20
|
|
|
Transport\Frame\Method, |
21
|
|
|
Transport\Frame\Value, |
22
|
|
|
Transport\Frame\Value\UnsignedLongLongInteger, |
23
|
|
|
Transport\Frame\Value\UnsignedLongInteger, |
24
|
|
|
Transport\Frame\Value\Bits, |
25
|
|
|
Transport\Frame\Value\ShortString, |
26
|
|
|
Transport\Frame\Value\UnsignedShortInteger, |
27
|
|
|
Transport\Frame\Value\Table, |
28
|
|
|
Transport\Frame\Value\UnsignedOctet, |
29
|
|
|
Transport\Frame\Value\Timestamp, |
30
|
|
|
Transport\Protocol\Basic as BasicInterface, |
31
|
|
|
Transport\Protocol\ArgumentTranslator |
32
|
|
|
}; |
33
|
|
|
use Innmind\Math\Algebra\Integer; |
34
|
|
|
use Innmind\Immutable\{ |
35
|
|
|
Str, |
36
|
|
|
Map, |
37
|
|
|
MapInterface, |
38
|
|
|
StreamInterface, |
39
|
|
|
Stream |
40
|
|
|
}; |
41
|
|
|
|
42
|
|
|
final class Basic implements BasicInterface |
43
|
|
|
{ |
44
|
|
|
private $translate; |
45
|
|
|
|
46
|
94 |
|
public function __construct(ArgumentTranslator $translator) |
47
|
|
|
{ |
48
|
94 |
|
$this->translate = $translator; |
49
|
94 |
|
} |
50
|
|
|
|
51
|
1 |
View Code Duplication |
public function ack(FrameChannel $channel, Ack $command): Frame |
|
|
|
|
52
|
|
|
{ |
53
|
1 |
|
return Frame::command( |
54
|
1 |
|
$channel, |
55
|
1 |
|
Methods::get('basic.ack'), |
56
|
1 |
|
new UnsignedLongLongInteger(new Integer($command->deliveryTag())), |
57
|
1 |
|
new Bits($command->isMultiple()) |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
public function cancel(FrameChannel $channel, Cancel $command): Frame |
62
|
|
|
{ |
63
|
1 |
|
return Frame::command( |
64
|
1 |
|
$channel, |
65
|
1 |
|
Methods::get('basic.cancel'), |
66
|
1 |
|
new ShortString(new Str($command->consumerTag())), |
67
|
1 |
|
new Bits(!$command->shouldWait()) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
View Code Duplication |
public function consume(FrameChannel $channel, Consume $command): Frame |
|
|
|
|
72
|
|
|
{ |
73
|
1 |
|
$consumerTag = ''; |
74
|
|
|
|
75
|
1 |
|
if (!$command->shouldAutoGenerateConsumerTag()) { |
76
|
1 |
|
$consumerTag = $command->consumerTag(); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
return Frame::command( |
80
|
1 |
|
$channel, |
81
|
1 |
|
Methods::get('basic.consume'), |
82
|
1 |
|
new UnsignedShortInteger(new Integer(0)), //ticket (reserved) |
83
|
1 |
|
new ShortString(new Str($command->queue())), |
84
|
1 |
|
new ShortString(new Str($consumerTag)), |
85
|
1 |
|
new Bits( |
86
|
1 |
|
!$command->isLocal(), |
87
|
1 |
|
$command->shouldAutoAcknowledge(), |
88
|
1 |
|
$command->isExclusive(), |
89
|
1 |
|
!$command->shouldWait() |
90
|
|
|
), |
91
|
1 |
|
$this->arguments($command->arguments()) |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
public function get(FrameChannel $channel, Get $command): Frame |
96
|
|
|
{ |
97
|
1 |
|
return Frame::command( |
98
|
1 |
|
$channel, |
99
|
1 |
|
Methods::get('basic.get'), |
100
|
1 |
|
new UnsignedShortInteger(new Integer(0)), //ticket (reserved) |
101
|
1 |
|
new ShortString(new Str($command->queue())), |
102
|
1 |
|
new Bits($command->shouldAutoAcknowledge()) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
7 |
|
public function publish( |
110
|
|
|
FrameChannel $channel, |
111
|
|
|
Publish $command, |
112
|
|
|
MaxFrameSize $maxFrameSize |
113
|
|
|
): StreamInterface { |
114
|
7 |
|
$frames = (new Stream(Frame::class)) |
115
|
7 |
|
->add(Frame::command( |
116
|
7 |
|
$channel, |
117
|
7 |
|
Methods::get('basic.publish'), |
118
|
7 |
|
new UnsignedShortInteger(new Integer(0)), //ticket (reserved) |
119
|
7 |
|
new ShortString(new Str($command->exchange())), |
120
|
7 |
|
new ShortString(new Str($command->routingKey())), |
121
|
7 |
|
new Bits( |
122
|
7 |
|
$command->mandatory(), |
123
|
7 |
|
$command->immediate() |
124
|
|
|
) |
125
|
|
|
)) |
126
|
7 |
|
->add(Frame::header( |
127
|
7 |
|
$channel, |
128
|
7 |
|
Methods::classId('basic'), |
129
|
7 |
|
new UnsignedLongLongInteger(new Integer( |
130
|
7 |
|
$command->message()->body()->length() |
131
|
|
|
)), |
132
|
7 |
|
...$this->serializeProperties($command->message()) |
133
|
|
|
)); |
134
|
|
|
|
135
|
|
|
//the "-8" is due to the content frame extra informations (type, channel and end flag) |
136
|
7 |
|
$chunk = $maxFrameSize->isLimited() ? ($maxFrameSize->toInt() - 8) : $command->message()->body()->length(); |
137
|
|
|
|
138
|
|
|
return $command |
139
|
7 |
|
->message() |
140
|
7 |
|
->body() |
141
|
7 |
|
->chunk($chunk) |
142
|
7 |
|
->reduce( |
143
|
7 |
|
$frames, |
144
|
7 |
|
static function(Stream $frames, Str $chunk) use ($channel): Stream { |
145
|
3 |
|
return $frames->add(Frame::body($channel, $chunk)); |
146
|
7 |
|
} |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
View Code Duplication |
public function qos(FrameChannel $channel, Qos $command): Frame |
|
|
|
|
151
|
|
|
{ |
152
|
1 |
|
return Frame::command( |
153
|
1 |
|
$channel, |
154
|
1 |
|
Methods::get('basic.qos'), |
155
|
1 |
|
new UnsignedLongInteger(new Integer($command->prefetchSize())), |
156
|
1 |
|
new UnsignedShortInteger(new Integer($command->prefetchCount())), |
157
|
1 |
|
new Bits($command->isGlobal()) |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
public function recover(FrameChannel $channel, Recover $command): Frame |
162
|
|
|
{ |
163
|
1 |
|
return Frame::command( |
164
|
1 |
|
$channel, |
165
|
1 |
|
Methods::get('basic.recover'), |
166
|
1 |
|
new Bits($command->shouldRequeue()) |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
|
170
|
1 |
View Code Duplication |
public function reject(FrameChannel $channel, Reject $command): Frame |
|
|
|
|
171
|
|
|
{ |
172
|
1 |
|
return Frame::command( |
173
|
1 |
|
$channel, |
174
|
1 |
|
Methods::get('basic.reject'), |
175
|
1 |
|
new UnsignedLongLongInteger(new Integer($command->deliveryTag())), |
176
|
1 |
|
new Bits($command->shouldRequeue()) |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
2 |
View Code Duplication |
private function arguments(MapInterface $arguments): Table |
|
|
|
|
181
|
|
|
{ |
182
|
2 |
|
return new Table( |
183
|
2 |
|
$arguments->reduce( |
184
|
2 |
|
new Map('string', Value::class), |
185
|
2 |
|
function(Map $carry, string $key, $value): Map { |
186
|
2 |
|
return $carry->put( |
187
|
2 |
|
$key, |
188
|
2 |
|
($this->translate)($value) |
189
|
|
|
); |
190
|
2 |
|
} |
191
|
|
|
) |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
|
195
|
7 |
|
private function serializeProperties(Message $message): array |
196
|
|
|
{ |
197
|
7 |
|
$properties = []; |
198
|
7 |
|
$flagBits = 0; |
199
|
|
|
|
200
|
7 |
|
if ($message->hasContentType()) { |
201
|
1 |
|
$properties[] = new ShortString( |
202
|
1 |
|
new Str((string) $message->contentType()) |
203
|
|
|
); |
204
|
1 |
|
$flagBits |= (1 << 15); |
205
|
|
|
} |
206
|
|
|
|
207
|
7 |
|
if ($message->hasContentEncoding()) { |
208
|
1 |
|
$properties[] = new ShortString( |
209
|
1 |
|
new Str((string) $message->contentEncoding()) |
210
|
|
|
); |
211
|
1 |
|
$flagBits |= (1 << 14); |
212
|
|
|
} |
213
|
|
|
|
214
|
7 |
|
if ($message->hasHeaders()) { |
215
|
1 |
|
$properties[] = $this->arguments($message->headers()); |
216
|
1 |
|
$flagBits |= (1 << 13); |
217
|
|
|
} |
218
|
|
|
|
219
|
7 |
|
if ($message->hasDeliveryMode()) { |
220
|
1 |
|
$properties[] = new UnsignedOctet( |
221
|
1 |
|
new Integer($message->deliveryMode()->toInt()) |
222
|
|
|
); |
223
|
1 |
|
$flagBits |= (1 << 12); |
224
|
|
|
} |
225
|
|
|
|
226
|
7 |
|
if ($message->hasPriority()) { |
227
|
1 |
|
$properties[] = new UnsignedOctet( |
228
|
1 |
|
new Integer($message->priority()->toInt()) |
229
|
|
|
); |
230
|
1 |
|
$flagBits |= (1 << 11); |
231
|
|
|
} |
232
|
|
|
|
233
|
7 |
|
if ($message->hasCorrelationId()) { |
234
|
1 |
|
$properties[] = new ShortString( |
235
|
1 |
|
new Str((string) $message->correlationId()) |
236
|
|
|
); |
237
|
1 |
|
$flagBits |= (1 << 10); |
238
|
|
|
} |
239
|
|
|
|
240
|
7 |
|
if ($message->hasReplyTo()) { |
241
|
1 |
|
$properties[] = new ShortString( |
242
|
1 |
|
new Str((string) $message->replyTo()) |
243
|
|
|
); |
244
|
1 |
|
$flagBits |= (1 << 9); |
245
|
|
|
} |
246
|
|
|
|
247
|
7 |
|
if ($message->hasExpiration()) { |
248
|
1 |
|
$properties[] = new ShortString( |
249
|
1 |
|
new Str((string) $message->expiration()->milliseconds()) |
250
|
|
|
); |
251
|
1 |
|
$flagBits |= (1 << 8); |
252
|
|
|
} |
253
|
|
|
|
254
|
7 |
View Code Duplication |
if ($message->hasId()) { |
|
|
|
|
255
|
1 |
|
$properties[] = new ShortString( |
256
|
1 |
|
new Str((string) $message->id()) |
257
|
|
|
); |
258
|
1 |
|
$flagBits |= (1 << 7); |
259
|
|
|
} |
260
|
|
|
|
261
|
7 |
|
if ($message->hasTimestamp()) { |
262
|
1 |
|
$properties[] = new Timestamp($message->timestamp()); |
263
|
1 |
|
$flagBits |= (1 << 6); |
264
|
|
|
} |
265
|
|
|
|
266
|
7 |
View Code Duplication |
if ($message->hasType()) { |
|
|
|
|
267
|
1 |
|
$properties[] = new ShortString( |
268
|
1 |
|
new Str((string) $message->type()) |
269
|
|
|
); |
270
|
1 |
|
$flagBits |= (1 << 5); |
271
|
|
|
} |
272
|
|
|
|
273
|
7 |
|
if ($message->hasUserId()) { |
274
|
1 |
|
$properties[] = new ShortString( |
275
|
1 |
|
new Str((string) $message->userId()) |
276
|
|
|
); |
277
|
1 |
|
$flagBits |= (1 << 4); |
278
|
|
|
} |
279
|
|
|
|
280
|
7 |
|
if ($message->hasAppId()) { |
281
|
1 |
|
$properties[] = new ShortString( |
282
|
1 |
|
new Str((string) $message->appId()) |
283
|
|
|
); |
284
|
1 |
|
$flagBits |= (1 << 3); |
285
|
|
|
} |
286
|
|
|
|
287
|
7 |
|
array_unshift( |
288
|
7 |
|
$properties, |
289
|
7 |
|
new UnsignedShortInteger(new Integer($flagBits)) |
290
|
|
|
); |
291
|
|
|
|
292
|
7 |
|
return $properties; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.