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