Completed
Pull Request — master (#30)
by Matthew
07:06
created

BaseJob::getWorker()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 105
    public function __construct(Worker $worker = null, $batch = false, $priority = 10, \DateTime $whenAt = null)
32
    {
33 105
        if ($worker) {
34 91
            $this->setWorker($worker);
35 91
            if ($jobManager = $worker->getJobManager()) {
36 88
                $this->setJobManager($jobManager);
37
            }
38 91
            $this->setClassName(get_class($worker));
39 91
            $this->setWorkerName($worker->getName());
40
        }
41
42 105
        if ($whenAt) {
43 21
            $this->setWhenAt($whenAt);
44
        }
45 105
        $this->setBatch($batch ? true : false);
46 105
        $this->setPriority($priority);
47 105
        $this->setStatus(self::STATUS_NEW);
48 105
        $dateTime = \DateTime::createFromFormat('U.u', Util::getMicrotimeStr());
49 105
        $this->setCreatedAt($dateTime);
0 ignored issues
show
Security Bug introduced by
It seems like $dateTime defined by \DateTime::createFromFor...til::getMicrotimeStr()) on line 48 can also be of type false; however, Dtc\QueueBundle\Model\BaseJob::setCreatedAt() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
50 105
    }
51
52
    /**
53
     * @param string $status The status of the job
54
     */
55 105
    public function setStatus($status)
56
    {
57 105
        $this->status = $status;
58
59 105
        return $this;
60
    }
61
62
    /**
63
     * @return string The status of the job
64
     */
65 55
    public function getStatus()
66
    {
67 55
        return $this->status;
68
    }
69
70
    /**
71
     * @return \DateTime|null
72
     */
73 44
    public function getWhenAt()
74
    {
75 44
        return $this->whenAt;
76
    }
77
78
    /**
79
     * @param \DateTime $whenAt
80
     */
81 54
    public function setWhenAt(\DateTime $whenAt)
82
    {
83 54
        $this->whenAt = $whenAt;
84
85 54
        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 98
    public function setWorker($worker)
100
    {
101 98
        $this->worker = $worker;
102
103 98
        return $this;
104
    }
105
106
    /**
107
     * @return bool
108
     */
109 70
    public function getBatch()
110
    {
111 70
        return $this->batch;
112
    }
113
114
    /**
115
     * @param bool $batch
116
     */
117 105
    public function setBatch($batch)
118
    {
119 105
        $this->batch = $batch;
120
121 105
        return $this;
122
    }
123
124
    /**
125
     * @return mixed
126
     */
127 94
    public function getArgs()
128
    {
129 94
        return $this->args;
130
    }
131
132
    /**
133
     * @param $args
134
     */
135 102
    public function setArgs($args)
136
    {
137 102
        if (!$this->validateArgs($args)) {
138 5
            throw new \InvalidArgumentException('Args must not contain object');
139
        }
140
141 102
        $this->args = $args;
142
143 102
        return $this;
144
    }
145
146 102
    protected function validateArgs($args)
147
    {
148 102
        if (is_array($args)) {
149 72
            foreach ($args as $key => $value) {
150 72
                if (!$this->validateArgs($value)) {
151 72
                    return false;
152
                }
153
            }
154
155 72
            return true;
156
        } else {
157 102
            return !is_object($args);
158
        }
159
    }
160
161
    /**
162
     * @return string
163
     */
164 90
    public function getMethod()
165
    {
166 90
        return $this->method;
167
    }
168
169
    /**
170
     * @param string $method
171
     */
172 63
    public function setMethod($method)
173
    {
174 63
        $this->method = $method;
175
176 63
        return $this;
177
    }
178
179
    /**
180
     * @return int
181
     */
182 88
    public function getPriority()
183
    {
184 88
        return $this->priority;
185
    }
186
187
    /**
188
     * @param int $priority
189
     */
190 105
    public function setPriority($priority)
191
    {
192 105
        $this->priority = $priority;
193
194 105
        return $this;
195
    }
196
197
    /**
198
     * @param string $workerName
199
     */
200 98
    public function setWorkerName($workerName)
201
    {
202 98
        $this->workerName = $workerName;
203
204 98
        return $this;
205
    }
206
207
    /**
208
     * @param string $className
209
     */
210 98
    public function setClassName($className)
211
    {
212 98
        $this->className = $className;
213
214 98
        return $this;
215
    }
216
217
    /**
218
     * @return string
219
     */
220 96
    public function getWorkerName()
221
    {
222 96
        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
     * @return \DateTime
243
     */
244 89
    public function getCreatedAt()
245
    {
246 89
        return $this->createdAt;
247
    }
248
249
    /**
250
     * @param \DateTime $createdAt
251
     */
252 105
    public function setCreatedAt(\DateTime $createdAt)
253
    {
254 105
        $this->createdAt = $createdAt;
255
256 105
        return $this;
257
    }
258
}
259