Completed
Push — 1.0 ( d56132...c155d9 )
by David
08:34
created

Client::getQueues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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