Queue::define()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 2

Importance

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