1 | <?php |
||
11 | class Driver implements \Bernard\Driver |
||
12 | { |
||
13 | private $baseDirectory; |
||
14 | |||
15 | private $permissions; |
||
16 | |||
17 | /** |
||
18 | * @param string $baseDirectory The base directory |
||
19 | * @param int $permissions permissions to create the file with |
||
20 | */ |
||
21 | 10 | public function __construct($baseDirectory, $permissions = 0740) |
|
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | 1 | public function listQueues() |
|
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | 10 | public function createQueue($queueName) |
|
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | public function countMessages($queueName) |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | 8 | public function pushMessage($queueName, $message) |
|
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | 4 | public function popMessage($queueName, $duration = 5) |
|
93 | { |
||
94 | 4 | $runtime = microtime(true) + $duration; |
|
95 | 4 | $queueDir = $this->getQueueDirectory($queueName); |
|
96 | |||
97 | 4 | $files = $this->getJobFiles($queueName); |
|
98 | |||
99 | 4 | while (microtime(true) < $runtime) { |
|
100 | 4 | if ($files) { |
|
|
|||
101 | 4 | $id = array_pop($files); |
|
102 | 4 | if (@rename($queueDir.DIRECTORY_SEPARATOR.$id, $queueDir.DIRECTORY_SEPARATOR.$id.'.proceed')) { |
|
103 | 4 | return [file_get_contents($queueDir.DIRECTORY_SEPARATOR.$id.'.proceed'), $id]; |
|
104 | } |
||
105 | |||
106 | return $this->processFileOrFail($queueDir, $id); |
||
107 | } else { |
||
108 | // In order to notice that a new message received, update the list. |
||
109 | 1 | $files = $this->getJobFiles($queueName); |
|
110 | } |
||
111 | |||
112 | 1 | usleep(1000); |
|
113 | 1 | } |
|
114 | |||
115 | return [null, null]; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param string $queueDir |
||
120 | * @param string $id |
||
121 | * |
||
122 | * @return array |
||
123 | */ |
||
124 | private function processFileOrFail($queueDir, $id) { |
||
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | 1 | public function acknowledgeMessage($queueName, $receipt) |
|
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | 1 | public function peekQueue($queueName, $index = 0, $limit = 20) |
|
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | 2 | public function removeQueue($queueName) |
|
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | public function info() |
||
201 | |||
202 | /** |
||
203 | * @param string $queueName |
||
204 | * |
||
205 | * @return string |
||
206 | */ |
||
207 | 10 | private function getQueueDirectory($queueName) |
|
211 | |||
212 | /** |
||
213 | * Generates a uuid. |
||
214 | * |
||
215 | * @param string $queueName |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | 8 | private function getJobFilename($queueName) |
|
247 | |||
248 | /** |
||
249 | * @param string $queueName |
||
250 | * |
||
251 | * @return string[] |
||
252 | */ |
||
253 | 4 | private function getJobFiles($queueName) |
|
264 | } |
||
265 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.