id = $id; $this->storage = Storage::disk('local'); $this->documentFilePath = "contracts/$this->id.md"; $this->infoFilePath = "searchers/$this->id.json"; } public function execute() { if ( ! $this->storage->exists($this->documentFilePath) || ! $this->storage->exists($this->infoFilePath) ) { // Handle this case, must report result to user. return; } $documentContent = $this->storage->get($this->documentFilePath); $info = json_decode($this->storage->get($this->infoFilePath), true); $searchAndDisplace = new SearchAndDisplace($documentContent, $info); $originalDocumentPath = $info['document_path']; $pathDetails = pathinfo($originalDocumentPath); $resultedDocumentPath = $pathDetails['dirname'] . '/' . $pathDetails['filename'] . '-displaced.md'; try { $resultedDocumentContent = $searchAndDisplace->execute(); file_put_contents($resultedDocumentPath, $resultedDocumentContent); } catch (\Exception $exception) { \Illuminate\Support\Facades\Log::info('exception: ' . $exception->getMessage()); return; } finally { $this->storage->delete($this->documentFilePath); $this->storage->delete($this->infoFilePath); } } }