ChannelContainer::setChannelOption()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 7
cp 0
rs 10
cc 4
nc 8
nop 1
crap 20
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\TableSync\Rabbit;
6
7
use PhpAmqpLib\Channel\AMQPChannel;
8
9
class ChannelContainer
10
{
11
    private $connectionContainer;
12
    private $channel;
13
    private $channelOptions = [
14
        'prefetch_size' => null,
15
        'prefetch_count' => 1,
16
        'a_global' => true,
17
    ];
18
19
    public function __construct(ConnectionContainer $connectionContainer)
20
    {
21
        $this->connectionContainer = $connectionContainer;
22
    }
23
24
    public function __destruct()
25
    {
26
        if (isset($this->channel)) {
27
            $this->channel->close();
28
        }
29
    }
30
31
    public function setChannelOption($option)
32
    {
33
        if (!empty($option['prefetch_size'])) {
34
            $this->channelOptions['prefetch_size'] = $option['prefetch_size'];
35
        }
36
        if (!empty($option['prefetch_count'])) {
37
            $this->channelOptions['prefetch_count'] = $option['prefetch_count'];
38
        }
39
        if (!empty($option['a_global'])) {
40
            $this->channelOptions['a_global'] = $option['a_global'];
41
        }
42
    }
43
44
    public function getChannel(): AMQPChannel
45
    {
46
        if (!isset($this->channel)) {
47
            $this->channel = $this->connectionContainer->connection()
48
                ->channel();
49
            $this->channel->basic_qos(
50
                $this->channelOptions['prefetch_size'],
51
                $this->channelOptions['prefetch_count'],
52
                $this->channelOptions['a_global']
53
            );
54
        }
55
        return $this->channel;
56
    }
57
}
58