1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace AmqpWorkers\Definition; |
5
|
|
|
|
6
|
|
|
use AmqpWorkers\Exception\DefinitionException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Exchange definition. Use it with `Producer::withExchange()` |
10
|
|
|
* if you need to send something to RabbitMQ into explicitly defined exchange. |
11
|
|
|
* |
12
|
|
|
* @see https://www.rabbitmq.com/tutorials/amqp-concepts.html |
13
|
|
|
* |
14
|
|
|
* @package AmqpWorkers\Definition |
15
|
|
|
* @author Alex Panshin <[email protected]> |
16
|
|
|
* @since 1.0 |
17
|
|
|
*/ |
18
|
|
|
class Exchange |
19
|
|
|
{ |
20
|
|
|
/** @var string */ |
21
|
|
|
private $name; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
private $type; |
25
|
|
|
|
26
|
|
|
/** @var bool */ |
27
|
|
|
private $passive = false; |
28
|
|
|
|
29
|
|
|
/** @var bool */ |
30
|
|
|
private $durable = false; |
31
|
|
|
|
32
|
|
|
/** @var bool */ |
33
|
|
|
private $internal = false; |
34
|
|
|
|
35
|
|
|
/** @var bool */ |
36
|
|
|
private $autoDelete = false; |
37
|
|
|
|
38
|
|
|
/** @var bool */ |
39
|
|
|
private $nowait = false; |
40
|
|
|
|
41
|
|
|
/** @var array|null */ |
42
|
|
|
private $arguments = null; |
43
|
|
|
|
44
|
|
|
/** @var int|null */ |
45
|
|
|
private $ticket = null; |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Named constructor to get more fluent interface |
50
|
|
|
* @param $name |
51
|
|
|
* @param $type |
52
|
|
|
* @return static |
53
|
|
|
*/ |
54
|
|
|
public static function factory($name, $type) |
55
|
|
|
{ |
56
|
|
|
return new static($name, $type); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Exchange constructor. |
61
|
|
|
* @param string $name Exchange name |
62
|
|
|
* @param string $type Exchange type. |
63
|
|
|
* |
64
|
|
|
* @see https://www.rabbitmq.com/tutorials/amqp-concepts.html#exchanges |
65
|
|
|
* @throws \AmqpWorkers\Exception\DefinitionException |
66
|
|
|
*/ |
67
|
|
|
public function __construct($name, $type) |
68
|
|
|
{ |
69
|
|
|
if (empty($name)) { |
70
|
|
|
throw new DefinitionException('Exchange name cannot be empty.'); |
71
|
|
|
} |
72
|
|
|
if (empty($type)) { |
73
|
|
|
throw new DefinitionException('Exchange type cannot be empty.'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->name = $name; |
77
|
|
|
$this->type = $type; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getName() |
84
|
|
|
{ |
85
|
|
|
return $this->name; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getType() |
92
|
|
|
{ |
93
|
|
|
return $this->type; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @see https://www.rabbitmq.com/amqp-0-9-1-reference.html#exchange.declare.passive |
98
|
|
|
* @default false |
99
|
|
|
* @param $passive |
100
|
|
|
* @return Exchange $this |
101
|
|
|
*/ |
102
|
|
|
public function passive($passive) |
103
|
|
|
{ |
104
|
|
|
$this->passive = $passive; |
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @see https://www.rabbitmq.com/amqp-0-9-1-reference.html#exchange.declare.durable |
110
|
|
|
* @default false |
111
|
|
|
* @param bool $durable |
112
|
|
|
* @return Exchange $this |
113
|
|
|
*/ |
114
|
|
|
public function durable($durable) |
115
|
|
|
{ |
116
|
|
|
$this->durable = $durable; |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @see https://www.rabbitmq.com/amqp-0-9-1-reference.html#exchange.declare.auto-delete |
122
|
|
|
* @default false |
123
|
|
|
* @param bool $autoDelete |
124
|
|
|
* @return Exchange $this |
125
|
|
|
*/ |
126
|
|
|
public function autoDelete($autoDelete) |
127
|
|
|
{ |
128
|
|
|
$this->autoDelete = $autoDelete; |
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @see * @see https://www.rabbitmq.com/amqp-0-9-1-reference.html#exchange.declare.internal |
134
|
|
|
* @default false |
135
|
|
|
* @param $internal |
136
|
|
|
* @return Exchange $this |
137
|
|
|
*/ |
138
|
|
|
public function internal($internal) |
139
|
|
|
{ |
140
|
|
|
$this->internal = $internal; |
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @see https://www.rabbitmq.com/amqp-0-9-1-reference.html#exchange.declare.no-wait |
146
|
|
|
* @default false |
147
|
|
|
* @param bool $nowait |
148
|
|
|
* @return Exchange $this |
149
|
|
|
*/ |
150
|
|
|
public function nowait($nowait) |
151
|
|
|
{ |
152
|
|
|
$this->nowait = $nowait; |
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @see https://www.rabbitmq.com/amqp-0-9-1-reference.html#queue.declare.arguments |
158
|
|
|
* @default null |
159
|
|
|
* @param array $arguments |
160
|
|
|
* @return $this |
161
|
|
|
*/ |
162
|
|
|
public function arguments(array $arguments) |
163
|
|
|
{ |
164
|
|
|
$this->arguments = $arguments; |
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @default null |
170
|
|
|
* @param int|null $ticket |
171
|
|
|
* @return $this |
172
|
|
|
*/ |
173
|
|
|
public function ticket($ticket) |
174
|
|
|
{ |
175
|
|
|
$this->ticket = $ticket; |
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Returns the list of parameters for exchange_declare |
181
|
|
|
* |
182
|
|
|
* @see \PhpAmqpLib\Channel\AMQPChannel::exchange_declare() |
183
|
|
|
* @return array |
184
|
|
|
*/ |
185
|
|
View Code Duplication |
public function listParams() |
|
|
|
|
186
|
|
|
{ |
187
|
|
|
return [ |
188
|
|
|
$this->passive, |
189
|
|
|
$this->durable, |
190
|
|
|
$this->autoDelete, |
191
|
|
|
$this->internal, |
192
|
|
|
$this->nowait, |
193
|
|
|
$this->arguments, |
194
|
|
|
$this->ticket |
195
|
|
|
]; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
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.