ProcessExportCodesUniques remove writed file in case of export failure

This commit is contained in:
Jérémy Munsch 2023-12-20 12:58:40 +01:00
parent ee8b0a6a41
commit de748bb9bb
No known key found for this signature in database
GPG key ID: 18361AF3BFEC0FEC

View file

@ -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
});
}