Completed
Pull Request — master (#40)
by Matthew
12:15 queued 04:06
created

StallableJobManager::getAllStatuses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Dtc\QueueBundle\Manager;
4
5
use Dtc\QueueBundle\Model\RetryableJob;
6
use Dtc\QueueBundle\Model\Job;
7
use Dtc\QueueBundle\Model\StallableJob;
8
9
abstract class StallableJobManager extends PriorityJobManager
10
{
11
    protected $defaultMaxStalls;
12
13
    abstract protected function stallableSave(StallableJob $job);
14
15
    abstract public function pruneStalledJobs($workerName = null, $method = null, callable $progressCallback = null);
16
17
    abstract public function resetStalledJobs($workerName = null, $method = null, callable $progressCallback = null);
18
19 4
    public static function getAllStatuses()
20
    {
21 4
        $statuses = parent::getAllStatuses();
22 4
        $statuses[StallableJob::STATUS_STALLED] = 0;
23 4
        $statuses[StallableJob::STATUS_MAX_STALLS] = 0;
24
25 4
        return $statuses;
26
    }
27
28 43
    protected function prioritySave(Job $job)
29
    {
30 43
        if (!$job instanceof StallableJob) {
31
            throw new \InvalidArgumentException('Job is not instanceof '.StallableJob::class);
32
        }
33
34 43
        if (!$job->getId()) {
35 43
            if ($job instanceof StallableJob) {
36 43
                $this->setStallableJobDefaults($job);
37
            }
38
        }
39
40 43
        return $this->stallableSave($job);
41
    }
42
43
    /**
44
     * @param StallableJob $job
45
     * @param $retry true if retry
46
     *
47
     * @return bool true if retry
48
     */
49
    abstract protected function stallableSaveHistory(StallableJob $job, $retry);
50
51
    /**
52
     * @return bool true if retry
53
     *
54
     * @param Job $job
55
     */
56 8
    protected function retryableSaveHistory(RetryableJob $job, $retry)
57
    {
58 8
        if (!$job instanceof StallableJob) {
59
            throw new \InvalidArgumentException('job not instance of '.StallableJob::class);
60
        }
61
62 8
        if ($retry) {
63 4
            return $this->stallableSaveHistory($job, $retry);
64
        }
65
66 8
        if (StallableJob::STATUS_STALLED === $job->getStatus()) {
67 2
            return $this->stallableSaveHistory($job, $this->updateJobStalled($job));
68
        }
69
70 6
        return $this->stallableSaveHistory($job, false);
71
    }
72
73
    /**
74
     * @param StallableJob $job
75
     *
76
     * @return bool false if
77
     */
78 2
    private function updateJobStalled(StallableJob $job)
79
    {
80 2
        return $this->updateJobMax($job, 'Stalls', StallableJob::STATUS_MAX_STALLS, true);
81
    }
82
83 43
    protected function setStallableJobDefaults(StallableJob $job)
84
    {
85 43
        if (null === $job->getMaxStalls()) {
86 43
            $job->setMaxStalls($this->defaultMaxStalls);
87
        }
88 43
    }
89
90
    /**
91
     * @return int|null
92
     */
93 2
    public function getDefaultMaxStalls()
94
    {
95 2
        return $this->defaultMaxStalls;
96
    }
97
98
    /**
99
     * @param int|null $defaultMaxStalls
100
     */
101 2
    public function setDefaultMaxStalls($defaultMaxStalls)
102
    {
103 2
        $this->defaultMaxStalls = $defaultMaxStalls;
104 2
    }
105
}
106