1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JobQueue\Domain\Task; |
4
|
|
|
|
5
|
|
|
use JobQueue\Domain\Job\ExecutableJob; |
6
|
|
|
|
7
|
|
|
final class Task implements \Serializable, \JsonSerializable |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private $identifier; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* |
17
|
|
|
* @var Status |
18
|
|
|
*/ |
19
|
|
|
private $status; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* |
23
|
|
|
* @var Profile |
24
|
|
|
*/ |
25
|
|
|
private $profile; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* |
29
|
|
|
* @var ExecutableJob |
30
|
|
|
*/ |
31
|
|
|
private $job; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
private $createdAt; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
|
41
|
|
|
* @var ParameterBag |
42
|
|
|
*/ |
43
|
|
|
private $parameters; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
|
47
|
|
|
* @var TagBag |
48
|
|
|
*/ |
49
|
|
|
private $tags; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* |
53
|
|
|
* @param Profile $profile |
54
|
|
|
* @param ExecutableJob $job |
55
|
|
|
* @param array $parameters |
56
|
|
|
* @param array $tags |
57
|
|
|
*/ |
58
|
13 |
|
public function __construct(Profile $profile, ExecutableJob $job, array $parameters = [], array $tags = []) |
59
|
|
|
{ |
60
|
13 |
|
$this->identifier = new Identifier; |
61
|
13 |
|
$this->status = new Status(Status::WAITING); |
62
|
13 |
|
$this->profile = $profile; |
63
|
13 |
|
$this->job = $job; |
64
|
13 |
|
$this->createdAt = time(); |
65
|
13 |
|
$this->parameters = new ParameterBag($parameters); |
66
|
11 |
|
$this->tags = new TagBag($tags); |
67
|
10 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
19 |
|
public function getIdentifier(): string |
74
|
|
|
{ |
75
|
19 |
|
return $this->identifier; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* |
80
|
|
|
* @return Status |
81
|
|
|
*/ |
82
|
19 |
|
public function getStatus(): Status |
83
|
|
|
{ |
84
|
19 |
|
return $this->status; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* |
89
|
|
|
* @param Status $status |
90
|
|
|
*/ |
91
|
9 |
|
public function updateStatus(Status $status) |
92
|
|
|
{ |
93
|
9 |
|
$this->status = $status; |
94
|
9 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* |
98
|
|
|
* @return Profile |
99
|
|
|
*/ |
100
|
18 |
|
public function getProfile(): Profile |
101
|
|
|
{ |
102
|
18 |
|
return $this->profile; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* |
107
|
|
|
* @return ExecutableJob |
108
|
|
|
*/ |
109
|
4 |
|
public function getJob(): ExecutableJob |
110
|
|
|
{ |
111
|
4 |
|
return $this->job; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* |
116
|
|
|
* @param bool $humanReadable |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
7 |
|
public function getJobName(bool $humanReadable = false): string |
120
|
|
|
{ |
121
|
7 |
|
$jobName = get_class($this->job); |
122
|
|
|
|
123
|
7 |
|
if ($humanReadable) { |
124
|
7 |
|
$name = explode('\\', $jobName); |
125
|
7 |
|
$name = array_pop($name); |
126
|
|
|
|
127
|
|
|
// Convert CamelCase to snake_case |
128
|
7 |
|
preg_match_all('/([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)/', $name, $matches); |
129
|
7 |
|
foreach ($matches[0] as &$match) { |
130
|
7 |
|
$match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match); |
131
|
|
|
} |
132
|
|
|
|
133
|
7 |
|
if ('job' !== $lmatch = array_pop($matches[0])) { |
134
|
|
|
$matches[0] = $lmatch; |
135
|
|
|
} |
136
|
|
|
|
137
|
7 |
|
return implode('_', $matches[0]); |
138
|
|
|
} |
139
|
|
|
|
140
|
3 |
|
return $jobName; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* |
145
|
|
|
* @param string|null $format |
146
|
|
|
* @return mixed |
147
|
|
|
*/ |
148
|
14 |
|
public function getCreatedAt(string $format = null) |
149
|
|
|
{ |
150
|
14 |
|
return $format |
151
|
10 |
|
? date($format, $this->createdAt) |
152
|
14 |
|
: $this->createdAt; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* |
157
|
|
|
* @param string $name |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
1 |
|
public function hasParameter(string $name): bool |
161
|
|
|
{ |
162
|
1 |
|
return $this->parameters->has($name); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* |
167
|
|
|
* @return array |
168
|
|
|
*/ |
169
|
3 |
|
public function getParameters(): array |
170
|
|
|
{ |
171
|
3 |
|
return $this->parameters->__toArray(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* |
176
|
|
|
* @param string $name |
177
|
|
|
* @return mixed |
178
|
|
|
*/ |
179
|
1 |
|
public function getParameter(string $name) |
180
|
|
|
{ |
181
|
1 |
|
return $this->parameters->get($name); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* |
186
|
|
|
* @param string $name |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
3 |
|
public function hasTag(string $name): bool |
190
|
|
|
{ |
191
|
3 |
|
return $this->tags->has($name); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* |
196
|
|
|
* @return array |
197
|
|
|
*/ |
198
|
5 |
|
public function getTags(): array |
199
|
|
|
{ |
200
|
5 |
|
return $this->tags->__toArray(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
15 |
|
public function serialize(): string |
208
|
|
|
{ |
209
|
15 |
|
return serialize([ |
210
|
15 |
|
(string) $this->identifier, |
211
|
15 |
|
(string) $this->status, |
212
|
15 |
|
(string) $this->profile, |
213
|
15 |
|
get_class($this->job), |
214
|
15 |
|
$this->createdAt, |
215
|
15 |
|
$this->parameters->__toArray(), |
216
|
15 |
|
$this->tags->__toArray(), |
217
|
|
|
]); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* |
222
|
|
|
* @param string $serialized |
223
|
|
|
*/ |
224
|
20 |
|
public function unserialize($serialized) |
225
|
|
|
{ |
226
|
20 |
|
$array = unserialize($serialized); |
227
|
|
|
|
228
|
20 |
|
$this->identifier = new Identifier($array[0]); |
229
|
20 |
|
$this->status = new Status($array[1]); |
230
|
20 |
|
$this->profile = new Profile($array[2]); |
231
|
20 |
|
$this->job = new $array[3]; |
232
|
20 |
|
$this->createdAt = $array[4]; |
233
|
20 |
|
$this->parameters = new ParameterBag($array[5]); |
234
|
20 |
|
$this->tags = new TagBag($array[6]); |
235
|
20 |
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* |
239
|
|
|
* @return array |
240
|
|
|
*/ |
241
|
4 |
|
public function jsonSerialize(): array |
242
|
|
|
{ |
243
|
|
|
return [ |
244
|
4 |
|
'identifier' => (string) $this->identifier, |
245
|
4 |
|
'status' => (string) $this->status, |
246
|
4 |
|
'profile' => (string) $this->profile, |
247
|
4 |
|
'job' => get_class($this->job), |
248
|
4 |
|
'date' => $this->getCreatedAt('r'), |
249
|
4 |
|
'parameters' => $this->parameters->__toArray(), |
250
|
4 |
|
'tags' => $this->tags->__toArray(), |
251
|
|
|
]; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* |
256
|
|
|
* @return string |
257
|
|
|
*/ |
258
|
7 |
|
public function __toString(): string |
259
|
|
|
{ |
260
|
7 |
|
return $this->identifier; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Try to free memory |
265
|
|
|
* |
266
|
|
|
*/ |
267
|
27 |
|
public function __destruct() |
268
|
|
|
{ |
269
|
|
|
unset( |
270
|
27 |
|
$this->identifier, |
271
|
27 |
|
$this->status, |
272
|
27 |
|
$this->profile, |
273
|
27 |
|
$this->job, |
274
|
27 |
|
$this->createdAt, |
275
|
27 |
|
$this->parameters, |
276
|
27 |
|
$this->tags |
277
|
|
|
); |
278
|
27 |
|
} |
279
|
|
|
} |
280
|
|
|
|