|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\AMQP\Model\Queue; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\AMQP\Exception\{ |
|
7
|
|
|
ExclusivePassiveDeclarationNotAllowed, |
|
8
|
|
|
NotWaitingPassiveDeclarationDoesNothing, |
|
9
|
|
|
PassiveQueueDeclarationMustHaveAName, |
|
10
|
|
|
}; |
|
11
|
|
|
use Innmind\Immutable\{ |
|
12
|
|
|
MapInterface, |
|
13
|
|
|
Map, |
|
14
|
|
|
}; |
|
15
|
|
|
|
|
16
|
|
|
final class Declaration |
|
17
|
|
|
{ |
|
18
|
|
|
private $name; |
|
19
|
|
|
private $passive = false; |
|
20
|
|
|
private $durable = false; |
|
21
|
|
|
private $autoDelete = false; |
|
22
|
|
|
private $exclusive = false; |
|
23
|
|
|
private $wait = true; |
|
24
|
|
|
private $arguments; |
|
25
|
|
|
|
|
26
|
84 |
|
private function __construct() |
|
27
|
|
|
{ |
|
28
|
84 |
|
$this->arguments = new Map('string', 'mixed'); |
|
29
|
84 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Check if the queue exists on the server |
|
33
|
|
|
*/ |
|
34
|
12 |
|
public static function passive(string $name): self |
|
35
|
|
|
{ |
|
36
|
12 |
|
$self = new self; |
|
37
|
12 |
|
$self->passive = true; |
|
38
|
|
|
|
|
39
|
12 |
|
return $self->withName($name); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The queue will survive after a server restart |
|
44
|
|
|
*/ |
|
45
|
32 |
|
public static function durable(): self |
|
46
|
|
|
{ |
|
47
|
32 |
|
$self = new self; |
|
48
|
32 |
|
$self->durable = true; |
|
49
|
|
|
|
|
50
|
32 |
|
return $self; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* The queue will disappear after a server restart |
|
55
|
|
|
*/ |
|
56
|
42 |
|
public static function temporary(): self |
|
57
|
|
|
{ |
|
58
|
42 |
|
return new self; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* The queue is deleted once all consumers have finished using it |
|
63
|
|
|
*/ |
|
64
|
4 |
|
public static function autoDelete(): self |
|
65
|
|
|
{ |
|
66
|
4 |
|
$self = new self; |
|
67
|
4 |
|
$self->autoDelete = true; |
|
68
|
|
|
|
|
69
|
4 |
|
return $self; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Make the queue only accessible to the current connection |
|
74
|
|
|
*/ |
|
75
|
44 |
|
public function exclusive(): self |
|
76
|
|
|
{ |
|
77
|
44 |
|
if ($this->isPassive()) { |
|
78
|
2 |
|
throw new ExclusivePassiveDeclarationNotAllowed; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
42 |
|
$self = clone $this; |
|
82
|
42 |
|
$self->exclusive = true; |
|
83
|
|
|
|
|
84
|
42 |
|
return $self; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Make the queue usable to all connections |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function notExclusive(): self |
|
91
|
|
|
{ |
|
92
|
2 |
|
$self = clone $this; |
|
93
|
2 |
|
$self->exclusive = false; |
|
94
|
|
|
|
|
95
|
2 |
|
return $self; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Don't wait for the server response |
|
100
|
|
|
*/ |
|
101
|
8 |
|
public function dontWait(): self |
|
102
|
|
|
{ |
|
103
|
8 |
|
if ($this->isPassive()) { |
|
104
|
2 |
|
throw new NotWaitingPassiveDeclarationDoesNothing; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
6 |
|
$self = clone $this; |
|
108
|
6 |
|
$self->wait = false; |
|
109
|
|
|
|
|
110
|
6 |
|
return $self; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Wait for the server response |
|
115
|
|
|
*/ |
|
116
|
2 |
|
public function wait(): self |
|
117
|
|
|
{ |
|
118
|
2 |
|
$self = clone $this; |
|
119
|
2 |
|
$self->wait = true; |
|
120
|
|
|
|
|
121
|
2 |
|
return $self; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Name the queue with the given string |
|
126
|
|
|
*/ |
|
127
|
54 |
|
public function withName(string $name): self |
|
128
|
|
|
{ |
|
129
|
54 |
|
$self = clone $this; |
|
130
|
54 |
|
$self->name = $name; |
|
131
|
|
|
|
|
132
|
54 |
|
return $self; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Let the server generate a name for the queue |
|
137
|
|
|
*/ |
|
138
|
4 |
|
public function withAutoGeneratedName(): self |
|
139
|
|
|
{ |
|
140
|
4 |
|
if ($this->isPassive()) { |
|
141
|
2 |
|
throw new PassiveQueueDeclarationMustHaveAName; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
2 |
|
$self = clone $this; |
|
145
|
2 |
|
$self->name = null; |
|
146
|
|
|
|
|
147
|
2 |
|
return $self; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
4 |
|
public function withArgument(string $key, $value): self |
|
151
|
|
|
{ |
|
152
|
4 |
|
$self = clone $this; |
|
153
|
4 |
|
$self->arguments = $self->arguments->put($key, $value); |
|
154
|
|
|
|
|
155
|
4 |
|
return $self; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
62 |
|
public function shouldAutoGenerateName(): bool |
|
159
|
|
|
{ |
|
160
|
62 |
|
return !\is_string($this->name); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
42 |
|
public function name(): string |
|
164
|
|
|
{ |
|
165
|
42 |
|
return $this->name; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
70 |
|
public function isPassive(): bool |
|
169
|
|
|
{ |
|
170
|
70 |
|
return $this->passive; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
58 |
|
public function isDurable(): bool |
|
174
|
|
|
{ |
|
175
|
58 |
|
return $this->durable; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
58 |
|
public function isAutoDeleted(): bool |
|
179
|
|
|
{ |
|
180
|
58 |
|
return $this->autoDelete; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
62 |
|
public function isExclusive(): bool |
|
184
|
|
|
{ |
|
185
|
62 |
|
return $this->exclusive; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
62 |
|
public function shouldWait(): bool |
|
189
|
|
|
{ |
|
190
|
62 |
|
return $this->wait; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @return MapInterface<string, mixed> |
|
195
|
|
|
*/ |
|
196
|
60 |
|
public function arguments(): MapInterface |
|
197
|
|
|
{ |
|
198
|
60 |
|
return $this->arguments; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|