Completed
Push — 1.1 ( d8480c...39b04a )
by David
9s
created

Client::getChannel()   D

Complexity

Conditions 10
Paths 14

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 19
nc 14
nop 0

How to fix   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
    public function __construct($host, $port, $user, $password)
85
    {
86
        $this->host = $host;
87
        $this->port = ($port !== null) ? $port : 5672;
88
        $this->user = $user;
89
        $this->password = $password;
90
    }
91
92
    /**
93
     * Get prefetch size for QOS.
94
     */
95
    public function getPrefetchSize()
96
    {
97
        return $this->prefetchSize;
98
    }
99
100
    /**
101
     * Set prefetch size
102
     * It's for QOS prefetch-size http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.qos.
103
     *
104
     * @param int $prefetchSize
105
     */
106
    public function setPrefetchSize($prefetchSize)
107
    {
108
        $this->prefetchSize = $prefetchSize;
109
110
        return $this;
111
    }
112
113
    /**
114
     * Get prefetch count for QOS.
115
     */
116
    public function getPrefetchCount()
117
    {
118
        return $this->prefetchCount;
119
    }
120
121
    /**
122
     * Set prefetch size
123
     * It's for QOS prefetch-size http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.qos.
124
     *
125
     * @param int $prefetchCount
126
     */
127
    public function setPrefetchCount($prefetchCount)
128
    {
129
        $this->prefetchCount = $prefetchCount;
130
131
        return $this;
132
    }
133
134
    /**
135
     * Get a global for QOS.
136
     */
137
    public function getAGlobal()
138
    {
139
        return $this->aGlobal;
140
    }
141
142
    /**
143
     * Set global
144
     * It's for QOS prefetch-size http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.qos.
145
     *
146
     * @param int $aGlobal
147
     */
148
    public function setAGlobal($aGlobal)
149
    {
150
        $this->aGlobal = $aGlobal;
151
152
        return $this;
153
    }
154
155
    /**
156
     * Set RabbitMq object.
157
     *
158
     * @param RabbitMqObjectInterface[] $rabbitMqObjects
159
     */
160
    public function setRabbitMqObjects(array $rabbitMqObjects)
161
    {
162
        $this->rabbitMqObjects = $rabbitMqObjects;
163
    }
164
165
    public function register(RabbitMqObjectInterface $object)
166
    {
167
        if (!in_array($object, $this->rabbitMqObjects, true)) {
168
            $this->rabbitMqObjects[] = $object;
169
        }
170
    }
171
172
    /**
173
     * Connection to the RabbitMq service with AMQPStreamConnection.
174
     *
175
     * @return AMQPChannel
176
     */
177
    public function getChannel()
178
    {
179
        if (!$this->connection) {
180
            try {
181
                if (function_exists('socket_create')) {
182
                    $this->connection = new AMQPSocketConnection($this->host, $this->port, $this->user, $this->password);
183
                } else {
184
                    $this->connection = new AMQPStreamConnection($this->host, $this->port, $this->user, $this->password);
185
                }
186
            } 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...
187
                /* We are trying to catch the exception when the connection if refused */
188
                if (preg_match("/.*unable to connect.*Connection refused.*/", $e->__toString())) {
189
                    throw new ConnectionException("Cannot create the connection", 404, $e);
190
                }
191
                throw $e;
192
            } catch (AMQPIOException $e) {
193
                throw new ConnectionException("Cannot create the connection", 404, $e);
194
            }
195
            $this->channel = $this->connection->channel();
196
197
            if ($this->prefetchSize !== null || $this->prefetchCount !== null || $this->aGlobal !== null) {
198
                $this->channel->basic_qos($this->prefetchSize, $this->prefetchCount, $this->aGlobal);
199
            }
200
201
            foreach ($this->rabbitMqObjects as $rabbitMqObject) {
202
                $rabbitMqObject->init($this->channel);
203
            }
204
        }
205
206
        return $this->channel;
207
    }
208
209
    /**
210
     * Returns the list of registered queues.
211
     *
212
     * @return QueueInterface[]
213
     */
214
    public function getQueues()
215
    {
216
        return array_filter($this->rabbitMqObjects, function (RabbitMqObjectInterface $object) {
217
            return $object instanceof QueueInterface;
218
        });
219
    }
220
}
221