QueueDeclarer::declareQueue()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 16
cts 16
cp 1
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 1
crap 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