Completed
Pull Request — master (#30)
by Matthew
19:54
created

MessageableJob::fromMessagearrayRetries()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

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