id = $payload['data']['id']; $this->content = $payload['data']['content']; $this->fileResultType = $payload['data']['file_result_type']; $this->status = $payload['data']['status']; } public function handle() { if ($this->fileResultType === 'md') { $this->handleDocumentMD(); return; } $this->handleDocumentJson(); } protected function handleDocumentMD() { $storage = Storage::disk('local'); $fileName = 'document'; // The .md extension signals the success status, the lack of signals the fail status. if ($this->status === 'success') { $fileName = $fileName . '.md'; } $dir = "contracts/$this->id"; try { $storage->makeDirectory($dir); $storage->put("$dir/$fileName", $this->content['document']); foreach ($this->content['images'] as $image) { $name = $image['name']; $type = $image['type']; $contents = $image['contents']; $contents = str_replace('data:image/' . $type . ';base64,', '', $contents); $storage->put("$dir/$name.$type", base64_decode($contents)); } // Emit event so other sections of the app can work on it. IngestDocumentReceived::dispatch($this->id); } catch (\Exception $exception) { \Illuminate\Support\Facades\Log::info('Exception: ' . $exception->getTraceAsString()); } } protected function handleDocumentJson() { $handler = new SearchAndDisplaceOriginalDocument(); if ($this->status !== 'success') { $handler->onIngestFail($this->id); return; } $handler->applySD($this->id, $this->content); } }