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