|
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 \DateTime |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $result; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $status = self::STATUS_PENDING; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $resolution = self::RESOLUTION_PENDING; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* QueuedTask constructor. |
|
67
|
|
|
* @param string $name |
|
68
|
|
|
* @param array $params |
|
69
|
|
|
* @param \DateTime $executeAt |
|
70
|
|
|
*/ |
|
71
|
|
|
public function __construct($name, array $params = null, \DateTime $executeAt = null) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->name = $name; |
|
74
|
|
|
$this->params = null === $params ? $params : []; |
|
75
|
|
|
$this->executeAt = null === $executeAt ? new \DateTime() : $executeAt; |
|
76
|
|
|
$this->created = new \DateTime(); |
|
77
|
|
|
$this->status = self::STATUS_PENDING; |
|
78
|
|
|
$this->resolution = self::RESOLUTION_PENDING; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getId() |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->id; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* {@inheritdoc} |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getName() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->name; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritdoc} |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getCreated() |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->created; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* {@inheritdoc} |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getExecuteAt() |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->executeAt; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* {@inheritdoc} |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getSchedule(): ScheduleInterface |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->schedule; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* {@inheritdoc} |
|
123
|
|
|
*/ |
|
124
|
|
|
public function setSchedule(ScheduleInterface $schedule) |
|
125
|
|
|
{ |
|
126
|
|
|
$this->schedule = $schedule; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* {@inheritdoc} |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getParams() |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->params; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* {@inheritdoc} |
|
139
|
|
|
*/ |
|
140
|
|
|
public function hasParams() |
|
141
|
|
|
{ |
|
142
|
|
|
return count($this->params) > 0; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* {@inheritdoc} |
|
147
|
|
|
*/ |
|
148
|
|
|
public function getResult() |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->result; |
|
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* {@inheritdoc} |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getResolution() |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->resolution; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* {@inheritdoc} |
|
163
|
|
|
*/ |
|
164
|
|
|
public function getFinished() |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->finished; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* {@inheritdoc} |
|
171
|
|
|
*/ |
|
172
|
|
|
public function getStatus() |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->status; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* {@inheritdoc} |
|
179
|
|
|
*/ |
|
180
|
|
|
public function getStarted() |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->started; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param \DateTime $started |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function setStarted(\DateTime $started) |
|
189
|
|
|
{ |
|
190
|
|
|
$this->started = $started; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param \DateTime $finished |
|
195
|
|
|
*/ |
|
196
|
|
|
protected function setFinished(\DateTime $finished) |
|
197
|
|
|
{ |
|
198
|
|
|
$this->finished = $finished; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @param string $status |
|
203
|
|
|
*/ |
|
204
|
|
|
protected function setStatus(string $status) |
|
205
|
|
|
{ |
|
206
|
|
|
$this->status = $status; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @param string $resolution |
|
211
|
|
|
*/ |
|
212
|
|
|
protected function setResolution(string $resolution) |
|
213
|
|
|
{ |
|
214
|
|
|
$this->resolution = $resolution; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @param \DateTime $result |
|
219
|
|
|
*/ |
|
220
|
|
|
protected function setResult(\DateTime $result) |
|
221
|
|
|
{ |
|
222
|
|
|
$this->result = $result; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @param string $resolution |
|
227
|
|
|
* @param mixed $response |
|
228
|
|
|
*/ |
|
229
|
|
|
protected function resolve($resolution, $response) |
|
230
|
|
|
{ |
|
231
|
|
|
if (!empty($response)) { |
|
232
|
|
|
$this->setResult(print_r($response, true)); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
$this->setStatus(QueuedTaskInterface::STATUS_DONE); |
|
236
|
|
|
$this->setResolution($resolution); |
|
237
|
|
|
$this->setFinished(new \DateTime()); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* {@inheritdoc} |
|
242
|
|
|
*/ |
|
243
|
|
|
public function start() |
|
244
|
|
|
{ |
|
245
|
|
|
$this->setStatus(QueuedTaskInterface::STATUS_RUNNING); |
|
246
|
|
|
$this->setStarted(new \DateTime()); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* {@inheritdoc} |
|
251
|
|
|
*/ |
|
252
|
|
|
public function success($response) |
|
253
|
|
|
{ |
|
254
|
|
|
$this->resolve(QueuedTaskInterface::RESOLUTION_SUCCESS, $response); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* {@inheritdoc} |
|
259
|
|
|
*/ |
|
260
|
|
|
public function failure($response) |
|
261
|
|
|
{ |
|
262
|
|
|
$this->resolve(QueuedTaskInterface::RESOLUTION_FAILURE, $response); |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.