Test Setup Failed
Push — master ( 298fd3...56daff )
by Matthew
17:14
created

Job::setMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Dtc\QueueBundle\Model;
4
5
class Job
6
{
7
    const STATUS_SUCCESS = 'success';
8
    const STATUS_ERROR = 'error';
9
    const STATUS_NEW = 'new';
10
    const STATUS_RUNNING = 'running';
11
12
    protected $id;
13
    protected $workerName;
14
    protected $className;
15
    protected $method;
16
    protected $args;
17
    protected $batch;
18
    protected $status;
19
    protected $message;
20
    protected $priority;
21
    protected $crcHash;
22
    protected $locked;
23
    protected $lockedAt;
24
    protected $whenAt;
25
    protected $expiresAt;
26
    protected $createdAt;
27
    protected $updatedAt;
28
    protected $delay;
29
    protected $startedAt;
30
    protected $finishedAt;
31
    protected $maxDuration;
32
    protected $elapsed;
33
    protected $runId;
34
35
    /**
36
     * @var JobManagerInterface
37
     */
38
    protected $jobManager;
39
40
    /**
41
     * @return string|null
42
     */
43
    public function getMessage()
44
    {
45
        return $this->message;
46
    }
47
48
    /**
49
     * @param string $message
50
     */
51
    public function setMessage($message)
52
    {
53
        $this->message = $message;
54
    }
55
56
    /**
57
     * @return string The status of the job
58
     */
59
    public function getStatus()
60
    {
61
        return $this->status;
62
    }
63
64
    /**
65
     * @return bool|null
66
     */
67
    public function getLocked()
68
    {
69
        return $this->locked;
70
    }
71
72
    /**
73
     * @return \DateTime|null
74
     */
75
    public function getLockedAt()
76
    {
77
        return $this->lockedAt;
78
    }
79
80
    /**
81
     * @return \DateTime|null
82
     */
83
    public function getExpiresAt()
84
    {
85
        return $this->expiresAt;
86
    }
87
88
    /**
89
     * @param string $status The status of the job
90
     */
91
    public function setStatus($status)
92
    {
93
        $this->status = $status;
94
    }
95
96
    /**
97
     * @param bool|null $locked
98
     */
99
    public function setLocked($locked)
100
    {
101
        $this->locked = $locked;
102
    }
103
104
    /**
105
     * @param \DateTime|null $lockedAt
106
     */
107
    public function setLockedAt($lockedAt)
108
    {
109
        $this->lockedAt = $lockedAt;
110
    }
111
112
    /**
113
     * @param \DateTime $expiresAt
114
     */
115
    public function setExpiresAt(\DateTime $expiresAt)
116
    {
117
        $this->expiresAt = $expiresAt;
118
    }
119
120
    /**
121
     * @return int
122
     */
123
    public function getId()
124
    {
125
        return $this->id;
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    public function getWorkerName()
132
    {
133
        return $this->workerName;
134
    }
135
136
    /**
137
     * @return string
138
     */
139
    public function getClassName()
140
    {
141
        return $this->className;
142
    }
143
144
    /**
145
     * @return string
146
     */
147
    public function getMethod()
148
    {
149
        return $this->method;
150
    }
151
152
    /**
153
     * @return
154
     */
155
    public function getArgs()
156
    {
157
        return $this->args;
158
    }
159
160
    /**
161
     * @return bool
162
     */
163
    public function getBatch()
164
    {
165
        return $this->batch;
166
    }
167
168
    /**
169
     * @return int
170
     */
171
    public function getPriority()
172
    {
173
        return $this->priority;
174
    }
175
176
    /**
177
     * @return string
178
     */
179
    public function getCrcHash()
180
    {
181
        return $this->crcHash;
182
    }
183
184
    /**
185
     * @return \DateTime|null
186
     */
187
    public function getWhenAt()
188
    {
189
        return $this->whenAt;
190
    }
191
192
    /**
193
     * @return \DateTime
194
     */
195
    public function getCreatedAt()
196
    {
197
        return $this->createdAt;
198
    }
199
200
    /**
201
     * @return \DateTime
202
     */
203
    public function getUpdatedAt()
204
    {
205
        return $this->updatedAt;
206
    }
207
208
    /**
209
     * @return JobManagerInterface
210
     */
211
    public function getJobManager()
212
    {
213
        return $this->jobManager;
214
    }
215
216
    /**
217
     * @param mixed $id
218
     */
219
    public function setId($id)
220
    {
221
        $this->id = $id;
222
    }
223
224
    /**
225
     * @param string $workerName
226
     */
227
    public function setWorkerName($workerName)
228
    {
229
        $this->workerName = $workerName;
230
    }
231
232
    /**
233
     * @param string $className
234
     */
235
    public function setClassName($className)
236
    {
237
        $this->className = $className;
238
    }
239
240
    /**
241
     * @param string $method
242
     */
243
    public function setMethod($method)
244
    {
245
        $this->method = $method;
246
    }
247
248
    /**
249
     * @return \DateTime|null
250
     */
251
    public function getStartedAt()
252
    {
253
        return $this->startedAt;
254
    }
255
256
    /**
257
     * @param \DateTime $startedAt
258
     */
259
    public function setStartedAt(\DateTime $startedAt)
260
    {
261
        $this->startedAt = $startedAt;
262
    }
263
264
    /**
265
     * @return \DateTime|null
266
     */
267
    public function getFinishedAt()
268
    {
269
        return $this->finishedAt;
270
    }
271
272
    /**
273
     * @param \DateTime $finishedAt
274
     */
275
    public function setFinishedAt($finishedAt)
276
    {
277
        $this->finishedAt = $finishedAt;
278
    }
279
280
    /**
281
     * @return int|null
282
     */
283
    public function getMaxDuration()
284
    {
285
        return $this->maxDuration;
286
    }
287
288
    /**
289
     * @param int|null $maxDuration
290
     */
291
    public function setMaxDuration($maxDuration)
292
    {
293
        $this->maxDuration = $maxDuration;
294
    }
295
296
    /**
297
     * @param $args
298
     */
299
    public function setArgs($args)
300
    {
301
        if (!$this->validateArgs($args)) {
302
            throw new \Exception('Args must not contain object');
303
        }
304
305
        $this->args = $args;
306
    }
307
308
    protected function validateArgs($args)
309
    {
310
        if (is_array($args)) {
311
            foreach ($args as $key => $value) {
312
                if (!$this->validateArgs($value)) {
313
                    return false;
314
                }
315
            }
316
317
            return true;
318
        } else {
319
            return !is_object($args);
320
        }
321
    }
322
323
    /**
324
     * @param bool $batch
325
     */
326
    public function setBatch($batch)
327
    {
328
        $this->batch = $batch;
329
    }
330
331
    /**
332
     * @param int $priority
333
     */
334
    public function setPriority($priority)
335
    {
336
        $this->priority = $priority;
337
    }
338
339
    /**
340
     * @param string $crcHash
341
     */
342
    public function setCrcHash($crcHash)
343
    {
344
        $this->crcHash = $crcHash;
345
    }
346
347
    /**
348
     * @param \DateTime $whenAt
349
     */
350
    public function setWhenAt(\DateTime $whenAt)
351
    {
352
        $this->whenAt = $whenAt;
353
    }
354
355
    /**
356
     * @param \DateTime $createdAt
357
     */
358
    public function setCreatedAt(\DateTime $createdAt)
359
    {
360
        $this->createdAt = $createdAt;
361
    }
362
363
    /**
364
     * @param \DateTime $updatedAt
365
     */
366
    public function setUpdatedAt(\DateTime $updatedAt)
367
    {
368
        $this->updatedAt = $updatedAt;
369
    }
370
371
    /**
372
     * @param JobManagerInterface $jobManager
373
     */
374
    public function setJobManager(JobManagerInterface $jobManager)
375
    {
376
        $this->jobManager = $jobManager;
377
    }
378
379
    protected $worker;
380
381
    public function __construct(Worker $worker = null, $batch = false, $priority = 10, \DateTime $whenAt = null)
382
    {
383
        $this->worker = $worker;
384
        if ($worker) {
385
            $this->jobManager = $worker->getJobManager();
386
            $this->className = get_class($worker);
387
            $this->workerName = $worker->getName();
388
        }
389
390
        $this->whenAt = $whenAt;
391
        $this->batch = $batch ? true : false;
392
        $this->priority = $priority;
393
        $this->status = self::STATUS_NEW;
394
    }
395
396
    public function __call($method, $args)
397
    {
398
        $this->method = $method;
399
        $this->setArgs($args);
400
        $this->createdAt = new \DateTime();
401
        $this->updatedAt = new \DateTime();
402
403
        // Make sure the method exists - job should not be created
404
        if (!method_exists($this->worker, $method)) {
405
            throw new \Exception("{$this->className}->{$method}() does not exist");
406
        }
407
408
        $this->jobManager->save($this);
409
410
        return $this;
411
    }
412
413
    /**
414
     * @return int
415
     */
416
    public function getDelay()
417
    {
418
        return $this->delay;
419
    }
420
421
    /**
422
     * @return Worker
423
     */
424
    public function getWorker()
425
    {
426
        return $this->worker;
427
    }
428
429
    /**
430
     * @param int $delay Delay in seconds
431
     */
432
    public function setDelay($delay)
433
    {
434
        $this->delay = $delay;
435
    }
436
437
    /**
438
     * @param Worker $worker
439
     */
440
    public function setWorker($worker)
441
    {
442
        $this->worker = $worker;
443
    }
444
445
    /**
446
     * @return int
447
     */
448
    public function getElapsed()
449
    {
450
        return $this->elapsed;
451
    }
452
453
    /**
454
     * @param int $elapsed
455
     */
456
    public function setElapsed($elapsed)
457
    {
458
        $this->elapsed = $elapsed;
459
    }
460
461
    /**
462
     * @return mixed
463
     */
464
    public function getRunId()
465
    {
466
        return $this->runId;
467
    }
468
469
    /**
470
     * @param mixed $runId
471
     */
472
    public function setRunId($runId)
473
    {
474
        $this->runId = $runId;
475
    }
476
477
    /**
478
     * @return string A json_encoded version of a queueable version of the object
479
     */
480 View Code Duplication
    public function toMessage()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
481
    {
482
        $arr = array(
483
            'worker' => $this->getWorkerName(),
484
            'args' => $this->getArgs(),
485
            'method' => $this->getMethod(),
486
            'expiresAt' => ($expiresAt = $this->getExpiresAt()) ? $expiresAt->getTimestamp() : null,
487
        );
488
489
        return json_encode($arr);
490
    }
491
492
    /**
493
     * @param string $message a json_encoded version of the object
494
     */
495 View Code Duplication
    public function fromMessage($message)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
496
    {
497
        $arr = json_decode($message, true);
498
        $this->setWorkerName($arr['worker']);
499
        $this->setArgs($arr['args']);
500
        $this->setMethod($arr['method']);
501
        $expiresAt = $arr['expiresAt'];
502
503
        if ($expiresAt) {
504
            $dateTime = new \DateTime();
505
            $dateTime->setTimestamp(intval($expiresAt));
506
            $this->setExpiresAt($dateTime);
507
        }
508
    }
509
}
510