1 | <?php |
||
12 | class FileProvider extends AbstractProvider |
||
13 | { |
||
14 | protected $filePointerList = []; |
||
15 | protected $queuePath; |
||
16 | |||
17 | 10 | public function __construct($name, array $options, $client, Cache $cache, Logger $logger) { |
|
18 | 10 | $this->name = $name; |
|
19 | /* md5 only contain numeric and A to F, so it is file system safe */ |
||
20 | 10 | $this->queuePath = $options['path'].DIRECTORY_SEPARATOR.str_replace('-', '', hash('md5', $name)); |
|
21 | 10 | $this->options = $options; |
|
22 | 10 | $this->cache = $cache; |
|
23 | 10 | $this->logger = $logger; |
|
24 | 10 | } |
|
25 | |||
26 | 1 | public function getProvider() |
|
30 | |||
31 | 8 | public function create() |
|
40 | |||
41 | 5 | public function publish(array $message, array $options = []) |
|
58 | |||
59 | /** |
||
60 | * @param array $options |
||
61 | * @return Message[] |
||
62 | */ |
||
63 | 6 | public function receive(array $options = []) |
|
101 | |||
102 | 2 | public function delete($id) |
|
103 | { |
||
104 | 2 | $success = false; |
|
105 | 2 | if (isset($this->filePointerList[$id])) { |
|
106 | 2 | $fileName = $id; |
|
107 | 2 | $path = substr(hash('md5', (string)$fileName), 0, 3); |
|
108 | 2 | $fs = new Filesystem(); |
|
109 | 2 | $fs->remove( |
|
110 | 2 | $this->queuePath . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $fileName . '.json' |
|
111 | ); |
||
112 | 2 | fclose($this->filePointerList[$id]); |
|
113 | 2 | unset($this->filePointerList[$id]); |
|
114 | 2 | $success = true; |
|
115 | } |
||
116 | 2 | if (rand(1,10) === 5) { |
|
117 | 1 | $this->cleanUp(); |
|
118 | } |
||
119 | 2 | return $success; |
|
120 | } |
||
121 | |||
122 | 3 | public function cleanUp() |
|
123 | { |
||
124 | 3 | $finder = new Finder(); |
|
125 | $finder |
||
126 | 3 | ->files() |
|
127 | 3 | ->in($this->queuePath) |
|
128 | 3 | ->ignoreDotFiles(true) |
|
129 | 3 | ->ignoreUnreadableDirs(true) |
|
130 | 3 | ->ignoreVCS(true) |
|
131 | 3 | ->depth('< 2') |
|
132 | 3 | ->name('*.json') |
|
133 | ; |
||
134 | 3 | $finder->date( |
|
135 | 3 | sprintf('< %d seconds ago', $this->options['message_expiration']) |
|
136 | ); |
||
137 | /** @var SplFileInfo $file */ |
||
138 | 3 | foreach ($finder as $file) { |
|
139 | 2 | @unlink($file->getRealPath()); |
|
|
|||
140 | } |
||
141 | 3 | } |
|
142 | |||
143 | 1 | public function destroy() |
|
150 | |||
151 | /** |
||
152 | * Removes the message from queue after all other listeners have fired |
||
153 | * |
||
154 | * If an earlier listener has erred or stopped propagation, this method |
||
155 | * will not fire and the Queued Message should become visible in queue again. |
||
156 | * |
||
157 | * Stops Event Propagation after removing the Message |
||
158 | * |
||
159 | * @param MessageEvent $event The SQS Message Event |
||
160 | * @return bool|void |
||
161 | */ |
||
162 | 1 | public function onMessageReceived(MessageEvent $event) |
|
168 | } |
If you suppress an error, we recommend checking for the error condition explicitly: