1 | <?php |
||
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 | 4 | public function getMaxPriority() |
|
20 | |||
21 | /** |
||
22 | * @param mixed $maxPriority |
||
23 | */ |
||
24 | 4 | public function setMaxPriority($maxPriority) |
|
28 | |||
29 | /** |
||
30 | * @return mixed |
||
31 | */ |
||
32 | 4 | public function getPriorityDirection() |
|
36 | |||
37 | /** |
||
38 | * @param mixed $priorityDirection |
||
39 | */ |
||
40 | 4 | public function setPriorityDirection($priorityDirection) |
|
44 | |||
45 | 5 | protected function validatePriority($priority) |
|
46 | { |
||
47 | 5 | if (null === $priority) { |
|
48 | 5 | return; |
|
49 | } |
||
50 | |||
51 | if (!ctype_digit(strval($priority))) { |
||
52 | throw new \Exception("Priority ($priority) needs to be a positive integer"); |
||
53 | } |
||
54 | if (strval(intval($priority)) !== strval($priority)) { |
||
55 | throw new \Exception("Priority ($priority) needs to be less than ".PHP_INT_MAX); |
||
56 | } |
||
57 | $maxPriority = $this->getMaxPriority(); |
||
58 | if (intval($priority) > $maxPriority) { |
||
59 | throw new \Exception("Priority ($priority) must be less than ".$maxPriority); |
||
60 | } |
||
61 | } |
||
62 | |||
63 | 5 | protected function calculatePriority($priority) |
|
64 | { |
||
65 | 5 | if (null === $priority) { |
|
66 | 5 | return $priority; |
|
67 | } |
||
68 | if (self::PRIORITY_DESC === $this->priorityDirection) { |
||
69 | $priority = $this->maxPriority - $priority; |
||
70 | } |
||
71 | |||
72 | return $priority; |
||
73 | } |
||
74 | |||
75 | protected function findHigherPriority($priority1, $priority2) |
||
90 | |||
91 | abstract protected function prioritySave(Job $job); |
||
92 | |||
93 | 5 | public function save(Job $job) |
|
102 | } |
||
103 |