Completed
Push — master ( e02161...971421 )
by Matthew
12:50
created

BaseJob::validateArgs()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 8
cp 0.875
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 1
crap 4.0312
1
<?php
2
3
namespace Dtc\QueueBundle\Model;
4
5
abstract class BaseJob
6
{
7
    const STATUS_SUCCESS = 'success';
8
    const STATUS_ERROR = 'error';
9
    const STATUS_NEW = 'new';
10
    const STATUS_RUNNING = 'running';
11
12
    /**
13
     * @var JobManagerInterface
14
     */
15
    protected $jobManager;
16
    protected $worker;
17
    protected $workerName;
18
    protected $className;
19
    protected $args;
20
    protected $batch;
21
    protected $method;
22
    protected $priority;
23
    protected $whenAt;
24
    protected $status;
25
26 9
    public function __construct(Worker $worker = null, $batch = false, $priority = 10, \DateTime $whenAt = null)
27
    {
28 9
        $this->worker = $worker;
29 9
        if ($worker) {
30 8
            $this->jobManager = $worker->getJobManager();
31 8
            $this->className = get_class($worker);
32 8
            $this->workerName = $worker->getName();
33 8
        }
34
35 9
        $this->whenAt = $whenAt;
36 9
        $this->batch = $batch ? true : false;
37 9
        $this->priority = $priority;
38 9
        $this->status = self::STATUS_NEW;
39 9
    }
40
41
    /**
42
     * @param string $status The status of the job
43
     */
44 2
    public function setStatus($status)
45
    {
46 2
        $this->status = $status;
47
48 2
        return $this;
49
    }
50
51
    /**
52
     * @return string The status of the job
53
     */
54 2
    public function getStatus()
55
    {
56 2
        return $this->status;
57
    }
58
59
    /**
60
     * @return \DateTime|null
61
     */
62 1
    public function getWhenAt()
63
    {
64 1
        return $this->whenAt;
65
    }
66
67
    /**
68
     * @param \DateTime $whenAt
69
     */
70 1
    public function setWhenAt(\DateTime $whenAt)
71
    {
72 1
        $this->whenAt = $whenAt;
73
74 1
        return $this;
75
    }
76
77
    /**
78
     * @return Worker
79
     */
80 2
    public function getWorker()
81
    {
82 2
        return $this->worker;
83
    }
84
85
    /**
86
     * @param Worker $worker
87
     */
88 2
    public function setWorker($worker)
89
    {
90 2
        $this->worker = $worker;
91
92 2
        return $this;
93
    }
94
95
    /**
96
     * @return bool
97
     */
98 3
    public function getBatch()
99
    {
100 3
        return $this->batch;
101
    }
102
103
    /**
104
     * @param bool $batch
105
     */
106 2
    public function setBatch($batch)
107
    {
108 2
        $this->batch = $batch;
109
110 2
        return $this;
111
    }
112
113
    /**
114
     * @return mixed
115
     */
116 8
    public function getArgs()
117
    {
118 8
        return $this->args;
119
    }
120
121
    /**
122
     * @param $args
123
     */
124 9
    public function setArgs($args)
125
    {
126 9
        if (!$this->validateArgs($args)) {
127
            throw new \InvalidArgumentException('Args must not contain object');
128
        }
129
130 9
        $this->args = $args;
131
132 9
        return $this;
133
    }
134
135 9
    protected function validateArgs($args)
136
    {
137 9
        if (is_array($args)) {
138 8
            foreach ($args as $key => $value) {
139 8
                if (!$this->validateArgs($value)) {
140
                    return false;
141
                }
142 8
            }
143
144 8
            return true;
145
        } else {
146 9
            return !is_object($args);
147
        }
148
    }
149
150
    /**
151
     * @return string
152
     */
153 8
    public function getMethod()
154
    {
155 8
        return $this->method;
156
    }
157
158
    /**
159
     * @param string $method
160
     */
161 7
    public function setMethod($method)
162
    {
163 7
        $this->method = $method;
164
165 7
        return $this;
166
    }
167
168
    /**
169
     * @return int
170
     */
171 8
    public function getPriority()
172
    {
173 8
        return $this->priority;
174
    }
175
176
    /**
177
     * @param int $priority
178
     */
179 4
    public function setPriority($priority)
180
    {
181 4
        $this->priority = $priority;
182
183 4
        return $this;
184
    }
185
186
    /**
187
     * @param string $workerName
188
     */
189 6
    public function setWorkerName($workerName)
190
    {
191 6
        $this->workerName = $workerName;
192
193 6
        return $this;
194
    }
195
196
    /**
197
     * @param string $className
198
     */
199 2
    public function setClassName($className)
200
    {
201 2
        $this->className = $className;
202
203 2
        return $this;
204
    }
205
206
    /**
207
     * @return string
208
     */
209 8
    public function getWorkerName()
210
    {
211 8
        return $this->workerName;
212
    }
213
214
    /**
215
     * @return string
216
     */
217 3
    public function getClassName()
218
    {
219 3
        return $this->className;
220
    }
221
222
    /**
223
     * @return JobManagerInterface
224
     */
225 1
    public function getJobManager()
226
    {
227 1
        return $this->jobManager;
228
    }
229
}
230