QueueDeclarer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 59
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A declareQueue() 0 23 3
1
<?php
2
3
namespace Ccovey\RabbitMQ;
4
5
use Ccovey\RabbitMQ\Config\QueueConfig;
6
use Ccovey\RabbitMQ\Connection\Connection;
7
8
class QueueDeclarer
9
{
10
    /**
11
     * @var array
12
     */
13
    private $declaredQueues = [];
14
15
    /**
16
     * @var Connection
17
     */
18
    private $connection;
19
20
    /**
21
     * @var QueueConfig
22
     */
23
    private $config;
24
25
    /**
26
     * @var ChannelInterface
27
     */
28
    private $channel;
29
30 2
    public function __construct(Connection $connection, QueueConfig $config, string $channelId = '')
31
    {
32 2
        $this->connection = $connection;
33 2
        $this->config = $config;
34 2
        $this->channel = $this->connection->getChannel($channelId);
35 2
    }
36
37
    /*
38
     * This method will be run each time we attempt to queue a message.
39
     * We will cache locally which queues we have already declared.
40
     * Declaring a queue on each iteration of a worker consuming from
41
     * a queue is really slow.
42
     */
43 2
    public function declareQueue($queueName)
44
    {
45 2
        if (!in_array($queueName, $this->declaredQueues)) {
46 2
            $queue = new Queue(
47
                $queueName,
48 2
                $this->config->getExchange(),
49 2
                $this->config->getAmqpTable(),
50 2
                false,
51 2
                $this->config->isDurable(),
52 2
                $this->config->isExclusive(),
53 2
                $this->config->isAutoDelete(),
54 2
                $this->config->isNoWait(),
55 2
                $this->config->getTicket()
56
            );
57
58 2
            $this->channel->declareQueue($queue);
59
60 2
            if ($this->config->getExchange() !== '') {
61 1
                $this->channel->bindQueue($queue);
62
            }
63 2
            $this->declaredQueues[] = $queueName;
64
        }
65 2
    }
66
}
67