id = $id; $this->storage = Storage::disk('local'); $this->directoryPath = "contracts/$this->id"; $this->infoFilePath = "searchers/$this->id.json"; } public function execute() { if ( ! $this->storage->exists($this->directoryPath) || ! $this->storage->exists($this->infoFilePath)) { // Handle this case, must report result to user. return; } try { $documentContent = $this->storage->get("$this->directoryPath/document.md"); $searchersContent = json_decode($this->storage->get($this->infoFilePath), true); $documentPath = $searchersContent['document_path']; $searchers = $searchersContent['searchers']; $this->storage->put($this->infoFilePath, json_encode($searchers[0]['content'])); $searchAndDisplace = new SearchAndDisplace($documentContent, [ 'searchers' => [ [ 'key' => $this->id, 'replace_with' => $searchers[0]['replace_with'], ] ], ]); $result = $searchAndDisplace->execute(); file_put_contents($documentPath, $result['content']); } catch (\Exception $exception) { \Illuminate\Support\Facades\Log::info('EXCEPTION: ' . $exception->getMessage()); return; } finally { $this->storage->deleteDirectory($this->directoryPath); $this->storage->delete($this->infoFilePath); } } }