Completed
Pull Request — master (#27)
by Matthew
20:41 queued 16:25
created

RunManager::pruneJobTimings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
eloc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Dtc\QueueBundle\Model;
4
5
use Dtc\QueueBundle\Exception\UnsupportedException;
6
7
class RunManager
8
{
9
    /** @var string */
10
    protected $runClass;
11
12
    public function __construct($runClass)
13
    {
14
        $this->runClass = $runClass;
15
    }
16
17
    /**
18 16
     * @return string
19
     */
20 16
    public function getRunClass()
21 16
    {
22 16
        return $this->runClass;
23 16
    }
24
25
    /**
26
     * @param string $runClass
27
     */
28 5
    public function setRunClass($runClass)
29
    {
30 5
        $this->runClass = $runClass;
31
    }
32
33
    /**
34
     * @param \DateTime $olderThan
35
     *
36
     * @return int Number of archived runs pruned
37
     */
38
    public function pruneArchivedRuns(\DateTime $olderThan)
0 ignored issues
show
Unused Code introduced by
The parameter $olderThan 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

38
    public function pruneArchivedRuns(/** @scrutinizer ignore-unused */ \DateTime $olderThan)

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...
39
    {
40
        throw new UnsupportedException('not supported');
41
    }
42
43
    public function pruneStalledRuns()
44
    {
45
        throw new UnsupportedException('not supported');
46
    }
47
48
    /**
49
     * @param float    $start
50
     * @param Job|null $job
51
     */
52
    public function recordHeartbeat(Run $run, $start, Job $job = null)
53
    {
54
        $jobId = null;
55
        if (null !== $job) {
56
            $jobId = $job->getId();
57
        }
58
59
        $run->setLastHeartbeatAt(new \DateTime());
60
        $run->setCurrentJobId($jobId);
61
        $run->setElapsed(microtime(true) - $start);
62
        $this->persistRun($run);
63
    }
64
65
    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

65
    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

65
    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...
66
    {
67
        // To be overridden
68
    }
69
70
    /**
71
     * @param int $count
72
     */
73
    public function updateProcessed(Run $run, $count)
74
    {
75
        $run->setProcessed($count);
76
        $this->persistRun($run);
77
    }
78
79
    /**
80
     * Sets up the runManager (document / entity persister) if appropriate.
81
     *
82 3
     * @param float    $start
83
     * @param int|null $maxCount
84 3
     * @param int|null $duration
85
     * @param int      $processTimeout
86
     *
87
     * @return Run
88
     */
89
    public function runStart($start, $maxCount = null, $duration = null, $processTimeout = null)
90 3
    {
91
        $runClass = $this->getRunClass();
92 3
        /** @var Run $run */
93 3
        $run = new $runClass();
94 3
        $startDate = \DateTime::createFromFormat('U.u', $start);
95 3
        $run->setLastHeartbeatAt($startDate);
0 ignored issues
show
Bug introduced by
It seems like $startDate can also be of type false; however, parameter $lastHeartbeatAt of Dtc\QueueBundle\Model\Run::setLastHeartbeatAt() does only seem to accept DateTime, maybe add an additional type check? ( Ignorable by Annotation )

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

95
        $run->setLastHeartbeatAt(/** @scrutinizer ignore-type */ $startDate);
Loading history...
96
        $run->setStartedAt($startDate);
0 ignored issues
show
Bug introduced by
It seems like $startDate can also be of type false; however, parameter $startedAt of Dtc\QueueBundle\Model\Run::setStartedAt() does only seem to accept DateTime, maybe add an additional type check? ( Ignorable by Annotation )

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

96
        $run->setStartedAt(/** @scrutinizer ignore-type */ $startDate);
Loading history...
97 3
        if (null !== $maxCount) {
98 3
            $run->setMaxCount($maxCount);
99 3
        }
100 3
        if (null !== $duration) {
101 3
            $run->setDuration($duration);
102
        }
103 1
        $run->setHostname(gethostname());
104
        $run->setPid(getmypid());
105
        $run->setProcessed(0);
106 1
        $run->setProcessTimeout($processTimeout);
107
        $this->persistRun($run);
108
109
        return $run;
110
    }
111 3
112
    /**
113 3
     * @param Run      $run
114 3
     * @param int|null $start
115 3
     */
116
    public function runStop(Run $run, $start)
117
    {
118
        $end = microtime(true);
119
        $endedTime = \DateTime::createFromFormat('U.u', $end);
120
        if ($endedTime) {
121
            $run->setEndedAt($endedTime);
122
        }
123
        $run->setElapsed($end - $start);
124
        $this->persistRun($run, 'remove');
125
    }
126
}
127