Completed
Pull Request — master (#30)
by Matthew
48:21 queued 02:09
created

Worker   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.55%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 95
ccs 29
cts 31
cp 0.9355
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setJobManager() 0 4 1
A getJobManager() 0 4 1
A getCurrentJob() 0 4 1
A later() 0 4 1
A batchOrLaterDelay() 0 7 1
getName() 0 1 ?
A setCurrentJob() 0 4 1
A batchLater() 0 4 1
A batchAt() 0 4 1
B at() 0 17 5
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 Worker
9
{
10
    const RESULT_SUCCESS = 0;
11
    const RESULT_FAILURE = 1;
12
13
    /** @var JobManagerInterface */
14
    private $jobManager;
15
    private $currentJob;
16
17 6
    public function setCurrentJob(BaseJob $job)
18
    {
19 6
        $this->currentJob = $job;
20 6
    }
21
22
    public function getCurrentJob()
23
    {
24
        return $this->currentJob;
25
    }
26
27
    /**
28
     * @param JobManagerInterface $jobManager
29
     */
30 32
    public function setJobManager(JobManagerInterface $jobManager)
31
    {
32 32
        $this->jobManager = $jobManager;
33 32
    }
34
35
    /**
36
     * @return
37
     */
38 94
    public function getJobManager()
39
    {
40 94
        return $this->jobManager;
41
    }
42
43
    /**
44
     * @param int|null $time
45
     * @param bool     $batch
46
     * @param int|null $priority
47
     */
48 22
    public function at($time = null, $batch = false, $priority = null)
49
    {
50 22
        $timeU = $time;
51 22
        if (null === $time) {
52 1
            $timeU = Util::getMicrotimeStr();
53 22
        } elseif (false === strpos(strval($time), '.')) {
54 2
            $timeU = strval($time).'.000000';
55
        }
56
57 22
        $dateTime = \DateTime::createFromFormat('U.u', (string) $timeU);
58 22
        if (!$dateTime) {
59 1
            throw new \InvalidArgumentException("Invalid time: $time".($timeU != $time ? " - (micro: $timeU)" : ''));
60
        }
61 22
        $jobClass = $this->jobManager->getJobClass();
62
63 22
        return new $jobClass($this, $batch, $priority, $dateTime);
64
    }
65
66
    /**
67
     * @param int      $delay    Amount of time to delay
68
     * @param int|null $priority
69
     */
70 19
    public function later($delay = 0, $priority = null)
71
    {
72 19
        return $this->batchOrLaterDelay($delay, false, $priority);
73
    }
74
75 20
    public function batchOrLaterDelay($delay = 0, $batch = false, $priority = null)
76
    {
77 20
        $job = $this->at(microtime(true) + $delay, $batch, $priority);
78 20
        $job->setDelay($delay);
79
80 20
        return $job;
81
    }
82
83
    /**
84
     * @param int      $delay    Amount of time to delay
85
     * @param int|null $priority
86
     */
87 5
    public function batchLater($delay = 0, $priority = null)
88
    {
89 5
        return $this->batchOrLaterDelay($delay, true, $priority);
90
    }
91
92
    /**
93
     * @param int|null $time
94
     * @param int|null $priority
95
     */
96 1
    public function batchAt($time = null, $priority = null)
97
    {
98 1
        return $this->at($time, true, $priority);
99
    }
100
101
    abstract public function getName();
102
}
103