ExchangeDeclarer::declareExchange()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Ccovey\RabbitMQ;
4
5
use Ccovey\RabbitMQ\Connection\Connection;
6
7
class ExchangeDeclarer
8
{
9
    /**
10
     * @var array
11
     */
12
    private $declaredExchanges = [];
13
14
    /**
15
     * @var Connection
16
     */
17
    private $connection;
18
19
    /**
20
     * @var ChannelInterface
21
     */
22
    private $channel;
23
24 1
    public function __construct(Connection $connection, string $channelId = '')
25
    {
26 1
        $this->connection = $connection;
27 1
        $this->channel = $this->connection->getChannel($channelId);
28 1
    }
29
30
    /*
31
     * This method will be run each time we attempt to queue a message.
32
     * We will cache locally which exchanges we have already declared.
33
     * Declaring an exchange on each iteration of a worker consuming from
34
     * a queue is really slow.
35
     */
36 1
    public function declareExchange(Exchange $exchange)
37
    {
38 1
        if (!in_array($exchange, $this->declaredExchanges)) {
39 1
            $this->channel->declareExchange($exchange);
40 1
            $this->declaredExchanges[] = $exchange;
41
        }
42 1
    }
43
}
44