ChannelContainer   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 24
c 1
b 0
f 0
dl 0
loc 47
ccs 0
cts 22
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __destruct() 0 4 2
A __construct() 0 3 1
A setChannelOption() 0 10 4
A getChannel() 0 12 2
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