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) { |
|
25 | |||
26 | 1 | public function getProvider() |
|
30 | |||
31 | 8 | public function create() |
|
32 | { |
||
33 | 8 | $fs = new Filesystem(); |
|
34 | 8 | if (!$fs->exists($this->queuePath)) { |
|
35 | 8 | $fs->mkdir($this->queuePath); |
|
36 | 8 | return $fs->exists($this->queuePath); |
|
37 | } |
||
38 | return true; |
||
39 | } |
||
40 | |||
41 | 5 | public function publish(array $message, array $options = []) |
|
42 | { |
||
43 | 5 | $fileName = microtime(false); |
|
44 | 5 | $fileName = str_replace(' ', '', $fileName); |
|
45 | 5 | $path = substr(hash('md5', $fileName), 0, 3); |
|
46 | |||
47 | 5 | $fs = new Filesystem(); |
|
48 | 5 | if (!$fs->exists($this->queuePath.DIRECTORY_SEPARATOR.$path)) { |
|
49 | 5 | $fs->mkdir($this->queuePath.DIRECTORY_SEPARATOR.$path); |
|
50 | } |
||
51 | |||
52 | 5 | $fs->dumpFile( |
|
53 | 5 | $this->queuePath.DIRECTORY_SEPARATOR.$path.DIRECTORY_SEPARATOR.$fileName.'.json', |
|
54 | 5 | json_encode($message) |
|
55 | ); |
||
56 | 5 | return $fileName; |
|
57 | } |
||
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 | $this->cleanUp(); |
||
118 | } |
||
119 | 2 | return $success; |
|
120 | } |
||
121 | |||
122 | 2 | public function cleanUp() |
|
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 | } |
||
169 |
If you suppress an error, we recommend checking for the error condition explicitly: