1
|
|
|
<?php |
2
|
|
|
namespace NeedleProject\LaravelRabbitMq\Entity; |
3
|
|
|
|
4
|
|
|
use NeedleProject\LaravelRabbitMq\AMQPConnection; |
5
|
|
|
use NeedleProject\LaravelRabbitMq\PublisherInterface; |
6
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
7
|
|
|
use PhpAmqpLib\Exception\AMQPProtocolChannelException; |
8
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ExchangeEntity |
12
|
|
|
* |
13
|
|
|
* @package NeedleProject\LaravelRabbitMq\Entity |
14
|
|
|
* @author Adrian Tilita <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class ExchangeEntity implements PublisherInterface, AMQPEntityInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @const array Default connections parameters |
20
|
|
|
*/ |
21
|
|
|
const DEFAULTS = [ |
22
|
|
|
'exchange_type' => 'topic', |
23
|
|
|
'passive' => false, |
24
|
|
|
'durable' => false, |
25
|
|
|
'auto_delete' => false, |
26
|
|
|
'internal' => false, |
27
|
|
|
'nowait' => false, |
28
|
|
|
'auto_create' => false, |
29
|
|
|
'throw_exception_on_redeclare' => true, |
30
|
|
|
'throw_exception_on_bind_fail' => true, |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var AMQPConnection |
35
|
|
|
*/ |
36
|
|
|
protected $connection; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $aliasName; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $attributes; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* ExchangeEntity constructor. |
50
|
|
|
* |
51
|
|
|
* @param AMQPConnection $connection |
52
|
|
|
* @param string $aliasName |
53
|
|
|
* @param array $attributes |
54
|
|
|
*/ |
55
|
13 |
|
public function __construct(AMQPConnection $connection, string $aliasName, array $attributes = []) |
56
|
|
|
{ |
57
|
13 |
|
$this->connection = $connection; |
58
|
13 |
|
$this->aliasName = $aliasName; |
59
|
13 |
|
$this->attributes = $attributes; |
60
|
13 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param AMQPConnection $connection |
64
|
|
|
* @param string $aliasName |
65
|
|
|
* @param array $exchangeDetails |
66
|
|
|
* @return ExchangeEntity |
67
|
|
|
*/ |
68
|
13 |
|
public static function createExchange(AMQPConnection $connection, string $aliasName, array $exchangeDetails) |
69
|
|
|
{ |
70
|
13 |
|
return new static( |
71
|
|
|
$connection, |
72
|
|
|
$aliasName, |
73
|
13 |
|
array_merge(self::DEFAULTS, $exchangeDetails) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
2 |
|
public function getAliasName(): string |
81
|
|
|
{ |
82
|
2 |
|
return $this->aliasName; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return AMQPConnection |
87
|
|
|
*/ |
88
|
8 |
|
protected function getConnection(): AMQPConnection |
89
|
|
|
{ |
90
|
8 |
|
return $this->connection; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return AMQPChannel |
95
|
|
|
*/ |
96
|
8 |
|
protected function getChannel(): AMQPChannel |
97
|
|
|
{ |
98
|
8 |
|
return $this->getConnection()->getChannel(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Create the Queue |
103
|
|
|
*/ |
104
|
4 |
View Code Duplication |
public function create() |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
try { |
107
|
4 |
|
$this->getChannel() |
108
|
4 |
|
->exchange_declare( |
109
|
4 |
|
$this->attributes['name'], |
110
|
4 |
|
$this->attributes['exchange_type'], |
111
|
4 |
|
$this->attributes['passive'], |
112
|
4 |
|
$this->attributes['durable'], |
113
|
4 |
|
$this->attributes['auto_delete'], |
114
|
4 |
|
$this->attributes['internal'], |
115
|
4 |
|
$this->attributes['nowait'] |
116
|
|
|
); |
117
|
2 |
|
} catch (AMQPProtocolChannelException $e) { |
118
|
|
|
// 406 is a soft error triggered for precondition failure (when redeclaring with different parameters) |
119
|
2 |
|
if (true === $this->attributes['throw_exception_on_redeclare'] || $e->amqp_reply_code !== 406) { |
|
|
|
|
120
|
1 |
|
throw $e; |
121
|
|
|
} |
122
|
|
|
// a failure trigger channels closing process |
123
|
1 |
|
$this->getConnection()->reconnect(); |
124
|
|
|
} |
125
|
3 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @throws AMQPProtocolChannelException |
129
|
|
|
*/ |
130
|
3 |
View Code Duplication |
public function bind() |
|
|
|
|
131
|
|
|
{ |
132
|
3 |
|
if (!isset($this->attributes['bind']) || empty($this->attributes['bind'])) { |
133
|
1 |
|
return; |
134
|
|
|
} |
135
|
2 |
|
foreach ($this->attributes['bind'] as $bindItem) { |
136
|
|
|
try { |
137
|
2 |
|
$this->getChannel() |
138
|
2 |
|
->queue_bind( |
139
|
2 |
|
$bindItem['queue'], |
140
|
2 |
|
$this->attributes['name'], |
141
|
2 |
|
$bindItem['routing_key'] |
142
|
|
|
); |
143
|
|
|
} catch (AMQPProtocolChannelException $e) { |
144
|
|
|
// 404 is the code for trying to bind to an non-existing entity |
145
|
|
|
if (true === $this->attributes['throw_exception_on_bind_fail'] || $e->amqp_reply_code !== 404) { |
|
|
|
|
146
|
|
|
throw $e; |
147
|
|
|
} |
148
|
2 |
|
$this->getConnection()->reconnect(); |
149
|
|
|
} |
150
|
|
|
} |
151
|
2 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Delete the queue |
155
|
|
|
*/ |
156
|
1 |
|
public function delete() |
157
|
|
|
{ |
158
|
1 |
|
$this->getChannel()->exchange_delete($this->attributes['name']); |
159
|
1 |
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Publish a message |
163
|
|
|
* |
164
|
|
|
* @param string $message |
165
|
|
|
* @param string $routingKey |
166
|
|
|
* @return mixed|void |
167
|
|
|
* @throws AMQPProtocolChannelException |
168
|
|
|
*/ |
169
|
3 |
View Code Duplication |
public function publish(string $message, string $routingKey = '') |
|
|
|
|
170
|
|
|
{ |
171
|
3 |
|
if ($this->attributes['auto_create'] === true) { |
172
|
1 |
|
$this->create(); |
173
|
1 |
|
$this->bind(); |
174
|
|
|
} |
175
|
3 |
|
$this->getChannel()->basic_publish( |
176
|
3 |
|
new AMQPMessage($message), |
177
|
3 |
|
$this->attributes['name'], |
178
|
|
|
$routingKey, |
179
|
3 |
|
true |
180
|
|
|
); |
181
|
3 |
|
} |
182
|
|
|
} |
183
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.