Completed
Push — master ( 6c3722...239355 )
by Matthew
19:49 queued 17:50
created

Worker::batchOrLaterDelay()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 2
1
<?php
2
3
namespace Dtc\QueueBundle\Model;
4
5
abstract class Worker
6
{
7
    protected $jobManager;
8
    protected $jobClass;
9
    protected $job;
10
11
    /**
12
     * @return string
13
     */
14
    public function getJobClass()
15
    {
16
        return $this->jobClass;
17
    }
18
19
    /**
20
     * @param string $jobClass
21
     */
22
    public function setJobClass($jobClass)
23
    {
24
        $this->jobClass = $jobClass;
25
    }
26
27
    /**
28
     * @param JobManagerInterface $jobManager
29
     */
30
    public function setJobManager(JobManagerInterface $jobManager)
31
    {
32
        $this->jobManager = $jobManager;
33
    }
34
35
    /**
36
     * @return
37
     */
38 4
    public function getJobManager()
39
    {
40 4
        return $this->jobManager;
41
    }
42
43
    /**
44
     * @param int|null $time
45
     * @param bool     $batch
46
     * @param int|null $priority
47
     */
48
    public function at($time = null, $batch = false, $priority = null)
49
    {
50
        if (null === $time) {
51
            $time = time();
52
        }
53
54
        if ($time) {
55
            $dateTime = new \DateTime();
56
            $dateTime->setTimestamp($time);
57
        } else {
58
            $dateTime = null;
59
        }
60
61
        return new $this->jobClass($this, $batch, $priority, $dateTime);
62
    }
63
64
    /**
65
     * @param int      $delay    Amount of time to delay
66
     * @param int|null $priority
67
     */
68
    public function later($delay = 0, $priority = null)
69
    {
70
        return $this->batchOrLaterDelay($delay, false, $priority);
71
    }
72
73
    public function batchOrLaterDelay($delay = 0, $batch = false, $priority = null)
74
    {
75
        $job = $this->at(time() + $delay, $batch, $priority);
76
        $job->setDelay($delay);
77
78
        return $job;
79
    }
80
81
    /**
82
     * @param int      $delay    Amount of time to delay
83
     * @param int|null $priority
84
     */
85
    public function batchLater($delay = 0, $priority = null)
86
    {
87
        return $this->batchOrLaterDelay($delay, true, $priority);
88
    }
89
90
    /**
91
     * @param int|null $time
92
     * @param int|null $priority
93
     */
94
    public function batchAt($time = null, $priority = null)
95
    {
96
        return $this->at($time, true, $priority);
97
    }
98
99
    abstract public function getName();
100
}
101