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

Queue::unbind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 3
crap 1
1
<?php
2
3
namespace ButterAMQP\AMQP091;
4
5
use ButterAMQP\AMQP091\Framing\Frame;
6
use ButterAMQP\AMQP091\Framing\Method\QueueBind;
7
use ButterAMQP\AMQP091\Framing\Method\QueueBindOk;
8
use ButterAMQP\AMQP091\Framing\Method\QueueDeclare;
9
use ButterAMQP\AMQP091\Framing\Method\QueueDeclareOk;
10
use ButterAMQP\AMQP091\Framing\Method\QueueDelete;
11
use ButterAMQP\AMQP091\Framing\Method\QueueDeleteOk;
12
use ButterAMQP\AMQP091\Framing\Method\QueuePurge;
13
use ButterAMQP\AMQP091\Framing\Method\QueuePurgeOk;
14
use ButterAMQP\AMQP091\Framing\Method\QueueUnbind;
15
use ButterAMQP\AMQP091\Framing\Method\QueueUnbindOk;
16
use ButterAMQP\QueueInterface;
17
use ButterAMQP\WireInterface;
18
19
class Queue implements QueueInterface
20
{
21
    const FLAG_NO_WAIT = 0b00000001;
22
    const FLAG_DURABLE = 0b00000010;
23
    const FLAG_PASSIVE = 0b00000100;
24
    const FLAG_EXCLUSIVE = 0b00001000;
25
    const FLAG_AUTO_DELETE = 0b00010000;
26
    const FLAG_INTERNAL = 0b00100000;
27
    const FLAG_IF_UNUSED = 0b01000000;
28
    const FLAG_IF_EMPTY = 0b10000000;
29
30
    /**
31
     * @var WireInterface
32
     */
33
    private $wire;
34
35
    /**
36
     * @var int
37
     */
38
    private $channel;
39
40
    /**
41
     * @var string
42
     */
43
    private $name;
44
45
    /**
46
     * @var int|null
47
     */
48
    private $messageCount;
49
50
    /**
51
     * @var int|null
52
     */
53
    private $consumerCount;
54
55
    /**
56
     * @param WireInterface $wire
57
     * @param int           $channel
58
     * @param string        $name
59
     */
60 25
    public function __construct(WireInterface $wire, $channel, $name)
61
    {
62 25
        $this->wire = $wire;
63 25
        $this->channel = $channel;
64 25
        $this->name = $name;
65 25
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 15
    public function define($flags = 0, array $arguments = [])
71
    {
72 15
        $this->send(new QueueDeclare(
73 15
            $this->channel,
74 15
            0,
75 15
            $this->name,
76 15
            $flags & self::FLAG_PASSIVE,
77 15
            $flags & self::FLAG_DURABLE,
78 15
            $flags & self::FLAG_EXCLUSIVE,
79 15
            $flags & self::FLAG_AUTO_DELETE,
80 15
            $flags & self::FLAG_NO_WAIT,
81
            $arguments
82 15
        ));
83
84 15
        if ($flags & self::FLAG_NO_WAIT) {
85 1
            return $this;
86
        }
87
88
        /** @var QueueDeclareOk $frame */
89 14
        $frame = $this->wait(QueueDeclareOk::class);
90
91 14
        $this->name = $frame->getQueue();
92 14
        $this->messageCount = $frame->getMessageCount();
93 14
        $this->consumerCount = $frame->getConsumerCount();
94
95 14
        return $this;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 3
    public function delete($flags = 0)
102
    {
103 3
        $this->send(new QueueDelete(
104 3
            $this->channel,
105 3
            0,
106 3
            $this->name,
107 3
            $flags & self::FLAG_IF_UNUSED,
108 3
            $flags & self::FLAG_IF_EMPTY,
109
            $flags & self::FLAG_NO_WAIT
110 3
        ));
111
112 3
        if ($flags & self::FLAG_NO_WAIT) {
113 1
            return $this;
114
        }
115
116
        /** @var QueueDeleteOk $frame */
117 2
        $frame = $this->wait(QueueDeleteOk::class);
118
119 2
        $this->messageCount = $frame->getMessageCount();
120
121 2
        return $this;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 4 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...
128
    {
129 4
        $this->send(new QueueBind(
130 4
            $this->channel,
131 4
            0,
132 4
            $this->name,
133 4
            (string) $exchange,
134 4
            $routingKey,
135 4
            $flags & self::FLAG_NO_WAIT,
136
            $arguments
137 4
        ));
138
139 4
        if (!($flags & self::FLAG_NO_WAIT)) {
140 3
            $this->wait(QueueBindOk::class);
141 3
        }
142
143 4
        return $this;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149 1
    public function unbind($exchange, $routingKey = '', array $arguments = [])
150
    {
151 1
        $this->send(new QueueUnbind(
152 1
            $this->channel,
153 1
            0,
154 1
            $this->name,
155 1
            $exchange,
156 1
            $routingKey,
157
            $arguments
158 1
        ));
159
160 1
        $this->wait(QueueUnbindOk::class);
161
162 1
        return $this;
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168 2
    public function purge($flags = 0)
169
    {
170 2
        $this->send(new QueuePurge(
171 2
            $this->channel,
172 2
            0,
173 2
            $this->name,
174 2
            (bool) ($flags & self::FLAG_NO_WAIT)
175 2
        ));
176
177 2
        if ($flags & self::FLAG_NO_WAIT) {
178 1
            return $this;
179
        }
180
181
        /** @var QueuePurgeOk $frame */
182 1
        $frame = $this->wait(QueuePurgeOk::class);
183
184 1
        $this->messageCount = $frame->getMessageCount();
185
186 1
        return $this;
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     */
192 4
    public function name()
193
    {
194 4
        return $this->name;
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200 7
    public function messagesCount()
201
    {
202 7
        return $this->messageCount;
203
    }
204
205
    /**
206
     * {@inheritdoc}
207
     */
208 1
    public function consumerCount()
209
    {
210 1
        return $this->consumerCount;
211
    }
212
213
    /**
214
     * @return string
215
     */
216 14
    public function __toString()
217
    {
218 14
        return $this->name;
219
    }
220
221
    /**
222
     * @param Frame $frame
223
     *
224
     * @return $this
225
     */
226 22
    private function send(Frame $frame)
227
    {
228 22
        $this->wire->send($frame);
229
230 22
        return $this;
231
    }
232
233
    /**
234
     * @param string|array $type
235
     *
236
     * @return Frame
237
     */
238 18
    private function wait($type)
239
    {
240 18
        return $this->wire->wait($this->channel, $type);
241
    }
242
}
243