AutoDeclare   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 6
dl 0
loc 92
ccs 48
cts 52
cp 0.9231
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A declareExchange() 0 11 3
A declareQueue() 0 15 4
A declareBinding() 0 10 2
A channel() 0 7 1
A closed() 0 4 1
A close() 0 5 1
A declareThrough() 0 17 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\AMQPBundle\Client;
5
6
use Innmind\AMQP\{
7
    Client,
8
    Client\Channel,
9
    Model\Exchange\Declaration as Exchange,
10
    Model\Exchange\Type,
11
    Model\Queue\Declaration as Queue,
12
    Model\Queue\Binding
13
};
14
use Innmind\Immutable\Set;
15
16
final class AutoDeclare implements Client
17
{
18
    private $client;
19
    private $exchanges;
20
    private $queues;
21
    private $bindings;
22
    private $declared = false;
23
24 16
    public function __construct(Client $client)
25
    {
26 16
        $this->client = $client;
27 16
        $this->exchanges = new Set(Exchange::class);
28 16
        $this->queues = new Set(Queue::class);
29 16
        $this->bindings = new Set(Binding::class);
30 16
    }
31
32 10
    public function declareExchange(string $name, string $type, bool $durable, array $arguments): void
33
    {
34 10
        $constructor = $durable ? 'durable' : 'temporary';
35 10
        $exchange = Exchange::$constructor($name, Type::$type());
36
37 10
        foreach ($arguments as $key => $value) {
38
            $exchange = $exchange->withArgument($key, $value);
39
        }
40
41 10
        $this->exchanges = $this->exchanges->add($exchange);
42 10
    }
43
44 12
    public function declareQueue(string $name, bool $durable, bool $exclusive, array $arguments): void
45
    {
46 12
        $constructor = $durable ? 'durable' : 'temporary';
47 12
        $queue = Queue::$constructor()->withName($name);
48
49 12
        if ($exclusive) {
50
            $queue = $queue->exclusive();
51
        }
52
53 12
        foreach ($arguments as $key => $value) {
54
            $queue = $queue->withArgument($key, $value);
55
        }
56
57 12
        $this->queues = $this->queues->add($queue);
58 12
    }
59
60 10
    public function declareBinding(string $exchange, string $queue, string $routingKey, array $arguments): void
61
    {
62 10
        $binding = new Binding($exchange, $queue, $routingKey);
63
64 10
        foreach ($arguments as $key => $value) {
65
            $binding = $binding->withArgument($key, $value);
66
        }
67
68 10
        $this->bindings = $this->bindings->add($binding);
69 10
    }
70
71 14
    public function channel(): Channel
72
    {
73 14
        $channel = $this->client->channel();
74 14
        $this->declareThrough($channel);
75
76 14
        return $channel;
77
    }
78
79 2
    public function closed(): bool
80
    {
81 2
        return $this->client->closed();
82
    }
83
84 6
    public function close(): void
85
    {
86 6
        $this->client->close();
87 6
        $this->declared = true;
88 6
    }
89
90 14
    private function declareThrough(Channel $channel): void
91
    {
92 14
        if ($this->declared) {
93 10
            return;
94
        }
95
96 14
        $this->exchanges->foreach(static function(Exchange $command) use ($channel): void {
97 10
            $channel->exchange()->declare($command);
98 14
        });
99 14
        $this->queues->foreach(static function(Queue $command) use ($channel): void {
100 12
            $channel->queue()->declare($command);
101 14
        });
102 14
        $this->bindings->foreach(static function(Binding $command) use ($channel): void {
103 10
            $channel->queue()->bind($command);
104 14
        });
105 14
        $this->declared = true;
106 14
    }
107
}
108