Client::getChannel()   C
last analyzed

Complexity

Conditions 10
Paths 14

Size

Total Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 6.9842
c 0
b 0
f 0
cc 10
nc 14
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Mouf\AmqpClient;
4
5
use PhpAmqpLib\Connection\AbstractConnection;
6
use PhpAmqpLib\Connection\AMQPSocketConnection;
7
use PhpAmqpLib\Connection\AMQPStreamConnection;
8
use PhpAmqpLib\Channel\AMQPChannel;
9
use Mouf\AmqpClient\Exception\ConnectionException;
10
use PhpAmqpLib\Exception\AMQPIOException;
11
12
class Client
13
{
14
    /**
15
     * RabbitMq host.
16
     *
17
     * @var string
18
     */
19
    private $host;
20
21
    /**
22
     * RabbitMq port.
23
     *
24
     * @var string
25
     */
26
    private $port;
27
28
    /**
29
     * RabbitMq user.
30
     *
31
     * @var string
32
     */
33
    private $user;
34
35
    /**
36
     * RabbitMq password.
37
     *
38
     * @var string
39
     */
40
    private $password;
41
42
    /**
43
     * It's for QOS prefetch-size http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.qos.
44
     *
45
     * @var int
46
     */
47
    private $prefetchSize = null;
48
49
    /**
50
     * It's for QOS prefetch-count http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.qos.
51
     *
52
     * @var int
53
     */
54
    private $prefetchCount = null;
55
56
    /**
57
     * It's for QOS global http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.qos.
58
     *
59
     * @var int
60
     */
61
    private $aGlobal = null;
62
63
    /**
64
     * RabbitMq connection.
65
     *
66
     * @var AbstractConnection
67
     */
68
    private $connection = null;
69
70
    /**
71
     * RabbitMq channel.
72
     *
73
     * @var \AMQPChannel
74
     */
75
    private $channel = null;
76
77
    /**
78
     * List of RabbitMq object.
79
     *
80
     * @var RabbitMqObjectInterface[]
81
     */
82
    private $rabbitMqObjects = [];
83
84
    /**
85
     * RabbitMq virtual host
86
     *
87
     * @var string
88
     */
89
    private $vhost;
90
91
    /**
92
     * RabbitMq insist
93
     *
94
     * @var bool
95
     */
96
    private $insist;
97
98
    /**
99
     * RabbitMq login method
100
     *
101
     * @var string
102
     */
103
    private $login_method;
104
105
    /**
106
     * RabbitMq login response
107
     *
108
     * @var null
109
     */
110
    private $login_response;
111
112
    /**
113
     * RabbitMq locale
114
     *
115
     * @var string
116
     */
117
    private $locale;
118
119
    /**
120
     * RabbitMq connection timeout
121
     *
122
     * @var float
123
     */
124
    private $connection_timeout;
125
126
    /**
127
     * RabbitMq read write timeout
128
     *
129
     * @var float
130
     */
131
    private $read_write_timeout;
132
133
    /**
134
     * RabbitMq context
135
     *
136
     * @var null
137
     */
138
    private $context;
139
140
    /**
141
     * RabbitMq keep a live
142
     *
143
     * @var bool
144
     */
145
    private $keepalive;
146
147
    /**
148
     * RabbitMq heartbeat
149
     *
150
     * @var int
151
     */
152
    private $heartbeat;
153
154
    /**
155
     * Client constructor.
156
     * @param $host
157
     * @param $port
158
     * @param $user
159
     * @param $password
160
     * @param string $vhost
161
     * @param bool $insist
162
     * @param string $login_method
163
     * @param null $login_response
164
     * @param string $locale
165
     * @param float $connection_timeout
166
     * @param float $read_write_timeout
167
     * @param null $context
168
     * @param bool $keepalive
169
     * @param int $heartbeat
170
     */
171
    public function __construct(
172
        $host,
173
        $port,
174
        $user,
175
        $password,
176
        $vhost = '/',
177
        $insist = false,
178
        $login_method = 'AMQPLAIN',
179
        $login_response = null,
180
        $locale = 'en_US',
181
        $connection_timeout = 3.0,
182
        $read_write_timeout = 3.0,
183
        $context = null,
184
        $keepalive = false,
185
        $heartbeat = 0)
186
    {
187
        $this->host = $host;
188
        $this->port = ($port !== null) ? $port : 5672;
189
        $this->user = $user;
190
        $this->password = $password;
191
        $this->vhost = $vhost;
192
        $this->insist = $insist;
193
        $this->login_method = $login_method;
194
        $this->login_response = $login_response;
195
        $this->locale = $locale;
196
        $this->connection_timeout = $connection_timeout;
197
        $this->read_write_timeout = $read_write_timeout;
198
        $this->context = $context;
199
        $this->keepalive = $keepalive;
200
        $this->heartbeat = $heartbeat;
201
    }
202
203
    /**
204
     * Get prefetch size for QOS.
205
     */
206
    public function getPrefetchSize()
207
    {
208
        return $this->prefetchSize;
209
    }
210
211
    /**
212
     * Set prefetch size
213
     * It's for QOS prefetch-size http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.qos.
214
     *
215
     * @param int $prefetchSize
216
     * @return Client
217
     */
218
    public function setPrefetchSize($prefetchSize)
219
    {
220
        $this->prefetchSize = $prefetchSize;
221
222
        return $this;
223
    }
224
225
    /**
226
     * Get prefetch count for QOS.
227
     */
228
    public function getPrefetchCount()
229
    {
230
        return $this->prefetchCount;
231
    }
232
233
    /**
234
     * Set prefetch size
235
     * It's for QOS prefetch-size http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.qos.
236
     *
237
     * @param int $prefetchCount
238
     * @return Client
239
     */
240
    public function setPrefetchCount($prefetchCount)
241
    {
242
        $this->prefetchCount = $prefetchCount;
243
244
        return $this;
245
    }
246
247
    /**
248
     * Get a global for QOS.
249
     */
250
    public function getAGlobal()
251
    {
252
        return $this->aGlobal;
253
    }
254
255
    /**
256
     * Set global
257
     * It's for QOS prefetch-size http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.qos.
258
     *
259
     * @param int $aGlobal
260
     * @return Client
261
     */
262
    public function setAGlobal($aGlobal)
263
    {
264
        $this->aGlobal = $aGlobal;
265
266
        return $this;
267
    }
268
269
    /**
270
     * Set RabbitMq object.
271
     *
272
     * @param RabbitMqObjectInterface[] $rabbitMqObjects
273
     */
274
    public function setRabbitMqObjects(array $rabbitMqObjects)
275
    {
276
        $this->rabbitMqObjects = $rabbitMqObjects;
277
    }
278
279
    public function register(RabbitMqObjectInterface $object)
280
    {
281
        if (!in_array($object, $this->rabbitMqObjects, true)) {
282
            $this->rabbitMqObjects[] = $object;
283
        }
284
    }
285
286
    /**
287
     * Connection to the RabbitMq service with AMQPStreamConnection.
288
     *
289
     * @return AMQPChannel
290
     * @throws ConnectionException
291
     * @throws \ErrorException
292
     */
293
    public function getChannel()
294
    {
295
        if (!$this->connection) {
296
            try {
297
                if (function_exists('socket_create')) {
298
                    $this->connection = new AMQPStreamConnection(
299
                        $this->host,
300
                        $this->port,
301
                        $this->user,
302
                        $this->password,
303
                        $this->vhost,
304
                        $this->insist,
305
                        $this->login_method,
306
                        $this->login_response,
307
                        $this->locale,
308
                        $this->connection_timeout,
309
                        $this->read_write_timeout,
310
                        $this->context,
311
                        $this->keepalive ,
312
                        $this->heartbeat
313
                    );
314
                } else {
315
                    $this->connection = new AMQPStreamConnection(
316
                        $this->host,
317
                        $this->port,
318
                        $this->user,
319
                        $this->password,
320
                        $this->vhost,
321
                        $this->insist,
322
                        $this->login_method,
323
                        $this->login_response,
324
                        $this->locale,
325
                        $this->connection_timeout,
326
                        $this->read_write_timeout,
327
                        $this->context,
328
                        $this->keepalive ,
329
                        $this->heartbeat
330
                    );
331
                }
332
            } catch (\ErrorException $e) {
0 ignored issues
show
Bug introduced by
The class ErrorException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
333
                /* We are trying to catch the exception when the connection if refused */
334
                if (preg_match("/.*unable to connect.*Connection refused.*/", $e->__toString())) {
335
                    throw new ConnectionException("Cannot create the connection", 404, $e);
336
                }
337
                throw $e;
338
            } catch (AMQPIOException $e) {
339
                throw new ConnectionException("Cannot create the connection", 404, $e);
340
            }
341
            $this->channel = $this->connection->channel();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->connection->channel() of type object<PhpAmqpLib\Channel\AMQPChannel> is incompatible with the declared type object<AMQPChannel> of property $channel.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
342
343
            if ($this->prefetchSize !== null || $this->prefetchCount !== null || $this->aGlobal !== null) {
344
                $this->channel->basic_qos($this->prefetchSize, $this->prefetchCount, $this->aGlobal);
0 ignored issues
show
Documentation introduced by
$this->aGlobal is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
345
            }
346
347
            foreach ($this->rabbitMqObjects as $rabbitMqObject) {
348
                $rabbitMqObject->init($this->channel);
349
            }
350
        }
351
352
        return $this->channel;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->channel; of type PhpAmqpLib\Channel\AMQPChannel|AMQPChannel adds the type AMQPChannel to the return on line 352 which is incompatible with the return type documented by Mouf\AmqpClient\Client::getChannel of type PhpAmqpLib\Channel\AMQPChannel.
Loading history...
353
    }
354
355
    /**
356
     * Returns the list of registered queues.
357
     *
358
     * @return QueueInterface[]
359
     */
360
    public function getQueues()
361
    {
362
        return array_filter($this->rabbitMqObjects, function (RabbitMqObjectInterface $object) {
363
            return $object instanceof QueueInterface;
364
        });
365
    }
366
}
367