Test Setup Failed
Pull Request — master (#7)
by Matthew
14:04
created

Job::getWhen()   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 Dtc\QueueBundle\Model;
4
5
class Job
6
{
7
    const STATUS_SUCCESS = 'success';
8
    const STATUS_ERROR = 'error';
9
    const STATUS_NEW = 'new';
10
11
    protected $id;
12
    protected $workerName;
13
    protected $className;
14
    protected $method;
15
    protected $args;
16
    protected $batch;
17
    protected $status;
18
    protected $message;
19
    protected $priority;
20
    protected $crcHash;
21
    protected $locked;
22
    protected $lockedAt;
23
    protected $when;
24
    protected $expire;
25
    protected $createdAt;
26
    protected $updatedAt;
27
    protected $delay;
28
    protected $startedAt;
29
    protected $finishedAt;
30
    protected $maxDuration;
31
    protected $elapsed;
32
33
    /**
34
     * @var JobManagerInterface
35
     */
36
    protected $jobManager;
37
38
    /**
39
     * @return
40
     */
41
    public function getMessage()
42
    {
43
        return $this->message;
44
    }
45
46
    /**
47
     * @param $message
48
     */
49
    public function setMessage($message)
50
    {
51
        $this->message = $message;
52
    }
53
54
    /**
55
     * @return $status
0 ignored issues
show
Documentation introduced by
The doc-type $status could not be parsed: Unknown type name "$status" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
56
     */
57
    public function getStatus()
58
    {
59
        return $this->status;
60
    }
61
62
    /**
63
     * @return $locked
0 ignored issues
show
Documentation introduced by
The doc-type $locked could not be parsed: Unknown type name "$locked" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
64
     */
65
    public function getLocked()
66
    {
67
        return $this->locked;
68
    }
69
70
    /**
71
     * @return $lockedAt
0 ignored issues
show
Documentation introduced by
The doc-type $lockedAt could not be parsed: Unknown type name "$lockedAt" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
72
     */
73
    public function getLockedAt()
74
    {
75
        return $this->lockedAt;
76
    }
77
78
    /**
79
     * @return $expire
0 ignored issues
show
Documentation introduced by
The doc-type $expire could not be parsed: Unknown type name "$expire" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
80
     */
81
    public function getExpire()
82
    {
83
        return $this->expire;
84
    }
85
86
    /**
87
     * @param $status
88
     */
89
    public function setStatus($status)
90
    {
91
        $this->status = $status;
92
    }
93
94
    /**
95
     * @param $locked
96
     */
97
    public function setLocked($locked)
98
    {
99
        $this->locked = $locked;
100
    }
101
102
    /**
103
     * @param $lockedAt
104
     */
105
    public function setLockedAt($lockedAt)
106
    {
107
        $this->lockedAt = $lockedAt;
108
    }
109
110
    /**
111
     * @param $expire
112
     */
113
    public function setExpire($expire)
114
    {
115
        $this->expire = $expire;
116
    }
117
118
    /**
119
     * @return $id
0 ignored issues
show
Documentation introduced by
The doc-type $id could not be parsed: Unknown type name "$id" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
120
     */
121
    public function getId()
122
    {
123
        return $this->id;
124
    }
125
126
    /**
127
     * @return $workerName
0 ignored issues
show
Documentation introduced by
The doc-type $workerName could not be parsed: Unknown type name "$workerName" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
128
     */
129
    public function getWorkerName()
130
    {
131
        return $this->workerName;
132
    }
133
134
    /**
135
     * @return $className
0 ignored issues
show
Documentation introduced by
The doc-type $className could not be parsed: Unknown type name "$className" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
136
     */
137
    public function getClassName()
138
    {
139
        return $this->className;
140
    }
141
142
    /**
143
     * @return $method
0 ignored issues
show
Documentation introduced by
The doc-type $method could not be parsed: Unknown type name "$method" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
144
     */
145
    public function getMethod()
146
    {
147
        return $this->method;
148
    }
149
150
    /**
151
     * @return array
152
     */
153
    public function getArgs()
154
    {
155
        return $this->args;
156
    }
157
158
    /**
159
     * @return $batch
0 ignored issues
show
Documentation introduced by
The doc-type $batch could not be parsed: Unknown type name "$batch" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
160
     */
161
    public function getBatch()
162
    {
163
        return $this->batch;
164
    }
165
166
    /**
167
     * @return $priority
0 ignored issues
show
Documentation introduced by
The doc-type $priority could not be parsed: Unknown type name "$priority" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
168
     */
169
    public function getPriority()
170
    {
171
        return $this->priority;
172
    }
173
174
    /**
175
     * @return $crcHash
0 ignored issues
show
Documentation introduced by
The doc-type $crcHash could not be parsed: Unknown type name "$crcHash" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
176
     */
177
    public function getCrcHash()
178
    {
179
        return $this->crcHash;
180
    }
181
182
    /**
183
     * @return $when
0 ignored issues
show
Documentation introduced by
The doc-type $when could not be parsed: Unknown type name "$when" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
184
     */
185
    public function getWhen()
186
    {
187
        return $this->when;
188
    }
189
190
    /**
191
     * @return $createdAt
0 ignored issues
show
Documentation introduced by
The doc-type $createdAt could not be parsed: Unknown type name "$createdAt" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
192
     */
193
    public function getCreatedAt()
194
    {
195
        return $this->createdAt;
196
    }
197
198
    /**
199
     * @return $updatedAt
0 ignored issues
show
Documentation introduced by
The doc-type $updatedAt could not be parsed: Unknown type name "$updatedAt" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
200
     */
201
    public function getUpdatedAt()
202
    {
203
        return $this->updatedAt;
204
    }
205
206
    /**
207
     * @return $jobManager
0 ignored issues
show
Documentation introduced by
The doc-type $jobManager could not be parsed: Unknown type name "$jobManager" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
208
     */
209
    public function getJobManager()
210
    {
211
        return $this->jobManager;
212
    }
213
214
    /**
215
     * @param $id
216
     */
217
    public function setId($id)
218
    {
219
        $this->id = $id;
220
    }
221
222
    /**
223
     * @param $workerName
224
     */
225
    public function setWorkerName($workerName)
226
    {
227
        $this->workerName = $workerName;
228
    }
229
230
    /**
231
     * @param string $className
232
     */
233
    public function setClassName($className)
234
    {
235
        $this->className = $className;
236
    }
237
238
    /**
239
     * @param $method
240
     */
241
    public function setMethod($method)
242
    {
243
        $this->method = $method;
244
    }
245
246
    /**
247
     * @return mixed
248
     */
249
    public function getStartedAt()
250
    {
251
        return $this->startedAt;
252
    }
253
254
    /**
255
     * @param mixed $startedAt
256
     */
257
    public function setStartedAt(\DateTime $startedAt)
258
    {
259
        $this->startedAt = $startedAt;
260
    }
261
262
    /**
263
     * @return mixed
264
     */
265
    public function getFinishedAt()
266
    {
267
        return $this->finishedAt;
268
    }
269
270
    /**
271
     * @param mixed $finishedAt
272
     */
273
    public function setFinishedAt($finishedAt)
274
    {
275
        $this->finishedAt = $finishedAt;
276
    }
277
278
    /**
279
     * @return mixed
280
     */
281
    public function getMaxDuration()
282
    {
283
        return $this->maxDuration;
284
    }
285
286
    /**
287
     * @param mixed $maxDuration
288
     */
289
    public function setMaxDuration($maxDuration)
290
    {
291
        $this->maxDuration = $maxDuration;
292
    }
293
294
    /**
295
     * @param $args
296
     */
297
    public function setArgs($args)
298
    {
299
        if (!$this->recursiveValidArgs($args)) {
300
            throw new \Exception('Args must not contain object');
301
        }
302
303
        $this->args = $args;
304
    }
305
306
    protected function recursiveValidArgs($args)
307
    {
308
        if (is_array($args)) {
309
            foreach ($args as $key => $value) {
310
                if (!$this->recursiveValidArgs($value)) {
311
                    return false;
312
                }
313
            }
314
315
            return true;
316
        } else {
317
            return !is_object($args);
318
        }
319
    }
320
321
    /**
322
     * @param $batch
323
     */
324
    public function setBatch($batch)
325
    {
326
        $this->batch = $batch;
327
    }
328
329
    /**
330
     * @param $priority
331
     */
332
    public function setPriority($priority)
333
    {
334
        $this->priority = $priority;
335
    }
336
337
    /**
338
     * @param $crcHash
339
     */
340
    public function setCrcHash($crcHash)
341
    {
342
        $this->crcHash = $crcHash;
343
    }
344
345
    /**
346
     * @param $when
347
     */
348
    public function setWhen($when)
349
    {
350
        $this->when = $when;
351
    }
352
353
    /**
354
     * @param $createdAt
355
     */
356
    public function setCreatedAt($createdAt)
357
    {
358
        $this->createdAt = $createdAt;
359
    }
360
361
    /**
362
     * @param $updatedAt
363
     */
364
    public function setUpdatedAt($updatedAt)
365
    {
366
        $this->updatedAt = $updatedAt;
367
    }
368
369
    /**
370
     * @param $jobManager
371
     */
372
    public function setJobManager($jobManager)
373
    {
374
        $this->jobManager = $jobManager;
375
    }
376
377
    protected $worker;
378
379
    public function __construct(Worker $worker = null, $batch = false, $priority = 10, \DateTime $when = null)
380
    {
381
        $this->worker = $worker;
382
        if ($worker) {
383
            $this->jobManager = $worker->getJobManager();
384
            $this->className = get_class($worker);
385
            $this->workerName = $worker->getName();
386
        }
387
388
        $this->when = $when;
389
        $this->batch = $batch ? true : false;
390
        $this->priority = $priority;
391
        $this->status = self::STATUS_NEW;
392
    }
393
394
    public function __call($method, $args)
395
    {
396
        $this->method = $method;
397
        $this->setArgs($args);
398
        $this->createdAt = new \DateTime();
399
        $this->updatedAt = new \DateTime();
400
401
        // Make sure the method exists - job should not be created
402
        if (!method_exists($this->worker, $method)) {
403
            throw new \Exception("{$this->className}->{$method}() does not exist");
404
        }
405
406
        $this->jobManager->save($this);
407
408
        return $this;
409
    }
410
411
    /**
412
     * @return $delay
0 ignored issues
show
Documentation introduced by
The doc-type $delay could not be parsed: Unknown type name "$delay" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
413
     */
414
    public function getDelay()
415
    {
416
        return $this->delay;
417
    }
418
419
    /**
420
     * @return $worker
0 ignored issues
show
Documentation introduced by
The doc-type $worker could not be parsed: Unknown type name "$worker" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
421
     */
422
    public function getWorker()
423
    {
424
        return $this->worker;
425
    }
426
427
    /**
428
     * @param $delay
429
     */
430
    public function setDelay($delay)
431
    {
432
        $this->delay = $delay;
433
    }
434
435
    /**
436
     * @param Worker $worker
437
     */
438
    public function setWorker($worker)
439
    {
440
        $this->worker = $worker;
441
    }
442
443
    /**
444
     * @return $elapsed
0 ignored issues
show
Documentation introduced by
The doc-type $elapsed could not be parsed: Unknown type name "$elapsed" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
445
     */
446
    public function getElapsed()
447
    {
448
        return $this->elapsed;
449
    }
450
451
    /**
452
     * @param $elapsed
453
     */
454
    public function setElapsed($elapsed)
455
    {
456
        $this->elapsed = $elapsed;
457
    }
458
459
    public function toMessage()
460
    {
461
        $arr = array(
462
            'worker' => $this->getWorkerName(),
463
            'args' => $this->getArgs(),
464
            'method' => $this->getMethod(),
465
        );
466
467
        return json_encode($arr);
468
    }
469
470
    public function fromMessage($message)
471
    {
472
        $arr = json_decode($message, true);
473
        $this->setWorkerName($arr['worker']);
474
        $this->setArgs($arr['args']);
475
        $this->setMethod($arr['method']);
476
    }
477
}
478