Completed
Push — master ( 40b87c...f82b1c )
by Matthew
04:37
created

MessageableJob::fromMessageArray()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 14
cts 14
cp 1
rs 7.551
c 0
b 0
f 0
cc 7
eloc 13
nc 32
nop 1
crap 7
1
<?php
2
3
namespace Dtc\QueueBundle\Model;
4
5
abstract class MessageableJob extends Job
6
{
7 12
    protected function toMessageArray()
8
    {
9
        return array(
10 12
            'worker' => $this->getWorkerName(),
11 12
            'args' => $this->getArgs(),
12 12
            'method' => $this->getMethod(),
13 12
            'expiresAt' => ($expiresAt = $this->getExpiresAt()) ? $expiresAt->format('U.u') : null,
14
        );
15
    }
16
17
    /**
18
     * @return string A json_encoded version of a queueable version of the object
19
     */
20 6
    public function toMessage()
21
    {
22 6
        return json_encode($this->toMessageArray());
23
    }
24
25
    /**
26
     * @param string $message a json_encoded version of the object
27
     */
28 5
    public function fromMessage($message)
29
    {
30 5
        $arr = json_decode($message, true);
31 5
        if (is_array($arr)) {
32 5
            $this->fromMessageArray($arr);
33
        }
34 5
    }
35
36 11
    protected function fromMessageArray(array $arr)
37
    {
38 11
        if (isset($arr['worker'])) {
39 11
            $this->setWorkerName($arr['worker']);
40
        }
41 11
        if (isset($arr['args'])) {
42 11
            $this->setArgs($arr['args']);
43
        }
44 11
        if (isset($arr['method'])) {
45 11
            $this->setMethod($arr['method']);
46
        }
47 11
        if (isset($arr['expiresAt'])) {
48 2
            $expiresAt = $arr['expiresAt'];
49 2
            if ($expiresAt) {
50 2
                $dateTime = \DateTime::createFromFormat('U.u', $expiresAt);
51 2
                if ($dateTime) {
52 2
                    $this->setExpiresAt($dateTime);
53
                }
54
            }
55
        }
56 11
    }
57
}
58