Completed
Push — develop ( 6bb646...82a853 )
by Baptiste
04:20
created

AutoDeclare   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 92%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 6
dl 0
loc 92
ccs 46
cts 50
cp 0.92
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 6
    public function __construct(Client $client)
25
    {
26 6
        $this->client = $client;
27 6
        $this->exchanges = new Set(Exchange::class);
28 6
        $this->queues = new Set(Queue::class);
29 6
        $this->bindings = new Set(Binding::class);
30 6
    }
31
32 4
    public function declareExchange(string $name, string $type, bool $durable, array $arguments): void
33
    {
34 4
        $constructor = $durable ? 'durable' : 'temporary';
35 4
        $exchange = Exchange::$constructor($name, Type::$type());
36
37 4
        foreach ($arguments as $key => $value) {
38
            $exchange = $exchange->withArgument($key, $value);
39
        }
40
41 4
        $this->exchanges = $this->exchanges->add($exchange);
42 4
    }
43
44 5
    public function declareQueue(string $name, bool $durable, bool $exclusive, array $arguments): void
45
    {
46 5
        $constructor = $durable ? 'durable' : 'temporary';
47 5
        $queue = Queue::$constructor()->withName($name);
48
49 5
        if ($exclusive) {
50
            $queue = $queue->exclusive();
51
        }
52
53 5
        foreach ($arguments as $key => $value) {
54
            $queue = $queue->withArgument($key, $value);
55
        }
56
57 5
        $this->queues = $this->queues->add($queue);
58 5
    }
59
60 4
    public function declareBinding(string $exchange, string $queue, string $routingKey, array $arguments): void
61
    {
62 4
        $binding = new Binding($exchange, $queue, $routingKey);
63
64 4
        foreach ($arguments as $key => $value) {
65
            $binding = $binding->withArgument($key, $value);
66
        }
67
68 4
        $this->bindings = $this->bindings->add($binding);
69 4
    }
70
71 6
    public function channel(): Channel
72
    {
73 6
        $channel = $this->client->channel();
74 6
        $this->declareThrough($channel);
75
76 6
        return $channel;
77
    }
78
79 1
    public function closed(): bool
80
    {
81 1
        return $this->client->closed();
82
    }
83
84 3
    public function close(): void
85
    {
86 3
        $this->client->close();
87 3
        $this->declared = true;
88 3
    }
89
90 6
    private function declareThrough(Channel $channel): void
91
    {
92 6
        if ($this->declared) {
93 4
            return;
94
        }
95
96 6
        $this->exchanges->foreach(static function(Exchange $command) use ($channel): void {
97 4
            $channel->exchange()->declare($command);
98 6
        });
99 6
        $this->queues->foreach(static function(Queue $command) use ($channel): void {
100 5
            $channel->queue()->declare($command);
101 6
        });
102 6
        $this->bindings->foreach(static function(Binding $command) use ($channel): void {
103 4
            $channel->queue()->bind($command);
104 6
        });
105
        $this->declared = true;
106
    }
107
}
108