QueuedTask::failure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 int
17
     */
18
    protected $pid;
19
20
    /**
21
     * @var string
22
     */
23
    protected $name;
24
25
    /**
26
     * @var \DateTime
27
     */
28
    protected $created;
29
30
    /**
31
     * @var \DateTime
32
     */
33
    protected $updated;
34
35
    /**
36
     * @var array
37
     */
38
    protected $params = [];
39
40
    /**
41
     * @var ScheduleInterface
42
     */
43
    protected $schedule;
44
45
    /**
46
     * @var \DateTime
47
     */
48
    protected $executeAt;
49
50
    /**
51
     * @var \DateTime
52
     */
53
    protected $started;
54
55
    /**
56
     * @var \DateTime
57
     */
58
    protected $finished;
59
60
    /**
61
     * @var string
62
     */
63
    protected $result;
64
65
    /**
66
     * @var int
67
     */
68
    protected $progress;
69
70
    /**
71
     * @var string
72
     */
73
    protected $progressInfo;
74
75
    /**
76
     * @var string
77
     */
78
    protected $status = self::STATUS_QUEUED;
79
80
    /**
81
     * @var string
82
     */
83
    protected $resolution = self::RESOLUTION_QUEUED;
84
85
    /**
86
     * QueuedTask constructor.
87
     * @param string $name
88
     * @param array $params
89
     * @param \DateTime $executeAt
90
     */
91
    public function __construct($name, array $params = null, \DateTime $executeAt = null)
92
    {
93
        $this->name = $name;
94
        $this->params = null === $params ? $params : [];
95
        $this->executeAt = null === $executeAt ? new \DateTime() : $executeAt;
96
        $this->created = new \DateTime();
97
        $this->updated = new \DateTime();
98
        $this->status = self::STATUS_QUEUED;
99
        $this->resolution = self::RESOLUTION_QUEUED;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function getId()
106
    {
107
        return $this->id;
108
    }
109
110
    /**
111
     * @return int
112
     */
113
    public function getPid()
114
    {
115
        return $this->pid;
116
    }
117
118
    /**
119
     * @return boolean
120
     */
121
    public function hasPId()
122
    {
123
        return null !== $this->pid;
124
    }
125
126
    /**
127
     * @param int $pid
128
     */
129
    public function setPid(int $pid)
130
    {
131
        $this->pid = $pid;
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function getName()
138
    {
139
        return $this->name;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function getCreated()
146
    {
147
        return $this->created;
148
    }
149
150
    /**
151
     * @return \DateTime
152
     */
153
    public function getUpdated()
154
    {
155
        return $this->updated;
156
    }
157
158
    /**
159
     * @param \DateTime $updated
160
     */
161
    public function setUpdated(\DateTime $updated)
162
    {
163
        $this->updated = $updated;
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function getExecuteAt()
170
    {
171
        return $this->executeAt;
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function getSchedule()
178
    {
179
        return $this->schedule;
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185
    public function setSchedule(ScheduleInterface $schedule)
186
    {
187
        $this->schedule = $schedule;
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function getParams()
194
    {
195
        return $this->params;
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201
    public function hasParams()
202
    {
203
        return count($this->params) > 0;
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     */
209
    public function getResult()
210
    {
211
        return $this->result;
212
    }
213
214
    /**
215
     * {@inheritdoc}
216
     */
217
    public function getResolution()
218
    {
219
        return $this->resolution;
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     */
225
    public function getFinished()
226
    {
227
        return $this->finished;
228
    }
229
230
    /**
231
     * {@inheritdoc}
232
     */
233
    public function getStatus()
234
    {
235
        return $this->status;
236
    }
237
238
    /**
239
     * {@inheritdoc}
240
     */
241
    public function getStarted()
242
    {
243
        return $this->started;
244
    }
245
246
    /**
247
     * @param \DateTime $started
248
     */
249
    protected function setStarted(\DateTime $started)
250
    {
251
        $this->started = $started;
252
    }
253
254
    /**
255
     * @param \DateTime $finished
256
     */
257
    protected function setFinished(\DateTime $finished)
258
    {
259
        $this->finished = $finished;
260
    }
261
262
    /**
263
     * @param string $status
264
     */
265
    protected function setStatus($status)
266
    {
267
        $this->status = $status;
268
    }
269
270
    /**
271
     * @param string $resolution
272
     */
273
    protected function setResolution($resolution)
274
    {
275
        $this->resolution = $resolution;
276
    }
277
278
    /**
279
     * @param string $result
280
     */
281
    protected function setResult($result)
282
    {
283
        $this->result = $result;
284
    }
285
286
    /**
287
     * @param string $resolution
288
     * @param mixed $response
289
     */
290
    protected function resolve($resolution, $response)
291
    {
292
        if (!empty($response)) {
293
            $this->setResult(print_r($response, true));
294
        }
295
296
        $this->setUpdated(new \DateTime());
297
        $this->setStatus(QueuedTaskInterface::STATUS_DONE);
298
        $this->setResolution($resolution);
299
        $this->setFinished(new \DateTime());
300
    }
301
302
    /**
303
     * {@inheritdoc}
304
     */
305
    public function start()
306
    {
307
        $this->setUpdated(new \DateTime());
308
        $this->setPid(posix_getpid());
309
        $this->setStatus(QueuedTaskInterface::STATUS_RUNNING);
310
        $this->setStarted(new \DateTime());
311
        $this->progress(0);
312
    }
313
314
    /**
315
     * {@inheritdoc}
316
     */
317
    public function success($response)
318
    {
319
        $this->resolve(QueuedTaskInterface::RESOLUTION_SUCCESS, $response);
320
        $this->progress(100);
321
    }
322
323
    /**
324
     * {@inheritdoc}
325
     */
326
    public function failure($response)
327
    {
328
        $this->resolve(QueuedTaskInterface::RESOLUTION_FAILURE, $response);
329
        $this->progress(100);
330
    }
331
332
    /**
333
     * {@inheritdoc}
334
     */
335
    public function progress(int $progress, ?string $info = null)
336
    {
337
        $this->progress = $progress;
338
339
        if ($info) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $info of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch 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:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
340
            $this->progressInfo = $info;
341
        }
342
    }
343
}
344