|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Simple\Queue; |
|
6
|
|
|
|
|
7
|
|
|
use Simple\Queue\Serializer\BaseSerializer; |
|
8
|
|
|
use Simple\Queue\Serializer\SerializerInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Config |
|
12
|
|
|
* @package Simple\Queue |
|
13
|
|
|
*/ |
|
14
|
|
|
class Config |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var int */ |
|
17
|
|
|
private int $redeliveryTimeInSeconds = 180; |
|
18
|
|
|
|
|
19
|
|
|
/** @var int */ |
|
20
|
|
|
private int $numberOfAttemptsBeforeFailure = 5; |
|
21
|
|
|
|
|
22
|
|
|
/** @var array */ |
|
23
|
|
|
private array $jobs = []; |
|
24
|
|
|
|
|
25
|
|
|
/** @var array */ |
|
26
|
|
|
private array $processors = []; |
|
27
|
|
|
|
|
28
|
|
|
/** @var SerializerInterface|null */ |
|
29
|
|
|
private ?SerializerInterface $serializer = null; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Config constructor. |
|
33
|
|
|
*/ |
|
34
|
37 |
|
public function __construct() |
|
35
|
|
|
{ |
|
36
|
37 |
|
if ($this->serializer === null) { |
|
37
|
37 |
|
$this->serializer = new BaseSerializer(); |
|
38
|
|
|
} |
|
39
|
37 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return static |
|
43
|
|
|
*/ |
|
44
|
36 |
|
public static function getDefault(): self |
|
45
|
|
|
{ |
|
46
|
36 |
|
return new self; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return int |
|
51
|
|
|
*/ |
|
52
|
8 |
|
public function getRedeliveryTimeInSeconds(): int |
|
53
|
|
|
{ |
|
54
|
8 |
|
return $this->redeliveryTimeInSeconds; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return int |
|
59
|
|
|
*/ |
|
60
|
2 |
|
public function getNumberOfAttemptsBeforeFailure(): int |
|
61
|
|
|
{ |
|
62
|
2 |
|
return $this->numberOfAttemptsBeforeFailure; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param int $seconds |
|
67
|
|
|
* @return $this |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public function changeRedeliveryTimeInSeconds(int $seconds): self |
|
70
|
|
|
{ |
|
71
|
1 |
|
$this->redeliveryTimeInSeconds = $seconds; |
|
72
|
|
|
|
|
73
|
1 |
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param int $attempt |
|
78
|
|
|
* @return $this |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function changeNumberOfAttemptsBeforeFailure(int $attempt): self |
|
81
|
|
|
{ |
|
82
|
1 |
|
$this->numberOfAttemptsBeforeFailure = $attempt; |
|
83
|
|
|
|
|
84
|
1 |
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return SerializerInterface |
|
89
|
|
|
*/ |
|
90
|
4 |
|
public function getSerializer(): SerializerInterface |
|
91
|
|
|
{ |
|
92
|
4 |
|
return $this->serializer; |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param SerializerInterface $serializer |
|
97
|
|
|
* @return $this |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function withSerializer(SerializerInterface $serializer): self |
|
100
|
|
|
{ |
|
101
|
1 |
|
$this->serializer = $serializer; |
|
102
|
|
|
|
|
103
|
1 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param string $jobName |
|
108
|
|
|
* @param Job $job |
|
109
|
|
|
* @return $this |
|
110
|
|
|
* @throws ConfigException |
|
111
|
|
|
*/ |
|
112
|
6 |
|
public function registerJob(string $jobName, Job $job): self |
|
113
|
|
|
{ |
|
114
|
6 |
|
if (isset($this->jobs[$jobName])) { |
|
115
|
1 |
|
throw new ConfigException(sprintf('Job "%s" is already registered.', $jobName)); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
6 |
|
if (class_exists($jobName) === false && (bool)preg_match('/^[a-zA-Z0-9_.-]*$/u', $jobName) === false) { |
|
119
|
1 |
|
throw new ConfigException(sprintf('Job alias "%s" contains invalid characters.', $jobName)); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
5 |
|
$this->jobs[$jobName] = $job; |
|
123
|
|
|
|
|
124
|
5 |
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return array |
|
129
|
|
|
*/ |
|
130
|
2 |
|
public function getJobs(): array |
|
131
|
|
|
{ |
|
132
|
2 |
|
return $this->jobs; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param string $jobName |
|
137
|
|
|
* @return Job |
|
138
|
|
|
* @throws QueueException |
|
139
|
|
|
*/ |
|
140
|
4 |
|
public function getJob(string $jobName): Job |
|
141
|
|
|
{ |
|
142
|
4 |
|
if ($this->hasJob($jobName) === false) { |
|
143
|
2 |
|
throw new QueueException(sprintf('Job "%s" not registered.', $jobName)); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
2 |
|
if (class_exists($jobName)) { |
|
147
|
1 |
|
foreach ($this->jobs as $jobAlias => $job) { |
|
148
|
1 |
|
if (is_a($job, $jobName)) { |
|
149
|
1 |
|
return $job; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
2 |
|
return $this->jobs[$jobName]; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param string $jobName |
|
159
|
|
|
* @return bool |
|
160
|
|
|
*/ |
|
161
|
5 |
|
public function hasJob(string $jobName): bool |
|
162
|
|
|
{ |
|
163
|
5 |
|
if (class_exists($jobName)) { |
|
164
|
1 |
|
foreach ($this->jobs as $jobAlias => $job) { |
|
165
|
1 |
|
if (is_a($job, $jobName)) { |
|
166
|
1 |
|
return true; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
5 |
|
return isset($this->jobs[$jobName]); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @param string $jobName |
|
176
|
|
|
* @return string |
|
177
|
|
|
* @throws ConfigException |
|
178
|
|
|
*/ |
|
179
|
3 |
|
public function getJobAlias(string $jobName): string |
|
180
|
|
|
{ |
|
181
|
3 |
|
if (isset($this->jobs[$jobName])) { |
|
182
|
2 |
|
return $jobName; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
2 |
|
foreach ($this->jobs as $jobAlias => $job) { |
|
186
|
1 |
|
if (is_a($job, $jobName)) { |
|
187
|
1 |
|
return $jobAlias; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
1 |
|
throw new ConfigException(sprintf('Job "%s" not registered.', $jobName)); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @param string $queue |
|
196
|
|
|
* @param callable $processor |
|
197
|
|
|
* @return $this |
|
198
|
|
|
* @throws ConfigException |
|
199
|
|
|
*/ |
|
200
|
5 |
|
public function registerProcessor(string $queue, callable $processor): self |
|
201
|
|
|
{ |
|
202
|
5 |
|
if ($this->hasProcessor($queue)) { |
|
203
|
1 |
|
throw new ConfigException(sprintf('Processor "%s" is already registered.', $queue)); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
5 |
|
$this->processors[$queue] = $processor; |
|
207
|
|
|
|
|
208
|
5 |
|
return $this; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @return array |
|
213
|
|
|
*/ |
|
214
|
2 |
|
public function getProcessors(): array |
|
215
|
|
|
{ |
|
216
|
2 |
|
return $this->processors; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @param string $queue |
|
221
|
|
|
* @return callable |
|
222
|
|
|
* @throws ConfigException |
|
223
|
|
|
*/ |
|
224
|
2 |
|
public function getProcessor(string $queue): callable |
|
225
|
|
|
{ |
|
226
|
2 |
|
if ($this->hasProcessor($queue) === false) { |
|
227
|
1 |
|
throw new ConfigException(sprintf('Processor "%s" not registered.', $queue)); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
1 |
|
return $this->processors[$queue]; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @param string $queue |
|
235
|
|
|
* @return bool |
|
236
|
|
|
*/ |
|
237
|
7 |
|
public function hasProcessor(string $queue): bool |
|
238
|
|
|
{ |
|
239
|
7 |
|
return isset($this->processors[$queue]); |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|