Completed
Push — 1.0 ( dbae57...8e8fdf )
by Marc
02:17 queued 01:07
created

Queue::__contruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 3
Metric Value
c 5
b 0
f 3
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
1
<?php
2
3
namespace Mouf\AmqpClient\Objects;
4
5
use Mouf\AmqpClient\RabbitMqObjectInterface;
6
use PhpAmqpLib\Channel\AMQPChannel;
7
use Mouf\AmqpClient\ConsumerInterface;
8
use Mouf\AmqpClient\Client;
9
10
/**
11
 * @author Marc
12
 */
13
class Queue implements RabbitMqObjectInterface
14
{
15
    /**
16
     * @var Client
17
     */
18
    private $client;
19
20
    /**
21
     * @var Binding
22
     */
23
    private $source;
24
25
    /**
26
     * Queue name.
27
     *
28
     * @var string
29
     */
30
    private $name;
31
32
    /**
33
     * Queue.
34
     *
35
     * @var string
36
     */
37
    private $queue = '';
0 ignored issues
show
Unused Code introduced by
The property $queue is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

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