1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Glooby\TaskBundle\Model; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @author Emil Kilhage |
7
|
|
|
*/ |
8
|
|
|
class QueuedTask implements QueuedTaskInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var int |
12
|
|
|
*/ |
13
|
|
|
protected $id; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $name; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \DateTime |
22
|
|
|
*/ |
23
|
|
|
protected $created; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $params = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ScheduleInterface |
32
|
|
|
*/ |
33
|
|
|
protected $schedule; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var \DateTime |
37
|
|
|
*/ |
38
|
|
|
protected $executeAt; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var \DateTime |
42
|
|
|
*/ |
43
|
|
|
protected $started; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var \DateTime |
47
|
|
|
*/ |
48
|
|
|
protected $finished; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $result; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var int |
57
|
|
|
*/ |
58
|
|
|
protected $progress; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $progressInfo; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
protected $status = self::STATUS_QUEUED; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
protected $resolution = self::RESOLUTION_QUEUED; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* QueuedTask constructor. |
77
|
|
|
* @param string $name |
78
|
|
|
* @param array $params |
79
|
|
|
* @param \DateTime $executeAt |
80
|
|
|
*/ |
81
|
|
|
public function __construct($name, array $params = null, \DateTime $executeAt = null) |
82
|
|
|
{ |
83
|
|
|
$this->name = $name; |
84
|
|
|
$this->params = null === $params ? $params : []; |
85
|
|
|
$this->executeAt = null === $executeAt ? new \DateTime() : $executeAt; |
86
|
|
|
$this->created = new \DateTime(); |
87
|
|
|
$this->status = self::STATUS_QUEUED; |
88
|
|
|
$this->resolution = self::RESOLUTION_QUEUED; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function getId() |
95
|
|
|
{ |
96
|
|
|
return $this->id; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
|
|
public function getName() |
103
|
|
|
{ |
104
|
|
|
return $this->name; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
|
public function getCreated() |
111
|
|
|
{ |
112
|
|
|
return $this->created; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
public function getExecuteAt() |
119
|
|
|
{ |
120
|
|
|
return $this->executeAt; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
public function getSchedule() |
127
|
|
|
{ |
128
|
|
|
return $this->schedule; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
|
public function setSchedule(ScheduleInterface $schedule) |
135
|
|
|
{ |
136
|
|
|
$this->schedule = $schedule; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
|
|
public function getParams() |
143
|
|
|
{ |
144
|
|
|
return $this->params; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
|
|
public function hasParams() |
151
|
|
|
{ |
152
|
|
|
return count($this->params) > 0; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
|
|
public function getResult() |
159
|
|
|
{ |
160
|
|
|
return $this->result; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
|
|
public function getResolution() |
167
|
|
|
{ |
168
|
|
|
return $this->resolution; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* {@inheritdoc} |
173
|
|
|
*/ |
174
|
|
|
public function getFinished() |
175
|
|
|
{ |
176
|
|
|
return $this->finished; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritdoc} |
181
|
|
|
*/ |
182
|
|
|
public function getStatus() |
183
|
|
|
{ |
184
|
|
|
return $this->status; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* {@inheritdoc} |
189
|
|
|
*/ |
190
|
|
|
public function getStarted() |
191
|
|
|
{ |
192
|
|
|
return $this->started; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param \DateTime $started |
197
|
|
|
*/ |
198
|
|
|
protected function setStarted(\DateTime $started) |
199
|
|
|
{ |
200
|
|
|
$this->started = $started; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param \DateTime $finished |
205
|
|
|
*/ |
206
|
|
|
protected function setFinished(\DateTime $finished) |
207
|
|
|
{ |
208
|
|
|
$this->finished = $finished; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param string $status |
213
|
|
|
*/ |
214
|
|
|
protected function setStatus($status) |
215
|
|
|
{ |
216
|
|
|
$this->status = $status; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param string $resolution |
221
|
|
|
*/ |
222
|
|
|
protected function setResolution($resolution) |
223
|
|
|
{ |
224
|
|
|
$this->resolution = $resolution; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param string $result |
229
|
|
|
*/ |
230
|
|
|
protected function setResult($result) |
231
|
|
|
{ |
232
|
|
|
$this->result = $result; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param string $resolution |
237
|
|
|
* @param mixed $response |
238
|
|
|
*/ |
239
|
|
|
protected function resolve($resolution, $response) |
240
|
|
|
{ |
241
|
|
|
if (!empty($response)) { |
242
|
|
|
$this->setResult(print_r($response, true)); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
$this->setStatus(QueuedTaskInterface::STATUS_DONE); |
246
|
|
|
$this->setResolution($resolution); |
247
|
|
|
$this->setFinished(new \DateTime()); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* {@inheritdoc} |
252
|
|
|
*/ |
253
|
|
|
public function start() |
254
|
|
|
{ |
255
|
|
|
$this->setStatus(QueuedTaskInterface::STATUS_RUNNING); |
256
|
|
|
$this->setStarted(new \DateTime()); |
257
|
|
|
$this->progress(0); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* {@inheritdoc} |
262
|
|
|
*/ |
263
|
|
|
public function success($response) |
264
|
|
|
{ |
265
|
|
|
$this->resolve(QueuedTaskInterface::RESOLUTION_SUCCESS, $response); |
266
|
|
|
$this->progress(100); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* {@inheritdoc} |
271
|
|
|
*/ |
272
|
|
|
public function failure($response) |
273
|
|
|
{ |
274
|
|
|
$this->resolve(QueuedTaskInterface::RESOLUTION_FAILURE, $response); |
275
|
|
|
$this->progress(100); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* {@inheritdoc} |
280
|
|
|
*/ |
281
|
|
|
public function progress(int $progress, ?string $info = null) |
282
|
|
|
{ |
283
|
|
|
$this->progress = $progress; |
284
|
|
|
|
285
|
|
|
if ($info) { |
|
|
|
|
286
|
|
|
$this->progressInfo = $info; |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: