Completed
Push — master ( 16d212...b7b31c )
by Matthew
07:34 queued 05:11
created

RunManager::getJobTimingClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
    /** @var string */
13
    protected $jobTimingClass;
14
15
    /** @var bool */
16
    protected $recordTimings;
17
18 16
    public function __construct($runClass, $jobTimingClass, $recordTimings)
19
    {
20 16
        $this->runClass = $runClass;
21 16
        $this->jobTimingClass = $jobTimingClass;
22 16
        $this->recordTimings = $recordTimings;
23 16
    }
24
25
    /**
26
     * @return string
27
     */
28 5
    public function getRunClass()
29
    {
30 5
        return $this->runClass;
31
    }
32
33
    /**
34
     * @param string $runClass
35
     */
36
    public function setRunClass($runClass)
37
    {
38
        $this->runClass = $runClass;
39
    }
40
41
    /**
42
     * @param \DateTime $olderThan
43
     *
44
     * @return int Number of archived runs pruned
45
     */
46
    public function pruneArchivedRuns(\DateTime $olderThan)
47
    {
48
        throw new UnsupportedException('not supported');
49
    }
50
51
    /**
52
     * @param \DateTime $olderThan
53
     *
54
     * @return int Number of archived runs pruned
55
     */
56
    public function pruneJobTimings(\DateTime $olderThan)
57
    {
58
        throw new UnsupportedException('not supported');
59
    }
60
61
    public function pruneStalledRuns()
62
    {
63
        throw new UnsupportedException('not supported');
64
    }
65
66
    /**
67
     * @return null|string
68
     */
69
    public function getJobTimingClass()
70
    {
71
        return $this->jobTimingClass;
72
    }
73
74
    /**
75
     * @param string $jobTimingClass
76
     */
77
    public function setJobTimingClass($jobTimingClass)
78
    {
79
        $this->jobTimingClass = $jobTimingClass;
80
    }
81
82 3
    public function recordJobRun(Job $job)
83
    {
84 3
    }
85
86
    /**
87
     * @param float    $start
88
     * @param Job|null $job
89
     */
90 3
    public function recordHeartbeat(Run $run, $start, Job $job = null)
91
    {
92 3
        $jobId = null;
93 3
        if (null !== $job) {
94 3
            $jobId = $job->getId();
95 3
        }
96
97 3
        $run->setLastHeartbeatAt(new \DateTime());
98 3
        $run->setCurrentJobId($jobId);
99 3
        $run->setElapsed(microtime(true) - $start);
100 3
        $this->persistRun($run);
101 3
    }
102
103 1
    protected function persistRun(Run $run, $action = 'persist')
104
    {
105
        // To be overridden
106 1
    }
107
108
    /**
109
     * @param int $count
110
     */
111 3
    public function updateProcessed(Run $run, $count)
112
    {
113 3
        $run->setProcessed($count);
114 3
        $this->persistRun($run);
115 3
    }
116
117
    /**
118
     * Sets up the runManager (document / entity persister) if appropriate.
119
     *
120
     * @param float    $start
121
     * @param int|null $maxCount
122
     * @param int|null $duration
123
     * @param int      $processTimeout
124
     *
125
     * @return Run
126
     */
127 3
    public function runStart($start, $maxCount = null, $duration = null, $processTimeout = null)
128
    {
129 3
        $runClass = $this->getRunClass();
130
        /** @var Run $run */
131 3
        $run = new $runClass();
132 3
        $startDate = \DateTime::createFromFormat('U.u', $start);
133 3
        $run->setLastHeartbeatAt($startDate);
0 ignored issues
show
Security Bug introduced by
It seems like $startDate defined by \DateTime::createFromFormat('U.u', $start) on line 132 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...
134 3
        $run->setStartedAt($startDate);
0 ignored issues
show
Security Bug introduced by
It seems like $startDate defined by \DateTime::createFromFormat('U.u', $start) on line 132 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...
135 3
        if (null !== $maxCount) {
136 3
            $run->setMaxCount($maxCount);
137 3
        }
138 3
        if (null !== $duration) {
139 2
            $run->setDuration($duration);
140 2
        }
141 3
        $run->setHostname(gethostname());
142 3
        $run->setPid(getmypid());
143 3
        $run->setProcessed(0);
144 3
        $run->setProcessTimeout($processTimeout);
145 3
        $this->persistRun($run);
146
147 3
        return $run;
148
    }
149
150
    /**
151
     * @param Run      $run
152
     * @param int|null $start
153
     */
154 3
    public function runStop(Run $run, $start)
155
    {
156 3
        $end = microtime(true);
157 3
        $endedTime = \DateTime::createFromFormat('U.u', $end);
158 3
        if ($endedTime) {
159 3
            $run->setEndedAt($endedTime);
160 3
        }
161 3
        $run->setElapsed($end - $start);
162 3
        $this->persistRun($run, 'remove');
163 3
    }
164
}
165