Passed
Push — master ( 1afe5c...e9653b )
by Matthew
11:56 queued 05:32
created

RunManager::runStart()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4.002

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 5
nop 4
dl 0
loc 24
ccs 19
cts 20
cp 0.95
crap 4.002
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace Dtc\QueueBundle\Manager;
4
5
use Dtc\QueueBundle\Exception\UnsupportedException;
6
use Dtc\QueueBundle\Model\Job;
7
use Dtc\QueueBundle\Model\Run;
8
use Dtc\QueueBundle\Util\Util;
9
10
class RunManager
11
{
12
    /** @var string */
13
    protected $runClass;
14
15 41
    public function __construct($runClass)
16
    {
17 41
        $this->runClass = $runClass;
18 41
    }
19
20
    /**
21
     * @return string
22
     */
23 22
    public function getRunClass()
24
    {
25 22
        return $this->runClass;
26
    }
27
28
    /**
29
     * @param string $runClass
30
     */
31 1
    public function setRunClass($runClass)
32
    {
33 1
        $this->runClass = $runClass;
34 1
    }
35
36
    /**
37
     * @param \DateTime $olderThan
38
     *
39
     * @return int Number of archived runs pruned
40
     *
41
     * @throws UnsupportedException
42
     */
43 1
    public function pruneArchivedRuns(\DateTime $olderThan)
44
    {
45 1
        throw new UnsupportedException('not supported - '.$olderThan->getTimestamp());
46
    }
47
48
    /**
49
     * Prunes stalled runs.
50
     *
51
     * @return int Number of stalled runs pruned
52
     *
53
     * @throws UnsupportedException
54
     */
55 1
    public function pruneStalledRuns()
56
    {
57 1
        throw new UnsupportedException('not supported');
58
    }
59
60
    /**
61
     * @param float    $start
62
     * @param Job|null $job
63
     */
64 3
    public function recordHeartbeat(Run $run, $start, Job $job = null)
65
    {
66 3
        $jobId = null;
67 3
        if (null !== $job) {
68 3
            $jobId = $job->getId();
69 3
        }
70
71 3
        $run->setLastHeartbeatAt(Util::getMicrotimeDateTime());
72 3
        $run->setCurrentJobId($jobId);
73 3
        $run->setElapsed(microtime(true) - $start);
74 3
        $this->persistRun($run);
75 3
    }
76
77
    /**
78
     * @param Run    $run
79
     * @param string $action
80
     */
81 5
    protected function persistRun(Run $run, $action = 'persist')
0 ignored issues
show
Unused Code introduced by
The parameter $action is not used and could be removed. ( Ignorable by Annotation )

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

81
    protected function persistRun(Run $run, /** @scrutinizer ignore-unused */ $action = 'persist')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $run is not used and could be removed. ( Ignorable by Annotation )

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

81
    protected function persistRun(/** @scrutinizer ignore-unused */ Run $run, $action = 'persist')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
    {
83
        // To be overridden
84 5
    }
85
86
    /**
87
     * @param int $count
88
     */
89 3
    public function updateProcessed(Run $run, $count)
90
    {
91 3
        $run->setProcessed($count);
92 3
        $this->persistRun($run);
93 3
    }
94
95
    /**
96
     * Sets up the runManager (document / entity persister) if appropriate.
97
     *
98
     * @param float    $start
99
     * @param int|null $maxCount
100
     * @param int|null $duration
101
     * @param int      $processTimeout
102
     *
103
     * @return Run
104
     */
105 5
    public function runStart($start, $maxCount = null, $duration = null, $processTimeout = null)
106
    {
107 5
        $runClass = $this->getRunClass();
108
        /** @var Run $run */
109 5
        $run = new $runClass();
110 5
        $startDate = \DateTime::createFromFormat('U.u', $formattedStart = number_format($start, 6, '.', ''), new \DateTimeZone(date_default_timezone_get()));
111 5
        if (false === $startDate) {
112
            throw new \RuntimeException("Could not create date from $start formatted as $formattedStart");
113
        }
114 5
        $run->setLastHeartbeatAt($startDate);
115 5
        $run->setStartedAt($startDate);
116 5
        if (null !== $maxCount) {
117 3
            $run->setMaxCount($maxCount);
118 3
        }
119 5
        if (null !== $duration) {
120 2
            $run->setDuration($duration);
121 2
        }
122 5
        $run->setHostname(gethostname());
123 5
        $run->setPid(getmypid());
124 5
        $run->setProcessed(0);
125 5
        $run->setProcessTimeout($processTimeout);
126 5
        $this->persistRun($run);
127
128 5
        return $run;
129
    }
130
131
    /**
132
     * @param Run      $run
133
     * @param int|null $start
134
     */
135 5
    public function runStop(Run $run, $start)
136
    {
137 5
        $end = microtime(true);
138 5
        $endedTime = \DateTime::createFromFormat('U.u', $end, new \DateTimeZone(date_default_timezone_get()));
139 5
        if ($endedTime) {
140 5
            $run->setEndedAt($endedTime);
141 5
        }
142 5
        $run->setElapsed($end - $start);
143 5
        $this->persistRun($run, 'remove');
144 5
    }
145
}
146