Completed
Push — master ( 4317fb...b1ebb8 )
by Matthew
04:36 queued 02:27
created

RunManager::recordHeartbeat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 3
crap 6
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 10
    public function __construct($runClass)
13
    {
14 10
        $this->runClass = $runClass;
15 10
    }
16
17
    /**
18
     * @return string
19
     */
20
    public function getRunClass()
21
    {
22
        return $this->runClass;
23
    }
24
25
    /**
26
     * @param string $runClass
27
     */
28
    public function setRunClass($runClass)
29
    {
30
        $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)
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')
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
     * @param float    $start
83
     * @param int|null $maxCount
84
     * @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
    {
91
        $runClass = $this->getRunClass();
92
        /** @var Run $run */
93
        $run = new $runClass();
94
        $startDate = \DateTime::createFromFormat('U.u', $start);
95
        $run->setLastHeartbeatAt($startDate);
0 ignored issues
show
Security Bug introduced by
It seems like $startDate defined by \DateTime::createFromFormat('U.u', $start) on line 94 can also be of type false; however, Dtc\QueueBundle\Model\Run::setLastHeartbeatAt() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
96
        $run->setStartedAt($startDate);
0 ignored issues
show
Security Bug introduced by
It seems like $startDate defined by \DateTime::createFromFormat('U.u', $start) on line 94 can also be of type false; however, Dtc\QueueBundle\Model\Run::setStartedAt() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
97
        if (null !== $maxCount) {
98
            $run->setMaxCount($maxCount);
99
        }
100
        if (null !== $duration) {
101
            $run->setDuration($duration);
102
        }
103
        $run->setHostname(gethostname());
104
        $run->setPid(getmypid());
105
        $run->setProcessed(0);
106
        $run->setProcessTimeout($processTimeout);
107
        $this->persistRun($run);
108
109
        return $run;
110
    }
111
112
    /**
113
     * @param Run      $run
114
     * @param int|null $start
115
     */
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