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

ConsumerService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 2 Features 1
Metric Value
wmc 6
c 4
b 2
f 1
lcom 1
cbo 3
dl 0
loc 43
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B run() 0 18 5
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