Completed
Push — master ( 31d53b...10a888 )
by Matthew
75:43 queued 71:47
created

Worker::at()   C

Complexity

Conditions 12
Paths 39

Size

Total Lines 56
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 13.912

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 12
eloc 40
c 2
b 1
f 0
nc 39
nop 3
dl 0
loc 56
ccs 29
cts 38
cp 0.7632
crap 13.912
rs 6.9666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 31
    public function setJobManager(JobManagerInterface $jobManager)
28
    {
29 31
        $this->jobManager = $jobManager;
30 31
    }
31
32
    /**
33
     * @return
34
     */
35 105
    public function getJobManager()
36
    {
37 105
        return $this->jobManager;
38
    }
39
40
    /**
41
     * @param int|null $time
42
     * @param bool     $batch
43
     * @param int|null $priority
44
     *
45
     * @throws \Exception
46
     * @throws \InvalidArgumentException
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(
54 1
                'U.u',
55 1
                $timeU
56
            );
57 1
            if (!$dateTime) {
58
                throw new \Exception("Could not create DateTime object from $timeU");
59
            }
60 1
            $dateTime->setTimezone(new \DateTimeZone(date_default_timezone_get()));
61
        } else {
62 24
            $localeInfo = localeconv();
63 24
            $decimalPoint = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : '.';
64 24
            $hasDecimalPoint = false !== strpos(strval($time), $decimalPoint);
65 24
            $hasEnDecimalPoint = '.' === $decimalPoint ? $hasDecimalPoint : strpos(strval($time), '.');
66 24
            if (!$hasEnDecimalPoint) {
67 2
                if ($hasDecimalPoint) {
68
                    $dateTime = \DateTime::createFromFormat(
69
                        'U'.$decimalPoint.'u',
70
                        strval($timeU)
71
                    );
72
                    if (!$dateTime) {
73
                        throw new \InvalidArgumentException("Could not create DateTime object from $timeU");
74
                    }
75
                    $dateTime->setTimezone(new \DateTimeZone(date_default_timezone_get()));
76
                } else {
77 2
                    $dateTime = \DateTime::createFromFormat(
78 2
                        'U',
79 2
                        strval($timeU)
80
                    );
81 2
                    if (!$dateTime) {
82 1
                        throw new \InvalidArgumentException("Could not create DateTime object from $timeU");
83
                    }
84 2
                    $dateTime->setTimezone(new \DateTimeZone(date_default_timezone_get()));
85
                }
86
            } else {
87 22
                $dateTime = \DateTime::createFromFormat(
88 22
                    'U.u',
89 22
                    strval($timeU)
90
                );
91 22
                if (!$dateTime) {
92
                    throw new \InvalidArgumentException("Could not create DateTime object from $timeU");
93
                }
94 22
                $dateTime->setTimezone(new \DateTimeZone(date_default_timezone_get()));
95
            }
96
        }
97
98 24
        if (!($dateTime instanceof \DateTime)) {
0 ignored issues
show
introduced by
$dateTime is always a sub-type of DateTime.
Loading history...
99
            throw new \InvalidArgumentException("Invalid time: $time".($timeU != $time ? " - (micro: $timeU)" : ''));
100
        }
101 24
        $jobClass = $this->jobManager->getJobClass();
102
103 24
        return new $jobClass($this, $batch, $priority, $dateTime);
104
    }
105
106
    /**
107
     * @param int      $delay    Amount of time to delay
108
     * @param int|null $priority
109
     */
110 21
    public function later($delay = 0, $priority = null)
111
    {
112 21
        return $this->batchOrLaterDelay($delay, false, $priority);
113
    }
114
115 22
    public function batchOrLaterDelay($delay = 0, $batch = false, $priority = null)
116
    {
117 22
        $timing = Util::getMicrotimeStr();
118 22
        $parts = explode('.', $timing);
119 22
        $parts[0] = strval(intval($parts[0]) + intval($delay));
120 22
        $localeInfo = localeconv();
121 22
        $decimalPoint = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : '.';
122
123 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

123
        $job = $this->at(/** @scrutinizer ignore-type */ implode($decimalPoint, $parts), $batch, $priority);
Loading history...
124 22
        $job->setDelay($delay);
125
126 22
        return $job;
127
    }
128
129
    /**
130
     * @param int      $delay    Amount of time to delay
131
     * @param int|null $priority
132
     */
133 6
    public function batchLater($delay = 0, $priority = null)
134
    {
135 6
        return $this->batchOrLaterDelay($delay, true, $priority);
136
    }
137
138
    /**
139
     * @param int|null $time
140
     * @param int|null $priority
141
     */
142 1
    public function batchAt($time = null, $priority = null)
143
    {
144 1
        return $this->at($time, true, $priority);
145
    }
146
147
    abstract public function getName();
148
}
149