Completed
Push — master ( 410c5e...621de0 )
by Emil
07:36
created

QueuedTask::getPid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
     * @param int $pid
120
     */
121
    public function setPid(int $pid)
122
    {
123
        $this->pid = $pid;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function getName()
130
    {
131
        return $this->name;
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function getCreated()
138
    {
139
        return $this->created;
140
    }
141
142
    /**
143
     * @return \DateTime
144
     */
145
    public function getUpdated()
146
    {
147
        return $this->updated;
148
    }
149
150
    /**
151
     * @param \DateTime $updated
152
     */
153
    public function setUpdated(\DateTime $updated)
154
    {
155
        $this->updated = $updated;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function getExecuteAt()
162
    {
163
        return $this->executeAt;
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function getSchedule()
170
    {
171
        return $this->schedule;
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function setSchedule(ScheduleInterface $schedule)
178
    {
179
        $this->schedule = $schedule;
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185
    public function getParams()
186
    {
187
        return $this->params;
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function hasParams()
194
    {
195
        return count($this->params) > 0;
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201
    public function getResult()
202
    {
203
        return $this->result;
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     */
209
    public function getResolution()
210
    {
211
        return $this->resolution;
212
    }
213
214
    /**
215
     * {@inheritdoc}
216
     */
217
    public function getFinished()
218
    {
219
        return $this->finished;
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     */
225
    public function getStatus()
226
    {
227
        return $this->status;
228
    }
229
230
    /**
231
     * {@inheritdoc}
232
     */
233
    public function getStarted()
234
    {
235
        return $this->started;
236
    }
237
238
    /**
239
     * @param \DateTime $started
240
     */
241
    protected function setStarted(\DateTime $started)
242
    {
243
        $this->started = $started;
244
    }
245
246
    /**
247
     * @param \DateTime $finished
248
     */
249
    protected function setFinished(\DateTime $finished)
250
    {
251
        $this->finished = $finished;
252
    }
253
254
    /**
255
     * @param string $status
256
     */
257
    protected function setStatus($status)
258
    {
259
        $this->status = $status;
260
    }
261
262
    /**
263
     * @param string $resolution
264
     */
265
    protected function setResolution($resolution)
266
    {
267
        $this->resolution = $resolution;
268
    }
269
270
    /**
271
     * @param string $result
272
     */
273
    protected function setResult($result)
274
    {
275
        $this->result = $result;
276
    }
277
278
    /**
279
     * @param string $resolution
280
     * @param mixed $response
281
     */
282
    protected function resolve($resolution, $response)
283
    {
284
        if (!empty($response)) {
285
            $this->setResult(print_r($response, true));
286
        }
287
288
        $this->setUpdated(new \DateTime());
289
        $this->setStatus(QueuedTaskInterface::STATUS_DONE);
290
        $this->setResolution($resolution);
291
        $this->setFinished(new \DateTime());
292
    }
293
294
    /**
295
     * {@inheritdoc}
296
     */
297
    public function start()
298
    {
299
        $this->setUpdated(new \DateTime());
300
        $this->setPid(posix_getpid());
301
        $this->setStatus(QueuedTaskInterface::STATUS_RUNNING);
302
        $this->setStarted(new \DateTime());
303
        $this->progress(0);
304
    }
305
306
    /**
307
     * {@inheritdoc}
308
     */
309
    public function success($response)
310
    {
311
        $this->resolve(QueuedTaskInterface::RESOLUTION_SUCCESS, $response);
312
        $this->progress(100);
313
    }
314
315
    /**
316
     * {@inheritdoc}
317
     */
318
    public function failure($response)
319
    {
320
        $this->resolve(QueuedTaskInterface::RESOLUTION_FAILURE, $response);
321
        $this->progress(100);
322
    }
323
324
    /**
325
     * {@inheritdoc}
326
     */
327
    public function progress(int $progress, ?string $info = null)
328
    {
329
        $this->progress = $progress;
330
331
        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...
332
            $this->progressInfo = $info;
333
        }
334
    }
335
}
336