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
|
21 |
|
public function getMaxPriority() |
19
|
|
|
{ |
20
|
21 |
|
return $this->maxPriority; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param mixed $maxPriority |
25
|
|
|
*/ |
26
|
11 |
|
public function setMaxPriority($maxPriority) |
27
|
|
|
{ |
28
|
11 |
|
$this->maxPriority = $maxPriority; |
29
|
11 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return mixed |
33
|
|
|
*/ |
34
|
10 |
|
public function getPriorityDirection() |
35
|
|
|
{ |
36
|
10 |
|
return $this->priorityDirection; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param mixed $priorityDirection |
41
|
|
|
*/ |
42
|
13 |
|
public function setPriorityDirection($priorityDirection) |
43
|
|
|
{ |
44
|
13 |
|
$this->priorityDirection = $priorityDirection; |
45
|
13 |
|
} |
46
|
|
|
|
47
|
39 |
|
protected function validatePriority($priority) |
48
|
|
|
{ |
49
|
39 |
|
if (null === $priority) { |
50
|
36 |
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
11 |
|
if (!ctype_digit(strval($priority))) { |
54
|
|
|
throw new PriorityException("Priority ($priority) needs to be a positive integer"); |
55
|
|
|
} |
56
|
11 |
|
if (strval(intval($priority)) !== strval($priority)) { |
57
|
|
|
throw new PriorityException("Priority ($priority) needs to be less than ".PHP_INT_MAX); |
58
|
|
|
} |
59
|
11 |
|
$maxPriority = $this->getMaxPriority(); |
60
|
11 |
|
if (intval($priority) > $maxPriority) { |
61
|
4 |
|
throw new PriorityException("Priority ($priority) must be less than ".$maxPriority); |
62
|
|
|
} |
63
|
11 |
|
} |
64
|
|
|
|
65
|
39 |
|
protected function calculatePriority($priority) |
66
|
|
|
{ |
67
|
39 |
|
if (null === $priority) { |
68
|
36 |
|
return $priority; |
69
|
|
|
} |
70
|
11 |
|
if (self::PRIORITY_DESC === $this->priorityDirection) { |
71
|
11 |
|
$priority = $this->maxPriority - $priority; |
72
|
11 |
|
} |
73
|
|
|
|
74
|
11 |
|
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
|
39 |
|
public function save(Job $job) |
96
|
|
|
{ |
97
|
39 |
|
$this->validatePriority($job->getPriority()); |
98
|
39 |
|
if (!$job->getId()) { // An unsaved job needs it's priority potentially adjusted |
99
|
39 |
|
$job->setPriority($this->calculatePriority($job->getPriority())); |
100
|
39 |
|
} |
101
|
|
|
|
102
|
39 |
|
return $this->prioritySave($job); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|