Completed
Push — master ( 3e20b8...89d50e )
by Luna
29s queued 25s
created

Task::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Cmp\Queues\Domain\Task;
4
5
use Cmp\Queues\Domain\Task\Exception\TaskException;
6
7
class Task implements TaskInterface
8
{
9
    /**
10
     * @var string
11
     */
12
    private $name;
13
14
    /**
15
     * @var array
16
     */
17
    private $body;
18
19
    /**
20
     * @var int
21
     */
22
    private $delay;
23
24
    /**
25
     * Task constructor.
26
     * @param string $name
27
     * @param array $body
28
     * @param int $delay
29
     * @throws TaskException
30
     */
31
    public function __construct($name, array $body, $delay=0)
32
    {
33
        $this->setName($name)
34
             ->setDelay($delay)
35
        ;
36
        $this->body = $body;
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    public function getName()
43
    {
44
        return $this->name;
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function getBody()
51
    {
52
        return $this->body;
53
    }
54
55
    /**
56
     * @return int
57
     */
58
    public function getDelay()
59
    {
60
        return $this->delay;
61
    }
62
63
    /**
64
     * @param $name
65
     * @return $this
66
     * @throws TaskException
67
     */
68
    protected function setName($name)
69
    {
70
        if(empty($name)) {
71
            throw new TaskException('Task name cannot be empty');
72
        }
73
        $this->name = $name;
74
        return $this;
75
    }
76
77
    /**
78
     * @param $delay
79
     * @return $this
80
     * @throws TaskException
81
     */
82
    protected function setDelay($delay)
83
    {
84
        if(!is_null($delay) && !preg_match('/^\d+$/', $delay)) {
85
            throw new TaskException("Task delay $delay is not a valid delay.");
86
        }
87
        $this->delay = $delay;
88
        return $this;
89
    }
90
91
92
    /**
93
     * @return array
94
     */
95
    public function jsonSerialize()
96
    {
97
        return [
98
            'name' => $this->name,
99
            'body' => $this->body,
100
            'delay' => $this->delay
101
        ];
102
    }
103
104
    /**
105
     * @return array
106
     */
107
    public function getAttributes()
108
    {
109
        return [];
110
    }
111
}
112