Completed
Push — 1.0 ( 96ad2d...e7718b )
by David
06:45
created

ConsumerService::run()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 18
rs 8.8571
cc 5
eloc 10
nc 8
nop 1
1
<?php
2
3
namespace Mouf\AmqpClient;
4
5
use Mouf\AmqpClient\Objects\Queue;
6
7
/**
8
 * Function to consume RabbitMq queue.
9
 * 
10
 * @author Marc Teyssier
11
 */
12
class ConsumerService
13
{
14
    /**
15
     * @param Client $client
16
     */
17
    private $client;
18
19
    /**
20
     * @param Queue[] $queues
21
     */
22
    private $queues;
23
24
    /**
25
     * @param Queue[] $queues List of queue to listen
26
     */
27
    public function __construct(Client $client, $queues)
28
    {
29
        $this->client = $client;
30
        $this->queues = $queues;
31
    }
32
33
    /**
34
     * Call this function in your script to consume the RabbitMq queue.
35
     */
36
    public function run($onlyOne = false)
37
    {
38
        foreach ($this->queues as $queue) {
39
            /* @var Queue $queue */
40
            $queue->consume();
41
        }
42
43
        $channel = $this->client->getChannel();
44
        if ($onlyOne) {
45
            if (count($channel->callbacks)) {
46
                $channel->wait();
47
            }
48
        } else {
49
            while (count($channel->callbacks)) {
50
                $channel->wait();
51
            }
52
        }
53
    }
54
}
55