Completed
Pull Request — master (#30)
by Matthew
16:57
created

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