1 | <?php |
||
16 | class Gateway |
||
17 | { |
||
18 | |||
19 | use TimeTrait; |
||
20 | use RedisTrait; |
||
21 | use IdGeneratorTrait; |
||
22 | |||
23 | const QUEUE_DELAYED = 'message_queue:delayed'; |
||
24 | const QUEUE_IMMEDIATE = 'message_queue:immediate'; |
||
25 | const META_DATA = 'message_queue:meta_data'; |
||
26 | const RETRY_TIME = 3600; // try again after 1 hour |
||
27 | |||
28 | /** |
||
29 | * @param string $eventId |
||
30 | * @param string $eventType |
||
31 | * @return bool success |
||
32 | */ |
||
33 | 3 | public function deleteEvent(string $eventId, string $eventType = null) : bool |
|
54 | |||
55 | /** |
||
56 | * @param AbstractEvent $event |
||
57 | * @param int $timestamp |
||
58 | * @return Job |
||
59 | */ |
||
60 | 2 | public function addEvent(AbstractEvent $event, int $timestamp = 0) : Job |
|
72 | |||
73 | /** |
||
74 | * @param Job $job |
||
75 | */ |
||
76 | 3 | public function addJob(Job $job) |
|
94 | |||
95 | /** |
||
96 | * @param string $eventType |
||
97 | * @param int $since |
||
98 | * @return Job[] |
||
99 | */ |
||
100 | 3 | public function getEventsByType(string $eventType = null, int $since = 0) : array |
|
104 | |||
105 | /** |
||
106 | * @param string $eventType |
||
107 | * @param int $since |
||
108 | * @return Generator|Job[] |
||
109 | */ |
||
110 | 3 | public function getEventsByTypeGenerator(string $eventType = null, int $since = 0) : Generator |
|
111 | { |
||
112 | 3 | $redis = $this->getRedis(); |
|
113 | |||
114 | 3 | $resultRaw = $redis->zrangebyscore( |
|
115 | 3 | self::QUEUE_DELAYED, |
|
116 | $since, |
||
117 | 3 | '+inf', |
|
118 | 3 | ['withscores' => true] |
|
119 | ); |
||
120 | |||
121 | 3 | $keys = []; |
|
122 | 3 | foreach ($resultRaw as $jobId => $timestamp) { |
|
123 | 2 | if (empty($eventType) || strpos($jobId, $eventType . ':') === 0) { |
|
124 | 2 | $keys[$jobId] = $timestamp; |
|
125 | } |
||
126 | } |
||
127 | |||
128 | 3 | yield from $this->getFromTimesQueue($keys); |
|
129 | 3 | yield from $this->getFromImmediateQueue($eventType); |
|
130 | 3 | } |
|
131 | |||
132 | /** |
||
133 | * @param Job $job |
||
134 | */ |
||
135 | 1 | public function restoreJob(Job $job) |
|
143 | |||
144 | /** |
||
145 | * @return int |
||
146 | */ |
||
147 | 1 | public function countAllJobs() : int |
|
154 | |||
155 | /** |
||
156 | * @param array $keys |
||
157 | * @return Generator |
||
158 | */ |
||
159 | 3 | private function getFromTimesQueue(array $keys) : Generator |
|
170 | |||
171 | /** |
||
172 | * @param string $eventType |
||
173 | * @return Generator |
||
174 | */ |
||
175 | 3 | private function getFromImmediateQueue(string $eventType = null) : Generator |
|
187 | } |
||
188 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.