Completed
Pull Request — 1.1 (#12)
by David
01:56
created

Client::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 4
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
11
class Client
12
{
13
    /**
14
     * RabbitMq host.
15
     *
16
     * @var string
17
     */
18
    private $host;
19
20
    /**
21
     * RabbitMq port.
22
     *
23
     * @var string
24
     */
25
    private $port;
26
27
    /**
28
     * RabbitMq user.
29
     *
30
     * @var string
31
     */
32
    private $user;
33
34
    /**
35
     * RabbitMq password.
36
     *
37
     * @var string
38
     */
39
    private $password;
40
41
    /**
42
     * It's for QOS prefetch-size http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.qos.
43
     *
44
     * @var int
45
     */
46
    private $prefetchSize = null;
47
48
    /**
49
     * It's for QOS prefetch-count http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.qos.
50
     *
51
     * @var int
52
     */
53
    private $prefetchCount = null;
54
55
    /**
56
     * It's for QOS global http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.qos.
57
     *
58
     * @var int
59
     */
60
    private $aGlobal = null;
61
62
    /**
63
     * RabbitMq connection.
64
     *
65
     * @var AbstractConnection
66
     */
67
    private $connection = null;
68
69
    /**
70
     * RabbitMq channel.
71
     *
72
     * @var \AMQPChannel
73
     */
74
    private $channel = null;
75
76
    /**
77
     * List of RabbitMq object.
78
     *
79
     * @var RabbitMqObjectInterface[]
80
     */
81
    private $rabbitMqObjects = [];
82
83
    public function __construct($host, $port, $user, $password)
84
    {
85
        $this->host = $host;
86
        $this->port = ($port !== null) ? $port : 5672;
87
        $this->user = $user;
88
        $this->password = $password;
89
    }
90
91
    /**
92
     * Get prefetch size for QOS.
93
     */
94
    public function getPrefetchSize()
95
    {
96
        return $this->prefetchSize;
97
    }
98
99
    /**
100
     * Set prefetch size
101
     * It's for QOS prefetch-size http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.qos.
102
     *
103
     * @param int $prefetchSize
104
     */
105
    public function setPrefetchSize($prefetchSize)
106
    {
107
        $this->prefetchSize = $prefetchSize;
108
109
        return $this;
110
    }
111
112
    /**
113
     * Get prefetch count for QOS.
114
     */
115
    public function getPrefetchCount()
116
    {
117
        return $this->prefetchCount;
118
    }
119
120
    /**
121
     * Set prefetch size
122
     * It's for QOS prefetch-size http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.qos.
123
     *
124
     * @param int $prefetchCount
125
     */
126
    public function setPrefetchCount($prefetchCount)
127
    {
128
        $this->prefetchCount = $prefetchCount;
129
130
        return $this;
131
    }
132
133
    /**
134
     * Get a global for QOS.
135
     */
136
    public function getAGlobal()
137
    {
138
        return $this->aGlobal;
139
    }
140
141
    /**
142
     * Set global
143
     * It's for QOS prefetch-size http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.qos.
144
     *
145
     * @param int $aGlobal
146
     */
147
    public function setAGlobal($aGlobal)
148
    {
149
        $this->aGlobal = $aGlobal;
150
151
        return $this;
152
    }
153
154
    /**
155
     * Set RabbitMq object.
156
     *
157
     * @param RabbitMqObjectInterface[] $rabbitMqObjects
158
     */
159
    public function setRabbitMqObjects(array $rabbitMqObjects)
160
    {
161
        $this->rabbitMqObjects = $rabbitMqObjects;
162
    }
163
164
    public function register(RabbitMqObjectInterface $object)
165
    {
166
        if (!in_array($object, $this->rabbitMqObjects, true)) {
167
            $this->rabbitMqObjects[] = $object;
168
        }
169
    }
170
171
    /**
172
     * Connection to the RabbitMq service with AMQPStreamConnection.
173
     *
174
     * @return AMQPChannel
175
     */
176
    public function getChannel()
177
    {
178
        if (!$this->connection) {
179
            try {
180
                if (function_exists('socket_create')) {
181
                    $this->connection = new AMQPSocketConnection($this->host, $this->port, $this->user, $this->password);
182
                } else {
183
                    $this->connection = new AMQPStreamConnection($this->host, $this->port, $this->user, $this->password);
184
                }
185
            } 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...
186
                /* We are trying to catch the exception when the connection if refused */
187
                if (preg_match("/.*unable to connect.*Connection refused.*/", $e->__toString())) {
188
                    throw new ConnectionException("Cannot create the connection", 404, $e);
189
                }
190
                throw $e;
191
            } catch (\AMQPIOException $e) {
0 ignored issues
show
Bug introduced by
The class AMQPIOException 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...
192
                throw new ConnectionException("Cannot create the connection", 404, $e);
193
            }
194
            $this->channel = $this->connection->channel();
195
196
            if ($this->prefetchSize !== null || $this->prefetchCount !== null || $this->aGlobal !== null) {
197
                $this->channel->basic_qos($this->prefetchSize, $this->prefetchCount, $this->aGlobal);
198
            }
199
200
            foreach ($this->rabbitMqObjects as $rabbitMqObject) {
201
                $rabbitMqObject->init($this->channel);
202
            }
203
        }
204
205
        return $this->channel;
206
    }
207
208
    /**
209
     * Returns the list of registered queues.
210
     *
211
     * @return QueueInterface[]
212
     */
213
    public function getQueues()
214
    {
215
        return array_filter($this->rabbitMqObjects, function (RabbitMqObjectInterface $object) {
216
            return $object instanceof QueueInterface;
217
        });
218
    }
219
}
220