Completed
Push — master ( e17409...9d6c1e )
by Sergey
04:06
created

Exchange::unbind()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 18
Ratio 100 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
dl 18
loc 18
ccs 13
cts 13
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 4
crap 2
1
<?php
2
3
namespace ButterAMQP\AMQP091;
4
5
use ButterAMQP\ExchangeInterface;
6
use ButterAMQP\AMQP091\Framing\Frame;
7
use ButterAMQP\AMQP091\Framing\Method\ExchangeBind;
8
use ButterAMQP\AMQP091\Framing\Method\ExchangeBindOk;
9
use ButterAMQP\AMQP091\Framing\Method\ExchangeDeclare;
10
use ButterAMQP\AMQP091\Framing\Method\ExchangeDeclareOk;
11
use ButterAMQP\AMQP091\Framing\Method\ExchangeDelete;
12
use ButterAMQP\AMQP091\Framing\Method\ExchangeDeleteOk;
13
use ButterAMQP\AMQP091\Framing\Method\ExchangeUnbind;
14
use ButterAMQP\AMQP091\Framing\Method\ExchangeUnbindOk;
15
use ButterAMQP\WireInterface;
16
17
class Exchange implements ExchangeInterface
18
{
19
    const FLAG_NO_WAIT = 0b00000001;
20
    const FLAG_DURABLE = 0b00000010;
21
    const FLAG_PASSIVE = 0b00000100;
22
    const FLAG_AUTO_DELETE = 0b00001000;
23
    const FLAG_INTERNAL = 0b00010000;
24
    const FLAG_IF_UNUSED = 0b00100000;
25
26
    const TYPE_TOPIC = 'topic';
27
    const TYPE_DIRECT = 'direct';
28
    const TYPE_FANOUT = 'fanout';
29
    const TYPE_HEADERS = 'headers';
30
31
    /**
32
     * @var WireInterface
33
     */
34
    private $wire;
35
36
    /**
37
     * @var int
38
     */
39
    private $channel;
40
41
    /**
42
     * @var string
43
     */
44
    private $name;
45
46
    /**
47
     * @param WireInterface $wire
48
     * @param int           $channel
49
     * @param string        $name
50
     */
51 14
    public function __construct(WireInterface $wire, $channel, $name)
52
    {
53 14
        $this->wire = $wire;
54 14
        $this->channel = $channel;
55 14
        $this->name = $name;
56 14
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 5
    public function define($type, $flags = 0, array $arguments = [])
62
    {
63 5
        $this->send(new ExchangeDeclare(
64 5
            $this->channel,
65 5
            0,
66 5
            $this->name,
67 5
            $type,
68 5
            (bool) ($flags & self::FLAG_PASSIVE),
69 5
            (bool) ($flags & self::FLAG_DURABLE),
70 5
            (bool) ($flags & self::FLAG_AUTO_DELETE),
71 5
            (bool) ($flags & self::FLAG_INTERNAL),
72 5
            (bool) ($flags & self::FLAG_NO_WAIT),
73
            $arguments
74 5
        ));
75
76 5
        if (!($flags & self::FLAG_NO_WAIT)) {
77 4
            $this->wait(ExchangeDeclareOk::class);
78 4
        }
79
80 5
        return $this;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 4
    public function delete($flags = 0)
87
    {
88 4
        $this->send(new ExchangeDelete(
89 4
            $this->channel,
90 4
            0,
91 4
            $this->name,
92 4
            (bool) ($flags & self::FLAG_IF_UNUSED),
93 4
            (bool) ($flags & self::FLAG_NO_WAIT)
94 4
        ));
95
96 4
        if (!($flags & self::FLAG_NO_WAIT)) {
97 3
            $this->wait(ExchangeDeleteOk::class);
98 3
        }
99
100 4
        return $this;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 3 View Code Duplication
    public function bind($exchange, $routingKey = '', array $arguments = [], $flags = 0)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
    {
108 3
        $this->send(new ExchangeBind(
109 3
            $this->channel,
110 3
            0,
111 3
            $exchange,
112 3
            $this->name,
113 3
            $routingKey,
114 3
            (bool) ($flags & self::FLAG_NO_WAIT),
115
            $arguments
116 3
        ));
117
118 3
        if (!($flags & self::FLAG_NO_WAIT)) {
119 1
            $this->wait(ExchangeBindOk::class);
120 1
        }
121
122 3
        return $this;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 1 View Code Duplication
    public function unbind($exchange, $routingKey = '', array $arguments = [], $flags = 0)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130 1
        $this->send(new ExchangeUnbind(
131 1
            $this->channel,
132 1
            0,
133 1
            $exchange,
134 1
            $this->name,
135 1
            $routingKey,
136 1
            (bool) ($flags & self::FLAG_NO_WAIT),
137
            $arguments
138 1
        ));
139
140 1
        if (!($flags & self::FLAG_NO_WAIT)) {
141 1
            $this->wait(ExchangeUnbindOk::class);
142 1
        }
143
144 1
        return $this;
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150 2
    public function name()
151
    {
152 2
        return $this->name;
153
    }
154
155
    /**
156
     * @return string
157
     */
158 3
    public function __toString()
159
    {
160 3
        return $this->name;
161
    }
162
163
    /**
164
     * @param Frame $frame
165
     *
166
     * @return $this
167
     */
168 11
    private function send(Frame $frame)
169
    {
170 11
        $this->wire->send($frame);
171
172 11
        return $this;
173
    }
174
175
    /**
176
     * @param string|array $type
177
     *
178
     * @return Frame
179
     */
180 7
    private function wait($type)
181
    {
182 7
        return $this->wire->wait($this->channel, $type);
183
    }
184
}
185