Completed
Push — 1.0 ( e7718b...afd35d )
by David
04:20
created

Queue::getDeadLetterExchanger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
0 ignored issues
show
Bug introduced by
There is no parameter named $source. Was it maybe removed?

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 method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
137
     * @param string              $name
138
     * @param ConsumerInterface[] $consumers
139
     */
140 View Code Duplication
    public function __construct(Client $client, $name, array $consumers = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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;
0 ignored issues
show
Bug introduced by
The property arguments does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $consumerCancelNotify of type object<Mouf\AmqpClient\Objects\Queue> is incompatible with the declared type boolean of property $consumerCancelNotify.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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
0 ignored issues
show
Bug introduced by
There is no parameter named $ttl. Was it maybe removed?

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 method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parameters of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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) {
0 ignored issues
show
Bug introduced by
The expression $this->consumers of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
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