Completed
Push — master ( 90bd67...742cd1 )
by Matthew
18:25
created

Worker::at()   C

Complexity

Conditions 12
Paths 39

Size

Total Lines 56
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 12.2812

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 40
nc 39
nop 3
dl 0
loc 56
ccs 28
cts 32
cp 0.875
crap 12.2812
rs 6.9666
c 1
b 0
f 0

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

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