Test Setup Failed
Push — master ( a87626...531aa8 )
by Matthew
02:42
created

Job::toMessageArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
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
    const STATUS_RUNNING = 'running';
11
12
    protected $id;
13
    protected $workerName;
14
    protected $className;
15
    protected $method;
16
    protected $args;
17
    protected $batch;
18
    protected $status;
19
    protected $message;
20
    protected $priority;
21
    protected $crcHash;
22
    protected $locked;
23
    protected $lockedAt;
24
    protected $whenAt;
25
    protected $expiresAt;
26
    protected $createdAt;
27
    protected $updatedAt;
28
    protected $delay;
29
    protected $startedAt;
30
    protected $finishedAt;
31
    protected $maxDuration;
32
    protected $elapsed;
33
    protected $runId;
34
    protected $worker;
35
36
    public function __construct(Worker $worker = null, $batch = false, $priority = 10, \DateTime $whenAt = null)
37
    {
38
        $this->worker = $worker;
39
        if ($worker) {
40
            $this->jobManager = $worker->getJobManager();
41
            $this->className = get_class($worker);
42
            $this->workerName = $worker->getName();
43
        }
44
45
        $this->whenAt = $whenAt;
46
        $this->batch = $batch ? true : false;
47
        $this->priority = $priority;
48
        $this->status = self::STATUS_NEW;
49
    }
50
51
    public function __call($method, $args)
52
    {
53
        $this->method = $method;
54
        $this->setArgs($args);
55
        $this->createdAt = new \DateTime();
56
        $this->updatedAt = new \DateTime();
57
58
        // Make sure the method exists - job should not be created
59
        if (!method_exists($this->worker, $method)) {
60
            throw new \Exception("{$this->className}->{$method}() does not exist");
61
        }
62
63
        $this->jobManager->save($this);
64
65
        return $this;
66
    }
67
68
    /**
69
     * @var JobManagerInterface
70
     */
71
    protected $jobManager;
72
73
    /**
74
     * @return string|null
75
     */
76
    public function getMessage()
77
    {
78
        return $this->message;
79
    }
80
81
    /**
82
     * @param string $message
83
     */
84
    public function setMessage($message)
85
    {
86
        $this->message = $message;
87
    }
88
89
    /**
90
     * @return string The status of the job
91
     */
92
    public function getStatus()
93
    {
94
        return $this->status;
95
    }
96
97
    /**
98
     * @return bool|null
99
     */
100
    public function getLocked()
101
    {
102
        return $this->locked;
103
    }
104
105
    /**
106
     * @return \DateTime|null
107
     */
108
    public function getLockedAt()
109
    {
110
        return $this->lockedAt;
111
    }
112
113
    /**
114
     * @return \DateTime|null
115
     */
116
    public function getExpiresAt()
117
    {
118
        return $this->expiresAt;
119
    }
120
121
    /**
122
     * @param string $status The status of the job
123
     */
124
    public function setStatus($status)
125
    {
126
        $this->status = $status;
127
    }
128
129
    /**
130
     * @param bool|null $locked
131
     */
132
    public function setLocked($locked)
133
    {
134
        $this->locked = $locked;
135
    }
136
137
    /**
138
     * @param \DateTime|null $lockedAt
139
     */
140
    public function setLockedAt($lockedAt)
141
    {
142
        $this->lockedAt = $lockedAt;
143
    }
144
145
    /**
146
     * @param \DateTime $expiresAt
147
     */
148
    public function setExpiresAt(\DateTime $expiresAt)
149
    {
150
        $this->expiresAt = $expiresAt;
151
    }
152
153
    /**
154
     * @return int
155
     */
156
    public function getId()
157
    {
158
        return $this->id;
159
    }
160
161
    /**
162
     * @return string
163
     */
164
    public function getWorkerName()
165
    {
166
        return $this->workerName;
167
    }
168
169
    /**
170
     * @return string
171
     */
172
    public function getClassName()
173
    {
174
        return $this->className;
175
    }
176
177
    /**
178
     * @return string
179
     */
180
    public function getMethod()
181
    {
182
        return $this->method;
183
    }
184
185
    /**
186
     * @return mixed
187
     */
188
    public function getArgs()
189
    {
190
        return $this->args;
191
    }
192
193
    /**
194
     * @return bool
195
     */
196
    public function getBatch()
197
    {
198
        return $this->batch;
199
    }
200
201
    /**
202
     * @return int
203
     */
204
    public function getPriority()
205
    {
206
        return $this->priority;
207
    }
208
209
    /**
210
     * @return string
211
     */
212
    public function getCrcHash()
213
    {
214
        return $this->crcHash;
215
    }
216
217
    /**
218
     * @return \DateTime|null
219
     */
220
    public function getWhenAt()
221
    {
222
        return $this->whenAt;
223
    }
224
225
    /**
226
     * @return \DateTime
227
     */
228
    public function getCreatedAt()
229
    {
230
        return $this->createdAt;
231
    }
232
233
    /**
234
     * @return \DateTime
235
     */
236
    public function getUpdatedAt()
237
    {
238
        return $this->updatedAt;
239
    }
240
241
    /**
242
     * @return JobManagerInterface
243
     */
244
    public function getJobManager()
245
    {
246
        return $this->jobManager;
247
    }
248
249
    /**
250
     * @param mixed $id
251
     */
252
    public function setId($id)
253
    {
254
        $this->id = $id;
255
    }
256
257
    /**
258
     * @param string $workerName
259
     */
260
    public function setWorkerName($workerName)
261
    {
262
        $this->workerName = $workerName;
263
    }
264
265
    /**
266
     * @param string $className
267
     */
268
    public function setClassName($className)
269
    {
270
        $this->className = $className;
271
    }
272
273
    /**
274
     * @param string $method
275
     */
276
    public function setMethod($method)
277
    {
278
        $this->method = $method;
279
    }
280
281
    /**
282
     * @return \DateTime|null
283
     */
284
    public function getStartedAt()
285
    {
286
        return $this->startedAt;
287
    }
288
289
    /**
290
     * @param \DateTime $startedAt
291
     */
292
    public function setStartedAt(\DateTime $startedAt)
293
    {
294
        $this->startedAt = $startedAt;
295
    }
296
297
    /**
298
     * @return \DateTime|null
299
     */
300
    public function getFinishedAt()
301
    {
302
        return $this->finishedAt;
303
    }
304
305
    /**
306
     * @param \DateTime|null $finishedAt
307
     */
308
    public function setFinishedAt($finishedAt)
309
    {
310
        $this->finishedAt = $finishedAt;
311
    }
312
313
    /**
314
     * @return int|null
315
     */
316
    public function getMaxDuration()
317
    {
318
        return $this->maxDuration;
319
    }
320
321
    /**
322
     * @param int|null $maxDuration
323
     */
324
    public function setMaxDuration($maxDuration)
325
    {
326
        $this->maxDuration = $maxDuration;
327
    }
328
329
    /**
330
     * @param $args
331
     */
332
    public function setArgs($args)
333
    {
334
        if (!$this->validateArgs($args)) {
335
            throw new \Exception('Args must not contain object');
336
        }
337
338
        $this->args = $args;
339
    }
340
341
    protected function validateArgs($args)
342
    {
343
        if (is_array($args)) {
344
            foreach ($args as $key => $value) {
345
                if (!$this->validateArgs($value)) {
346
                    return false;
347
                }
348
            }
349
350
            return true;
351
        } else {
352
            return !is_object($args);
353
        }
354
    }
355
356
    /**
357
     * @param bool $batch
358
     */
359
    public function setBatch($batch)
360
    {
361
        $this->batch = $batch;
362
    }
363
364
    /**
365
     * @param int $priority
366
     */
367
    public function setPriority($priority)
368
    {
369
        $this->priority = $priority;
370
    }
371
372
    /**
373
     * @param string $crcHash
374
     */
375
    public function setCrcHash($crcHash)
376
    {
377
        $this->crcHash = $crcHash;
378
    }
379
380
    /**
381
     * @param \DateTime $whenAt
382
     */
383
    public function setWhenAt(\DateTime $whenAt)
384
    {
385
        $this->whenAt = $whenAt;
386
    }
387
388
    /**
389
     * @param \DateTime $createdAt
390
     */
391
    public function setCreatedAt(\DateTime $createdAt)
392
    {
393
        $this->createdAt = $createdAt;
394
    }
395
396
    /**
397
     * @param \DateTime $updatedAt
398
     */
399
    public function setUpdatedAt(\DateTime $updatedAt)
400
    {
401
        $this->updatedAt = $updatedAt;
402
    }
403
404
    /**
405
     * @param JobManagerInterface $jobManager
406
     */
407
    public function setJobManager(JobManagerInterface $jobManager)
408
    {
409
        $this->jobManager = $jobManager;
410
    }
411
412
    /**
413
     * @return int
414
     */
415
    public function getDelay()
416
    {
417
        return $this->delay;
418
    }
419
420
    /**
421
     * @return Worker
422
     */
423
    public function getWorker()
424
    {
425
        return $this->worker;
426
    }
427
428
    /**
429
     * @param int $delay Delay in seconds
430
     */
431
    public function setDelay($delay)
432
    {
433
        $this->delay = $delay;
434
    }
435
436
    /**
437
     * @param Worker $worker
438
     */
439
    public function setWorker($worker)
440
    {
441
        $this->worker = $worker;
442
    }
443
444
    /**
445
     * @return int
446
     */
447
    public function getElapsed()
448
    {
449
        return $this->elapsed;
450
    }
451
452
    /**
453
     * @param int $elapsed
454
     */
455
    public function setElapsed($elapsed)
456
    {
457
        $this->elapsed = $elapsed;
458
    }
459
460
    /**
461
     * @return mixed
462
     */
463
    public function getRunId()
464
    {
465
        return $this->runId;
466
    }
467
468
    /**
469
     * @param mixed $runId
470
     */
471
    public function setRunId($runId)
472
    {
473
        $this->runId = $runId;
474
    }
475
476
    protected function toMessageArray()
477
    {
478
        return array(
479
            'worker' => $this->getWorkerName(),
480
            'args' => $this->getArgs(),
481
            'method' => $this->getMethod(),
482
            'expiresAt' => ($expiresAt = $this->getExpiresAt()) ? $expiresAt->format('U.u') : null,
483
        );
484
    }
485
486
    /**
487
     * @return string A json_encoded version of a queueable version of the object
488
     */
489
    public function toMessage()
490
    {
491
        return json_encode($this->toMessageArray());
492
    }
493
494
    /**
495
     * @param string $message a json_encoded version of the object
496
     */
497
    public function fromMessage($message)
498
    {
499
        $arr = json_decode($message, true);
500
        if (is_array($arr)) {
501
            $this->fromMessageArray($arr);
502
        }
503
    }
504
505
    protected function fromMessageArray(array $arr)
506
    {
507
        if (isset($arr['worker'])) {
508
            $this->setWorkerName($arr['worker']);
509
        }
510
        if (isset($arr['args'])) {
511
            $this->setArgs($arr['args']);
512
        }
513
        if (isset($arr['method'])) {
514
            $this->setMethod($arr['method']);
515
        }
516
        if (isset($arr['expiresAt'])) {
517
            $expiresAt = $arr['expiresAt'];
518
            if ($expiresAt) {
519
                $dateTime = \DateTime::createFromFormat('U.u', $expiresAt);
520
                if ($dateTime) {
521
                    $this->setExpiresAt($dateTime);
522
                }
523
            }
524
        }
525
    }
526
}
527