Worker::at()   C
last analyzed

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
    public const RESULT_SUCCESS = 0;
11
    public 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 111
    public function getJobManager()
36
    {
37 111
        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 25
    public function at($time = null, $batch = false, $priority = null)
49
    {
50 25
        $timeU = $time;
51 25
        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) {
0 ignored issues
show
introduced by
$dateTime is of type DateTime, thus it always evaluated to true.
Loading history...
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 25
            $localeInfo = localeconv();
63 25
            $decimalPoint = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : '.';
64 25
            $hasDecimalPoint = false !== strpos(strval($time), $decimalPoint);
65 25
            $hasEnDecimalPoint = '.' === $decimalPoint ? $hasDecimalPoint : strpos(strval($time), '.');
66 25
            if (!$hasEnDecimalPoint) {
67 2
                if ($hasDecimalPoint) {
68
                    $dateTime = \DateTime::createFromFormat(
69
                        'U'.$decimalPoint.'u',
70
                        strval($timeU)
71
                    );
72
                    if (!$dateTime) {
0 ignored issues
show
introduced by
$dateTime is of type DateTime, thus it always evaluated to true.
Loading history...
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) {
0 ignored issues
show
introduced by
$dateTime is of type DateTime, thus it always evaluated to true.
Loading history...
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 23
                $dateTime = \DateTime::createFromFormat(
88 23
                    'U.u',
89 23
                    strval($timeU)
90
                );
91 23
                if (!$dateTime) {
0 ignored issues
show
introduced by
$dateTime is of type DateTime, thus it always evaluated to true.
Loading history...
92
                    throw new \InvalidArgumentException("Could not create DateTime object from $timeU");
93
                }
94 23
                $dateTime->setTimezone(new \DateTimeZone(date_default_timezone_get()));
95
            }
96
        }
97
98 25
        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 25
        $jobClass = $this->jobManager->getJobClass();
102
103 25
        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 22
    public function later($delay = 0, $priority = null)
111
    {
112 22
        return $this->batchOrLaterDelay($delay, false, $priority);
113
    }
114
115 23
    public function batchOrLaterDelay($delay = 0, $batch = false, $priority = null)
116
    {
117 23
        $timing = Util::getMicrotimeStr();
118 23
        $parts = explode('.', $timing);
119 23
        $parts[0] = strval(intval($parts[0]) + intval($delay));
120 23
        $localeInfo = localeconv();
121 23
        $decimalPoint = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : '.';
122
123 23
        $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 23
        $job->setDelay($delay);
125
126 23
        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