Passed
Pull Request — master (#57)
by Matthew
07:36
created

BaseJob::validateArgs()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

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