1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mouf\AmqpClient\Objects; |
4
|
|
|
|
5
|
|
|
use Mouf\AmqpClient\Client; |
6
|
|
|
use Mouf\AmqpClient\RabbitMqObjectInterface; |
7
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
8
|
|
|
use Mouf\AmqpClient\ConsumerInterface; |
9
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Marc |
13
|
|
|
*/ |
14
|
|
|
class Queue implements RabbitMqObjectInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Client |
18
|
|
|
*/ |
19
|
|
|
private $client; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Queue name. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $name; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Passive. |
30
|
|
|
* |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
private $passive = false; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Durable. |
37
|
|
|
* |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
private $durable = false; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Exclusive. |
44
|
|
|
* |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
private $exclusive = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Auto delete. |
51
|
|
|
* |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
private $autoDelete = false; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* No wait. |
58
|
|
|
* |
59
|
|
|
* @var bool |
60
|
|
|
*/ |
61
|
|
|
private $noWait = false; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Ticket. |
65
|
|
|
* |
66
|
|
|
* @var int |
67
|
|
|
*/ |
68
|
|
|
private $ticket = null; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* RabbitMq specific parameter : x-dead-letter-exchange. |
72
|
|
|
* |
73
|
|
|
* @var Exchange |
74
|
|
|
*/ |
75
|
|
|
private $deadLetterExchanger = null; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* RabbitMq specific parameter : confirm. |
79
|
|
|
* |
80
|
|
|
* @var int |
81
|
|
|
*/ |
82
|
|
|
private $confirm = null; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* RabbitMq specific parameter : consumer_cancel_notify. |
86
|
|
|
* |
87
|
|
|
* @var bool |
88
|
|
|
*/ |
89
|
|
|
private $consumerCancelNotify = null; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* RabbitMq specific parameter : alternate-exchange. |
93
|
|
|
* |
94
|
|
|
* @var Queue |
95
|
|
|
*/ |
96
|
|
|
private $alternateExchange = null; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* RabbitMq specific parameter : x-message-ttl. |
100
|
|
|
* |
101
|
|
|
* @var int |
102
|
|
|
*/ |
103
|
|
|
private $ttl = null; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* RabbitMq specific parameter : x-max-length. |
107
|
|
|
* |
108
|
|
|
* @var int |
109
|
|
|
*/ |
110
|
|
|
private $maxLength = null; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* RabbitMq specific parameter : x-max-priority. |
114
|
|
|
* |
115
|
|
|
* @var int |
116
|
|
|
*/ |
117
|
|
|
private $maxPriority = null; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Parameter to initialize object only one time. |
121
|
|
|
* |
122
|
|
|
* @var bool |
123
|
|
|
*/ |
124
|
|
|
private $init = false; |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Consumer list implement ConsumerInterface. |
128
|
|
|
* |
129
|
|
|
* @var array|null |
130
|
|
|
*/ |
131
|
|
|
private $consumers; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set the source (Binding). |
135
|
|
|
* |
136
|
|
|
* @param Binding $source |
|
|
|
|
137
|
|
|
* @param string $name |
138
|
|
|
* @param ConsumerInterface[] $consumers |
139
|
|
|
*/ |
140
|
|
View Code Duplication |
public function __construct(Client $client, $name, array $consumers = []) |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
$this->client = $client; |
143
|
|
|
$this->client->register($this); |
144
|
|
|
$this->name = $name; |
145
|
|
|
$this->consumers = $consumers; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get queue name. |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
public function getName() |
154
|
|
|
{ |
155
|
|
|
return $this->name; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get passive. |
160
|
|
|
* |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
|
|
public function getPassive() |
164
|
|
|
{ |
165
|
|
|
return $this->passive; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param bool $passive |
170
|
|
|
* |
171
|
|
|
* @return Queue |
172
|
|
|
*/ |
173
|
|
|
public function setPassive($passive) |
174
|
|
|
{ |
175
|
|
|
$this->passive = $passive; |
176
|
|
|
|
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get durable. |
182
|
|
|
* |
183
|
|
|
* @return bool |
184
|
|
|
*/ |
185
|
|
|
public function getDurable() |
186
|
|
|
{ |
187
|
|
|
return $this->durable; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Set durable. |
192
|
|
|
* |
193
|
|
|
* @param bool $durable |
194
|
|
|
* |
195
|
|
|
* @return Queue |
196
|
|
|
*/ |
197
|
|
|
public function setDurable($durable) |
198
|
|
|
{ |
199
|
|
|
$this->durable = $durable; |
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get exclusive. |
206
|
|
|
* |
207
|
|
|
* @return bool |
208
|
|
|
*/ |
209
|
|
|
public function getExclusive() |
210
|
|
|
{ |
211
|
|
|
return $this->exclusive; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Set exclusive. |
216
|
|
|
* |
217
|
|
|
* @param bool $exclusive |
218
|
|
|
* |
219
|
|
|
* @return Queue |
220
|
|
|
*/ |
221
|
|
|
public function setExclusive($exclusive) |
222
|
|
|
{ |
223
|
|
|
$this->exclusive = $exclusive; |
224
|
|
|
|
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Get autoDelete. |
230
|
|
|
* |
231
|
|
|
* @return bool |
232
|
|
|
*/ |
233
|
|
|
public function getAutoDelete() |
234
|
|
|
{ |
235
|
|
|
return $this->autoDelete; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Set autoDelete. |
240
|
|
|
* |
241
|
|
|
* @param bool $autoDelete |
242
|
|
|
* |
243
|
|
|
* @return Queue |
244
|
|
|
*/ |
245
|
|
|
public function setAutoDelete($autoDelete) |
246
|
|
|
{ |
247
|
|
|
$this->autoDelete = $autoDelete; |
248
|
|
|
|
249
|
|
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Get noWait. |
254
|
|
|
* |
255
|
|
|
* @return bool |
256
|
|
|
*/ |
257
|
|
|
public function getNoWait() |
258
|
|
|
{ |
259
|
|
|
return $this->noWait; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Set noWait. |
264
|
|
|
* |
265
|
|
|
* @param bool $noWait |
266
|
|
|
* |
267
|
|
|
* @return Queue |
268
|
|
|
*/ |
269
|
|
|
public function setNoWait($noWait) |
270
|
|
|
{ |
271
|
|
|
$this->noWait = $noWait; |
272
|
|
|
|
273
|
|
|
return $this; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Get arguments. |
278
|
|
|
* |
279
|
|
|
* @return array|null |
280
|
|
|
*/ |
281
|
|
|
public function getArguments() |
282
|
|
|
{ |
283
|
|
|
return $this->arguments; |
|
|
|
|
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Set arguments. |
288
|
|
|
* |
289
|
|
|
* @param array $arguments |
290
|
|
|
* |
291
|
|
|
* @return Queue |
292
|
|
|
*/ |
293
|
|
|
public function setArguments($arguments) |
294
|
|
|
{ |
295
|
|
|
$this->arguments = $arguments; |
296
|
|
|
|
297
|
|
|
return $this; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Get ticket. |
302
|
|
|
* |
303
|
|
|
* @return int |
304
|
|
|
*/ |
305
|
|
|
public function getTicket() |
306
|
|
|
{ |
307
|
|
|
return $this->ticket; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Set ticket. |
312
|
|
|
* |
313
|
|
|
* @param int $ticket |
314
|
|
|
* |
315
|
|
|
* @return Queue |
316
|
|
|
*/ |
317
|
|
|
public function setTicket($ticket) |
318
|
|
|
{ |
319
|
|
|
$this->ticket = $ticket; |
320
|
|
|
|
321
|
|
|
return $this; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Get RabbitMq specific parameter : dead letter queue. |
326
|
|
|
* |
327
|
|
|
* @return Queue |
328
|
|
|
*/ |
329
|
|
|
public function getDeadLetterExchanger() |
330
|
|
|
{ |
331
|
|
|
return $this->deadLetterExchanger; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Set RabbitMq specific parameter : dead letter queue. |
336
|
|
|
* |
337
|
|
|
* @param Exchange $exchange |
338
|
|
|
* |
339
|
|
|
* @return Queue |
340
|
|
|
*/ |
341
|
|
|
public function setDeadLetterExchange(Exchange $exchange) |
342
|
|
|
{ |
343
|
|
|
$this->deadLetterExchanger = $exchange; |
344
|
|
|
|
345
|
|
|
return $this; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Get RabbitMq specific parameter : confirm. |
350
|
|
|
* |
351
|
|
|
* @return int |
352
|
|
|
*/ |
353
|
|
|
public function getConfirm() |
354
|
|
|
{ |
355
|
|
|
return $this->confirm; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Set RabbitMq specific parameter : confirm. |
360
|
|
|
* |
361
|
|
|
* @param int $confirm |
362
|
|
|
* |
363
|
|
|
* @return Queue |
364
|
|
|
*/ |
365
|
|
|
public function setConfirm($confirm) |
366
|
|
|
{ |
367
|
|
|
$this->confirm = $confirm; |
368
|
|
|
|
369
|
|
|
return $this; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Get RabbitMq specific parameter : consumer_cancel_notify. |
374
|
|
|
* |
375
|
|
|
* @return bool |
376
|
|
|
*/ |
377
|
|
|
public function getConsumerCancelNotify() |
378
|
|
|
{ |
379
|
|
|
return $this->consumerCancelNotify; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Set RabbitMq specific parameter : consumer_cancel_notify. |
384
|
|
|
* |
385
|
|
|
* @param Queue $consumerCancelNotify |
386
|
|
|
* |
387
|
|
|
* @return Queue |
388
|
|
|
*/ |
389
|
|
|
public function setConsumerCancelNotify(Queue $consumerCancelNotify) |
390
|
|
|
{ |
391
|
|
|
$this->consumerCancelNotify = $consumerCancelNotify; |
|
|
|
|
392
|
|
|
|
393
|
|
|
return $this; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Get RabbitMq specific parameter : alternate_exchange. |
398
|
|
|
* |
399
|
|
|
* @return Queue |
400
|
|
|
*/ |
401
|
|
|
public function getAlternateExchange() |
402
|
|
|
{ |
403
|
|
|
return $this->alternateExchange; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Set RabbitMq specific parameter : alternate_exchange. |
408
|
|
|
* |
409
|
|
|
* @param Queue $alternateExchange |
410
|
|
|
* |
411
|
|
|
* @return Queue |
412
|
|
|
*/ |
413
|
|
|
public function setAlternateExchange(Queue $alternateExchange) |
414
|
|
|
{ |
415
|
|
|
$this->alternateExchange = $alternateExchange; |
416
|
|
|
|
417
|
|
|
return $this; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Get RabbitMq specific parameter : ttl. |
422
|
|
|
* |
423
|
|
|
* @return int |
424
|
|
|
*/ |
425
|
|
|
public function getTtl() |
426
|
|
|
{ |
427
|
|
|
return $this->ttl; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Set RabbitMq specific parameter : ttl. |
432
|
|
|
* |
433
|
|
|
* @param int $ttl |
434
|
|
|
* |
435
|
|
|
* @return Queue |
436
|
|
|
*/ |
437
|
|
|
public function setTtl($ttl) |
438
|
|
|
{ |
439
|
|
|
$this->ttl = $ttl; |
440
|
|
|
|
441
|
|
|
return $this; |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Get RabbitMq specific parameter : max length. |
446
|
|
|
* |
447
|
|
|
* @return int |
448
|
|
|
*/ |
449
|
|
|
public function getMaxLength() |
450
|
|
|
{ |
451
|
|
|
return $this->maxLength; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Set RabbitMq specific parameter : max length. |
456
|
|
|
* |
457
|
|
|
* @param int $ttl |
|
|
|
|
458
|
|
|
* |
459
|
|
|
* @return Queue |
460
|
|
|
*/ |
461
|
|
|
public function setMaxLength($maxLength) |
462
|
|
|
{ |
463
|
|
|
$this->maxLength = $maxLength; |
464
|
|
|
|
465
|
|
|
return $this; |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* Get RabbitMq specific parameter : max priority. |
470
|
|
|
* |
471
|
|
|
* @return int |
472
|
|
|
*/ |
473
|
|
|
public function getMaxPriority() |
474
|
|
|
{ |
475
|
|
|
return $this->maxPriority; |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
/** |
479
|
|
|
* Set RabbitMq specific parameter : max priority. |
480
|
|
|
* |
481
|
|
|
* @param int $maxPriority |
482
|
|
|
* |
483
|
|
|
* @return Queue |
484
|
|
|
*/ |
485
|
|
|
public function setMaxPriority($maxPriority) |
486
|
|
|
{ |
487
|
|
|
$this->maxPriority = $maxPriority; |
488
|
|
|
|
489
|
|
|
return $this; |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
public function init(AMQPChannel $amqpChannel) |
493
|
|
|
{ |
494
|
|
|
if (!$this->init) { |
495
|
|
|
if ($this->deadLetterExchanger) { |
496
|
|
|
$this->deadLetterExchanger->init($amqpChannel); |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
$parameters = []; |
500
|
|
|
if ($this->alternateExchange !== null) { |
501
|
|
|
$parameters['alternate-exchange'] = ['S', $this->alternateExchange->getName()]; |
502
|
|
|
} |
503
|
|
|
if ($this->confirm !== null) { |
504
|
|
|
$parameters['confirm'] = ['I', $this->confirm]; |
505
|
|
|
} |
506
|
|
|
if ($this->consumerCancelNotify !== null) { |
507
|
|
|
$parameters['consumer_cancel_notify'] = ['I', $this->consumerCancelNotify]; |
508
|
|
|
} |
509
|
|
|
if ($this->deadLetterExchanger !== null) { |
510
|
|
|
$parameters['x-dead-letter-exchange'] = ['S', $this->deadLetterExchanger->getName()]; |
511
|
|
|
} |
512
|
|
|
if ($this->maxLength) { |
513
|
|
|
$parameters['x-max-length'] = ['I', $this->maxLength]; |
514
|
|
|
} |
515
|
|
|
if ($this->maxPriority) { |
516
|
|
|
$parameters['x-max-priority'] = ['I', $this->maxPriority]; |
517
|
|
|
} |
518
|
|
|
if ($this->ttl) { |
519
|
|
|
$parameters['x-message-ttl'] = ['I', $this->ttl]; |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
if (!$parameters) { |
|
|
|
|
523
|
|
|
$parameters = null; |
524
|
|
|
} |
525
|
|
|
$amqpChannel->queue_declare($this->name, $this->passive, $this->durable, $this->exclusive, $this->autoDelete, $this->noWait, $parameters); |
526
|
|
|
$this->init = true; |
527
|
|
|
} |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
public function consume() |
531
|
|
|
{ |
532
|
|
|
$channel = $this->client->getChannel(); |
533
|
|
|
|
534
|
|
|
foreach ($this->consumers as $consumer) { |
|
|
|
|
535
|
|
|
/* @var $consumer ConsumerInterface */ |
536
|
|
|
$channel->basic_consume($this->name, |
537
|
|
|
$consumer->getConsumerTag(), |
538
|
|
|
$consumer->isNoLocal(), |
539
|
|
|
$consumer->isNoAck(), |
540
|
|
|
$consumer->isExclusive(), |
541
|
|
|
$consumer->isNoWait(), |
542
|
|
|
function (AMQPMessage $msg) use ($consumer) { |
543
|
|
|
$consumer->callback($msg); |
544
|
|
|
}, |
545
|
|
|
$consumer->getTicket(), |
546
|
|
|
$consumer->getArguments()); |
547
|
|
|
} |
548
|
|
|
} |
549
|
|
|
} |
550
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.