Completed
Push — master ( 4109b7...859c70 )
by Matthew
08:07 queued 02:26
created

PriorityJobManager   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 76.19%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 3
dl 0
loc 98
ccs 32
cts 42
cp 0.7619
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setPriorityDirection() 0 4 1
B validatePriority() 0 17 5
A getMaxPriority() 0 4 1
A setMaxPriority() 0 4 1
A getPriorityDirection() 0 4 1
A findHigherPriority() 0 15 4
prioritySave() 0 1 ?
A calculatePriority() 0 11 3
A save() 0 9 2
1
<?php
2
3
namespace Dtc\QueueBundle\Model;
4
5
use Dtc\QueueBundle\Exception\PriorityException;
6
7
abstract class PriorityJobManager extends AbstractJobManager
8
{
9
    const PRIORITY_ASC = 'asc';
10
    const PRIORITY_DESC = 'desc';
11
12
    protected $maxPriority;
13
    protected $priorityDirection = self::PRIORITY_DESC;
14
15
    /**
16
     * @return mixed
17
     */
18 11
    public function getMaxPriority()
19
    {
20 11
        return $this->maxPriority;
21
    }
22
23
    /**
24
     * @param mixed $maxPriority
25
     */
26 8
    public function setMaxPriority($maxPriority)
27
    {
28 8
        $this->maxPriority = $maxPriority;
29 8
    }
30
31
    /**
32
     * @return mixed
33
     */
34 8
    public function getPriorityDirection()
35
    {
36 8
        return $this->priorityDirection;
37
    }
38
39
    /**
40
     * @param mixed $priorityDirection
41
     */
42 9
    public function setPriorityDirection($priorityDirection)
43
    {
44 9
        $this->priorityDirection = $priorityDirection;
45 9
    }
46
47 22
    protected function validatePriority($priority)
48
    {
49 22
        if (null === $priority) {
50 21
            return;
51
        }
52
53 3
        if (!ctype_digit(strval($priority))) {
54
            throw new PriorityException("Priority ($priority) needs to be a positive integer");
55
        }
56 3
        if (strval(intval($priority)) !== strval($priority)) {
57
            throw new PriorityException("Priority ($priority) needs to be less than ".PHP_INT_MAX);
58
        }
59 3
        $maxPriority = $this->getMaxPriority();
60 3
        if (intval($priority) > $maxPriority) {
61 1
            throw new PriorityException("Priority ($priority) must be less than ".$maxPriority);
62
        }
63 3
    }
64
65 22
    protected function calculatePriority($priority)
66
    {
67 22
        if (null === $priority) {
68 21
            return $priority;
69
        }
70 3
        if (self::PRIORITY_DESC === $this->priorityDirection) {
71 3
            $priority = $this->maxPriority - $priority;
72 3
        }
73
74 3
        return $priority;
75
    }
76
77
    protected function findHigherPriority($priority1, $priority2)
78
    {
79
        if (null === $priority1) {
80
            return $priority2;
81
        }
82
        if (null === $priority2) {
83
            return $priority1;
84
        }
85
86
        if (self::PRIORITY_DESC === $this->priorityDirection) {
87
            return min($priority1, $priority2);
88
        } else {
89
            return max($priority1, $priority2);
90
        }
91
    }
92
93
    abstract protected function prioritySave(Job $job);
94
95 22
    public function save(Job $job)
96
    {
97 22
        $this->validatePriority($job->getPriority());
98 22
        if (!$job->getId()) { // An unsaved job needs it's priority potentially adjusted
99 22
            $job->setPriority($this->calculatePriority($job->getPriority()));
100 22
        }
101
102 22
        return $this->prioritySave($job);
103
    }
104
}
105