1 | <?php |
||
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 | 3 | public function getMaxPriority() |
|
19 | { |
||
20 | 3 | return $this->maxPriority; |
|
21 | } |
||
22 | |||
23 | /** |
||
24 | * @param mixed $maxPriority |
||
25 | */ |
||
26 | 3 | public function setMaxPriority($maxPriority) |
|
27 | { |
||
28 | 3 | $this->maxPriority = $maxPriority; |
|
29 | 3 | } |
|
30 | |||
31 | /** |
||
32 | * @return mixed |
||
33 | */ |
||
34 | 3 | public function getPriorityDirection() |
|
35 | { |
||
36 | 3 | return $this->priorityDirection; |
|
37 | } |
||
38 | |||
39 | /** |
||
40 | * @param mixed $priorityDirection |
||
41 | */ |
||
42 | 3 | public function setPriorityDirection($priorityDirection) |
|
46 | |||
47 | 1 | protected function validatePriority($priority) |
|
48 | { |
||
49 | 1 | if (null === $priority) { |
|
50 | 1 | return; |
|
51 | } |
||
52 | |||
53 | if (!ctype_digit(strval($priority))) { |
||
54 | throw new PriorityException("Priority ($priority) needs to be a positive integer"); |
||
55 | } |
||
56 | if (strval(intval($priority)) !== strval($priority)) { |
||
57 | throw new PriorityException("Priority ($priority) needs to be less than ".PHP_INT_MAX); |
||
58 | } |
||
59 | $maxPriority = $this->getMaxPriority(); |
||
60 | if (intval($priority) > $maxPriority) { |
||
61 | throw new PriorityException("Priority ($priority) must be less than ".$maxPriority); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | 1 | protected function calculatePriority($priority) |
|
66 | { |
||
67 | 1 | if (null === $priority) { |
|
68 | 1 | return $priority; |
|
69 | } |
||
70 | if (self::PRIORITY_DESC === $this->priorityDirection) { |
||
71 | $priority = $this->maxPriority - $priority; |
||
72 | } |
||
73 | |||
74 | return $priority; |
||
75 | } |
||
76 | |||
77 | protected function findHigherPriority($priority1, $priority2) |
||
92 | |||
93 | abstract protected function prioritySave(Job $job); |
||
94 | |||
95 | 1 | protected function recordTiming(Job $job) |
|
96 | { |
||
97 | 1 | $status = JobTiming::STATUS_INSERT; |
|
98 | 1 | if ($job->getWhenAt() && $job->getWhenAt() > (new \DateTime())) { |
|
99 | $status = JobTiming::STATUS_INSERT_DELAYED; |
||
100 | } |
||
101 | |||
102 | 1 | $this->jobTiminigManager->recordTiming($status); |
|
103 | 1 | } |
|
104 | |||
105 | /** |
||
106 | * @param Job $job |
||
107 | * |
||
108 | * @return mixed |
||
109 | * |
||
110 | * @throws PriorityException |
||
111 | */ |
||
112 | 1 | public function save(Job $job) |
|
124 | } |
||
125 |