Passed
Push — master ( 26ea1a...43abdf )
by Matthew
07:12
created

Worker   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 91.49%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 44
c 2
b 0
f 0
dl 0
loc 116
ccs 43
cts 47
cp 0.9149
rs 10
wmc 17

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setCurrentJob() 0 3 1
A getCurrentJob() 0 3 1
A getJobManager() 0 3 1
A setJobManager() 0 3 1
A batchOrLaterDelay() 0 12 2
B at() 0 32 8
A batchLater() 0 3 1
A later() 0 3 1
A batchAt() 0 3 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 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 31
    public function setJobManager(JobManagerInterface $jobManager)
31
    {
32 31
        $this->jobManager = $jobManager;
33 31
    }
34
35
    /**
36
     * @return
37
     */
38 105
    public function getJobManager()
39
    {
40 105
        return $this->jobManager;
41
    }
42
43
    /**
44
     * @param int|null $time
45
     * @param bool     $batch
46
     * @param int|null $priority
47
     */
48 24
    public function at($time = null, $batch = false, $priority = null)
49
    {
50 24
        $timeU = $time;
51 24
        if (null === $time) {
52 1
            $timeU = Util::getMicrotimeStr();
53 1
            $dateTime = \DateTime::createFromFormat('U.u', $timeU,
54 1
                new \DateTimeZone(date_default_timezone_get()));
55
        } else {
56 24
            $localeInfo = localeconv();
57 24
            $decimalPoint = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : '.';
58 24
            $hasDecimalPoint = strpos(strval($time), $decimalPoint) !== false;
59 24
            $hasEnDecimalPoint = $decimalPoint === '.' ? $hasDecimalPoint : strpos(strval($time), '.');
60 24
            if (!$hasEnDecimalPoint) {
61 2
                if ($hasDecimalPoint) {
62
                    $dateTime = \DateTime::createFromFormat('U' . $decimalPoint . 'u', strval($timeU),
63
                        new \DateTimeZone(date_default_timezone_get()));
64
                } else {
65 2
                    $dateTime = \DateTime::createFromFormat('U', strval($timeU),
66 2
                        new \DateTimeZone(date_default_timezone_get()));
67
                }
68
            } else {
69 22
                $dateTime = \DateTime::createFromFormat('U.u', strval($timeU),
70 22
                    new \DateTimeZone(date_default_timezone_get()));
71
            }
72
        }
73
74 24
        if (!($dateTime instanceof \DateTime)) {
75 1
            throw new \InvalidArgumentException("Invalid time: $time".($timeU != $time ? " - (micro: $timeU)" : ''));
76
        }
77 24
        $jobClass = $this->jobManager->getJobClass();
78
79 24
        return new $jobClass($this, $batch, $priority, $dateTime);
80
    }
81
82
    /**
83
     * @param int      $delay    Amount of time to delay
84
     * @param int|null $priority
85
     */
86 21
    public function later($delay = 0, $priority = null)
87
    {
88 21
        return $this->batchOrLaterDelay($delay, false, $priority);
89
    }
90
91 22
    public function batchOrLaterDelay($delay = 0, $batch = false, $priority = null)
92
    {
93 22
        $timing = Util::getMicrotimeStr();
94 22
        $parts = explode('.', $timing);
95 22
        $parts[0] = strval(intval($parts[0]) + intval($delay));
96 22
        $localeInfo = localeconv();
97 22
        $decimalPoint = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : '.';
98
99 22
        $job = $this->at(implode($decimalPoint, $parts), $batch, $priority);
0 ignored issues
show
Bug introduced by
implode($decimalPoint, $parts) of type string is incompatible with the type integer|null expected by parameter $time of Dtc\QueueBundle\Model\Worker::at(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

99
        $job = $this->at(/** @scrutinizer ignore-type */ implode($decimalPoint, $parts), $batch, $priority);
Loading history...
100 22
        $job->setDelay($delay);
101
102 22
        return $job;
103
    }
104
105
    /**
106
     * @param int      $delay    Amount of time to delay
107
     * @param int|null $priority
108
     */
109 6
    public function batchLater($delay = 0, $priority = null)
110
    {
111 6
        return $this->batchOrLaterDelay($delay, true, $priority);
112
    }
113
114
    /**
115
     * @param int|null $time
116
     * @param int|null $priority
117
     */
118 1
    public function batchAt($time = null, $priority = null)
119
    {
120 1
        return $this->at($time, true, $priority);
121
    }
122
123
    abstract public function getName();
124
}
125