Passed
Pull Request — master (#57)
by Matthew
08:25
created

MessageableJob::setByList()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Dtc\QueueBundle\Model;
4
5
use Dtc\QueueBundle\Util\Util;
6
7
abstract class MessageableJob extends RetryableJob
8
{
9 52
    public function __construct(Worker $worker = null, $batch = false, $priority = null, \DateTime $whenAt = null)
10
    {
11 52
        parent::__construct($worker, $batch, $priority, $whenAt);
12 52
        if (!$this->getWhenAt()) {
13 52
            $this->setWhenAt(Util::getMicrotimeDateTime());
14
        }
15 52
    }
16
17 46
    protected function toMessageArray()
18
    {
19
        return array(
20 46
            'worker' => $this->getWorkerName(),
21 46
            'args' => $this->getArgs(),
22 46
            'method' => $this->getMethod(),
23 46
            'priority' => $this->getPriority(),
24 46
            'whenAt' => $this->getWhenAt()->format('U.u'),
25 46
            'createdAt' => $this->getCreatedAt()->format('U.u'),
26 46
            'updatedAt' => $this->getUpdatedAt()->format('U.u'),
27 46
            'expiresAt' => ($expiresAt = $this->getExpiresAt()) ? $expiresAt->format('U.u') : null,
28 46
            'retries' => $this->getRetries(),
29 46
            'maxRetries' => $this->getMaxRetries(),
30 46
            'failures' => $this->getFailures(),
31 46
            'maxFailures' => $this->getMaxFailures(),
32 46
            'exceptions' => $this->getExceptions(),
33 46
            'maxExceptions' => $this->getMaxExceptions(),
34
        );
35
    }
36
37
    /**
38
     * @return string A json_encoded version of a queueable version of the object
39
     */
40 8
    public function toMessage()
41
    {
42 8
        return json_encode($this->toMessageArray());
43
    }
44
45
    /**
46
     * @param string $message a json_encoded version of the object
47
     */
48 6
    public function fromMessage($message)
49
    {
50 6
        $arr = json_decode($message, true);
51 6
        if (is_array($arr)) {
52 6
            $this->fromMessageArray($arr);
53
        }
54 6
    }
55
56 38
    protected function fromMessageArray(array $arr)
57
    {
58 38
        $this->setByList(['args', 'priority', 'method'], $arr);
59 38
        if (isset($arr['worker'])) {
60 38
            $this->setWorkerName($arr['worker']);
61
        }
62
63 38
        $this->fromMessageArrayRetries($arr);
64 38
        foreach (['expiresAt', 'createdAt', 'updatedAt', 'whenAt'] as $dateField) {
65 38
            if (isset($arr[$dateField])) {
66 38
                $this->setDateTimeField($dateField, $arr[$dateField]);
67
            }
68
        }
69 38
    }
70
71
    /**
72
     * @param $dateField string
73
     * @param $timeStr string|null
74
     */
75 38
    private function setDateTimeField($dateField, $timeStr)
76
    {
77 38
        if ($timeStr) {
78 38
            $dateTime = \DateTime::createFromFormat('U.u', $timeStr);
79 38
            if ($dateTime) {
0 ignored issues
show
introduced by
The condition $dateTime can never be true.
Loading history...
80 38
                $method = 'set'.ucfirst($dateField);
81 38
                $this->$method($dateTime);
82
            }
83
        }
84 38
    }
85
86 38
    private function setByList(array $list, array $arr)
87
    {
88 38
        foreach ($list as $key) {
89 38
            if (isset($arr[$key])) {
90 38
                $method = 'set'.ucfirst($key);
91 38
                $this->$method($arr[$key]);
92
            }
93
        }
94 38
    }
95
96 38
    protected function fromMessagearrayRetries(array $arr)
97
    {
98 38
        $this->setByList(['retries', 'maxRetries', 'failures', 'maxFailures', 'exceptions', 'maxExceptions'], $arr);
99 38
    }
100
}
101