Passed
Push — master ( e3298a...32f3de )
by Matthew
08:05
created

RunManager::runStart()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 4
nop 4
dl 0
loc 21
ccs 16
cts 16
cp 1
crap 3
rs 9.7666
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
        }
70
71 3
        $heartbeat = microtime(true);
72 3
        $run->setLastHeartbeatAt(Util::getMicrotimeFloatDateTime($heartbeat));
73 3
        $run->setCurrentJobId($jobId);
74 3
        $run->setElapsed($heartbeat - $start);
75 3
        $this->persistRun($run);
76 3
    }
77
78
    /**
79
     * @param Run    $run
80
     * @param string $action
81
     */
82 5
    protected function persistRun(Run $run, $action = 'persist')
0 ignored issues
show
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

82
    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...
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

82
    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...
83
    {
84
        // To be overridden
85 5
    }
86
87
    /**
88
     * @param int $count
89
     */
90 3
    public function updateProcessed(Run $run, $count)
91
    {
92 3
        $run->setProcessed($count);
93 3
        $this->persistRun($run);
94 3
    }
95
96
    /**
97
     * Sets up the runManager (document / entity persister) if appropriate.
98
     *
99
     * @param float    $start
100
     * @param int|null $maxCount
101
     * @param int|null $duration
102
     * @param int      $processTimeout
103
     *
104
     * @return Run
105
     */
106 5
    public function runStart($start, $maxCount = null, $duration = null, $processTimeout = null)
107
    {
108 5
        $runClass = $this->getRunClass();
109
        /** @var Run $run */
110 5
        $run = new $runClass();
111 5
        $startDate = Util::getMicrotimeFloatDateTime($start);
112 5
        $run->setLastHeartbeatAt($startDate);
113 5
        $run->setStartedAt($startDate);
114 5
        if (null !== $maxCount) {
115 3
            $run->setMaxCount($maxCount);
116
        }
117 5
        if (null !== $duration) {
118 2
            $run->setDuration($duration);
119
        }
120 5
        $run->setHostname(gethostname());
121 5
        $run->setPid(getmypid());
122 5
        $run->setProcessed(0);
123 5
        $run->setProcessTimeout($processTimeout);
124 5
        $this->persistRun($run);
125
126 5
        return $run;
127
    }
128
129
    /**
130
     * @param Run      $run
131
     * @param int|null $start
132
     */
133 5
    public function runStop(Run $run, $start)
134
    {
135 5
        $end = microtime(true);
136 5
        $run->setEndedAt(Util::getMicrotimeFloatDateTime($end));
137 5
        $run->setElapsed($end - $start);
138 5
        $this->persistRun($run, 'remove');
139 5
    }
140
}
141