Completed
Pull Request — master (#30)
by Matthew
23:29 queued 08:10
created

PriorityJobManager::getMaxPriority()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Dtc\QueueBundle\Manager;
4
5
use Dtc\QueueBundle\Exception\PriorityException;
6
use Dtc\QueueBundle\Model\RetryableJob;
7
use Dtc\QueueBundle\Model\Job;
8
9
abstract class PriorityJobManager extends RetryableJobManager
10
{
11
    const PRIORITY_ASC = 'asc';
12
    const PRIORITY_DESC = 'desc';
13
14
    protected $maxPriority;
15
    protected $priorityDirection = self::PRIORITY_DESC;
16
17
    /**
18
     * @return mixed
19
     */
20 38
    public function getMaxPriority()
21
    {
22 38
        return $this->maxPriority;
23
    }
24
25
    /**
26
     * @param mixed $maxPriority
27
     */
28 20
    public function setMaxPriority($maxPriority)
29
    {
30 20
        $this->maxPriority = $maxPriority;
31 20
    }
32
33
    /**
34
     * @return mixed
35
     */
36 19
    public function getPriorityDirection()
37
    {
38 19
        return $this->priorityDirection;
39
    }
40
41
    /**
42
     * @param mixed $priorityDirection
43
     */
44 24
    public function setPriorityDirection($priorityDirection)
45
    {
46 24
        $this->priorityDirection = $priorityDirection;
47 24
    }
48
49 69
    protected function validatePriority($priority)
50
    {
51 69
        if (null === $priority) {
52 64
            return;
53
        }
54
55 19
        if (!ctype_digit(strval($priority))) {
56
            throw new PriorityException("Priority ($priority) needs to be a positive integer");
57
        }
58 19
        if (strval(intval($priority)) !== strval($priority)) {
59
            throw new PriorityException("Priority ($priority) needs to be less than ".PHP_INT_MAX);
60
        }
61 19
        $maxPriority = $this->getMaxPriority();
62 19
        if (intval($priority) > $maxPriority) {
63 8
            throw new PriorityException("Priority ($priority) must be less than ".$maxPriority);
64
        }
65 19
    }
66
67
    /**
68
     * Returns the prioirty in ASCENDING order regardless of the User's choice of direction
69
     *   (for storing RabbitMQ, Mysql, others).
70
     *
71
     * @param $priority
72
     *
73
     * @return mixed
74
     */
75 69
    protected function calculatePriority($priority)
76
    {
77 69
        if (null === $priority) {
78 64
            return $priority;
79
        }
80 19
        if (self::PRIORITY_DESC === $this->priorityDirection) {
81 19
            $priority = $this->maxPriority - $priority;
82
        }
83
84 19
        return $priority;
85
    }
86
87
    protected function findHigherPriority($priority1, $priority2)
88
    {
89
        if (null === $priority1) {
90
            return $priority2;
91
        }
92
        if (null === $priority2) {
93
            return $priority1;
94
        }
95
96
        if (self::PRIORITY_DESC === $this->priorityDirection) {
97
            return min($priority1, $priority2);
98
        } else {
99
            return max($priority1, $priority2);
100
        }
101
    }
102
103
    abstract protected function prioritySave(Job $job);
104
105
    /**
106
     * @param Job $job
107
     *
108
     * @throws PriorityException
109
     */
110 69
    protected function retryableSave(RetryableJob $job)
111
    {
112 69
        $this->validatePriority($job->getPriority());
113 69
        if (!$job->getId()) { // An unsaved job needs it's priority potentially adjusted
114 69
            $job->setPriority($this->calculatePriority($job->getPriority()));
115
        }
116
117 69
        $result = $this->prioritySave($job);
118
119 69
        return $result;
120
    }
121
}
122