1
|
|
|
<?php |
2
|
|
|
namespace Mouf\AmqpClient; |
3
|
|
|
|
4
|
|
|
use PhpAmqpLib\Connection\AMQPStreamConnection; |
5
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
6
|
|
|
|
7
|
|
|
class Client { |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* RabbitMq host |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private $host; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* RabbitMq port |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $port; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* RabbitMq user |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $user; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* RabbitMq password |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $password; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* It's for QOS prefetch-size http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.qos |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
private $prefetchSize = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* It's for QOS prefetch-count http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.qos |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
private $prefetchCount = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* It's for QOS global http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.qos |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
private $aGlobal = null; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* RabbitMq connection |
53
|
|
|
* @var AMQPStreamConnection |
54
|
|
|
*/ |
55
|
|
|
private $connection = null; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* RabbitMq channel |
59
|
|
|
* @var \AMQPChannel |
60
|
|
|
*/ |
61
|
|
|
private $channel = null; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* List of RabbitMq object |
65
|
|
|
* @var RabbitMqObjectInterface |
66
|
|
|
*/ |
67
|
|
|
private $rabbitMqObjects = []; |
68
|
|
|
|
69
|
|
|
public function __construct($host, $port, $user, $password) { |
70
|
|
|
$this->host = $host; |
71
|
|
|
$this->port = ($port !== null) ? $port : 5672; |
72
|
|
|
$this->user = $user; |
73
|
|
|
$this->password = $password; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get prefetch size for QOS |
78
|
|
|
*/ |
79
|
|
|
public function getPrefetchSize() { |
80
|
|
|
return $this->prefetchSize; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set prefetch size |
85
|
|
|
* It's for QOS prefetch-size http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.qos |
86
|
|
|
* |
87
|
|
|
* @param int $prefetchSize |
88
|
|
|
*/ |
89
|
|
|
public function setPrefetchSize($prefetchSize) { |
90
|
|
|
$this->prefetchSize = $prefetchSize; |
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get prefetch count for QOS |
96
|
|
|
*/ |
97
|
|
|
public function getPrefetchCount() { |
98
|
|
|
return $this->prefetchCount; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Set prefetch size |
103
|
|
|
* It's for QOS prefetch-size http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.qos |
104
|
|
|
* |
105
|
|
|
* @param int $prefetchCount |
106
|
|
|
*/ |
107
|
|
|
public function setPrefetchCount($prefetchCount) { |
108
|
|
|
$this->prefetchCount = $prefetchCount; |
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get a global for QOS |
114
|
|
|
*/ |
115
|
|
|
public function getAGlobal() { |
116
|
|
|
return $this->aGlobal; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set global |
121
|
|
|
* It's for QOS prefetch-size http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.qos |
122
|
|
|
* |
123
|
|
|
* @param int $aGlobal |
124
|
|
|
*/ |
125
|
|
|
public function setAGlobal($aGlobal) { |
126
|
|
|
$this->aGlobal = $aGlobal; |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set RabbitMq object |
132
|
|
|
* @param RabbitMqObjectInterface[] $rabbitMqObjects |
133
|
|
|
*/ |
134
|
|
|
public function setRabbitMqObjects(array $rabbitMqObjects) { |
135
|
|
|
$this->rabbitMqObjects = $rabbitMqObjects; |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Connection to the RabbitMq service with AMQPStreamConnection |
140
|
|
|
* |
141
|
|
|
* @return AMQPChannel |
142
|
|
|
*/ |
143
|
|
|
public function getChannel() { |
144
|
|
|
if(!$this->connection) { |
145
|
|
|
$this->connection = new AMQPStreamConnection($this->host, $this->port, $this->user, $this->password); |
146
|
|
|
|
147
|
|
|
$this->channel = $this->connection->channel(); |
|
|
|
|
148
|
|
|
|
149
|
|
|
if($this->prefetchCount !== null || $this->prefetchCount !== null || $this->aGlobal !== null) { |
150
|
|
|
$this->channel->basic_qos(null, 1, null); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
foreach ($this->rabbitMqObjects as $rabbitMqObject) { |
|
|
|
|
154
|
|
|
$rabbitMqObject->init($this->channel); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
return $this->channel; |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
} |
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..