Passed
Push — master ( 73617e...6bb62b )
by Matthew
07:13
created

Worker::batchAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 24
    public function at($time = null, $batch = false, $priority = null)
46
    {
47 24
        $timeU = $time;
48 24
        if (null === $time) {
49 1
            $timeU = Util::getMicrotimeStr();
50 1
            $dateTime = \DateTime::createFromFormat(
51 1
                'U.u',
52 1
                $timeU,
53 1
                new \DateTimeZone(date_default_timezone_get())
54
            );
55
        } else {
56 24
            $localeInfo = localeconv();
57 24
            $decimalPoint = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : '.';
58 24
            $hasDecimalPoint = false !== strpos(strval($time), $decimalPoint);
59 24
            $hasEnDecimalPoint = '.' === $decimalPoint ? $hasDecimalPoint : strpos(strval($time), '.');
60 24
            if (!$hasEnDecimalPoint) {
61 2
                if ($hasDecimalPoint) {
62
                    $dateTime = \DateTime::createFromFormat(
63
                        'U'.$decimalPoint.'u',
64
                        strval($timeU),
65
                        new \DateTimeZone(date_default_timezone_get())
66
                    );
67
                } else {
68 2
                    $dateTime = \DateTime::createFromFormat(
69 2
                        'U',
70 2
                        strval($timeU),
71 2
                        new \DateTimeZone(date_default_timezone_get())
72
                    );
73
                }
74
            } else {
75 22
                $dateTime = \DateTime::createFromFormat(
76 22
                    'U.u',
77 22
                    strval($timeU),
78 22
                    new \DateTimeZone(date_default_timezone_get())
79
                );
80
            }
81
        }
82
83 24
        if (!($dateTime instanceof \DateTime)) {
84 1
            throw new \InvalidArgumentException("Invalid time: $time".($timeU != $time ? " - (micro: $timeU)" : ''));
85
        }
86 24
        $jobClass = $this->jobManager->getJobClass();
87
88 24
        return new $jobClass($this, $batch, $priority, $dateTime);
89
    }
90
91
    /**
92
     * @param int      $delay    Amount of time to delay
93
     * @param int|null $priority
94
     */
95 21
    public function later($delay = 0, $priority = null)
96
    {
97 21
        return $this->batchOrLaterDelay($delay, false, $priority);
98
    }
99
100 22
    public function batchOrLaterDelay($delay = 0, $batch = false, $priority = null)
101
    {
102 22
        $timing = Util::getMicrotimeStr();
103 22
        $parts = explode('.', $timing);
104 22
        $parts[0] = strval(intval($parts[0]) + intval($delay));
105 22
        $localeInfo = localeconv();
106 22
        $decimalPoint = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : '.';
107
108 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

108
        $job = $this->at(/** @scrutinizer ignore-type */ implode($decimalPoint, $parts), $batch, $priority);
Loading history...
109 22
        $job->setDelay($delay);
110
111 22
        return $job;
112
    }
113
114
    /**
115
     * @param int      $delay    Amount of time to delay
116
     * @param int|null $priority
117
     */
118 6
    public function batchLater($delay = 0, $priority = null)
119
    {
120 6
        return $this->batchOrLaterDelay($delay, true, $priority);
121
    }
122
123
    /**
124
     * @param int|null $time
125
     * @param int|null $priority
126
     */
127 1
    public function batchAt($time = null, $priority = null)
128
    {
129 1
        return $this->at($time, true, $priority);
130
    }
131
132
    abstract public function getName();
133
}
134