diff --git a/app/Jobs/ProcessExportCodesUniques.php b/app/Jobs/ProcessExportCodesUniques.php index 72ef07a..cc86c3f 100644 --- a/app/Jobs/ProcessExportCodesUniques.php +++ b/app/Jobs/ProcessExportCodesUniques.php @@ -66,43 +66,47 @@ class ProcessExportCodesUniques implements ShouldQueue, ShouldBeUnique date('dmY_His') ); - $lastExportedCodeId = Code::query() - ->where('code_exported', true) - ->orderBy('id', 'desc')->first()->id ?? null; + $exportFilePath = storage_path(sprintf('export_codes_uniques/%s.zip', $this->exportFileName)); - $filesPath = $this->generateCsvFiles($lastExportedCodeId); + try { + $lastExportedCodeId = Code::query() + ->where('code_exported', true) + ->orderBy('id', 'desc')->first()->id ?? null; - // * Compression des fichiers. - $zip = new \ZipArchive(); - if ( - $zip->open( - storage_path(sprintf('export_codes_uniques/%s.zip', $this->exportFileName)), - \ZipArchive::CREATE - ) === true - ) { + $filesPath = $this->generateCsvFiles($lastExportedCodeId); + + // * Compression des fichiers. + $zip = new \ZipArchive(); + if ($zip->open($exportFilePath, \ZipArchive::CREATE) === true) { + foreach ($filesPath as $filePath) { + if (File::exists($filePath)) { + $zip->addFile($filePath, \pathinfo($filePath, \PATHINFO_BASENAME)); + } + } + $zip->close(); + } + + // * On vide le dossier temporaire des fichiers csv. foreach ($filesPath as $filePath) { if (File::exists($filePath)) { - $zip->addFile($filePath, \pathinfo($filePath, \PATHINFO_BASENAME)); + File::delete($filePath); } } - $zip->close(); - } - // * On vide le dossier temporaire des fichiers csv. - foreach ($filesPath as $filePath) { - if (File::exists($filePath)) { - File::delete($filePath); - } - } - - // * Envoi de mail à MAIL_ADMIN avec le nom du ZIP "CodesUniques_". date('dmY_His') .".zip". - Mail::to(config('mail.admin')) - ->send(new CodeExportSucceed( + // * Envoi de mail à MAIL_ADMIN avec le nom du ZIP "CodesUniques_". date('dmY_His') .".zip". + Mail::to(config('mail.admin'))->send(new CodeExportSucceed( $this->nbCodeFic, $this->nbCodeTot, $this->nbCodeLigne, - storage_path(sprintf('export_codes_uniques/%s.zip', $this->exportFileName)) + $exportFilePath )); + } catch (\Exception $e) { + // * Clear exported file in case of Failure. + if (File::exists($exportFilePath)) { + File::delete($exportFilePath); + } + throw $e; + } //end try }); }