ldc-gen-game-web/app/Jobs/ProcessExportCodesUniques.php

205 lines
6.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Jobs;
2023-12-05 12:33:25 +01:00
use App\Mail\Jobs\CodeExportSucceed;
use App\Mail\Jobs\JobFailed;
use App\Models\Code;
use Illuminate\Bus\Queueable;
2023-11-23 17:31:28 +01:00
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
2023-12-05 12:33:25 +01:00
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Mail;
2023-11-23 17:31:28 +01:00
class ProcessExportCodesUniques implements ShouldQueue, ShouldBeUnique
{
2021-08-18 17:08:02 +02:00
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
/**
* The number of seconds the job can run before timing out.
*
* 28800 seconds (8 hours)
*
* @var integer
*/
public $timeout = 28800;
/**
* The export file name
*
* @var string
*/
2023-11-23 17:31:28 +01:00
protected $exportFileName = null;
/**
* Create a new job instance.
*
* @param integer $nbCodeFic Nombre de codes par fichiers.
* @param integer $nbCodeTot Nombre total de codes à exporter.
* @param integer $nbCodeLigne Nombre de codes par ligne.
* @return void
*/
2023-11-23 17:31:28 +01:00
public function __construct(
protected int $nbCodeFic,
protected int $nbCodeTot,
protected int $nbCodeLigne
) {
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
2023-12-05 12:33:25 +01:00
DB::transaction(function () {
$this->exportFileName = sprintf(
'CodesUniques_%s',
date('dmY_His')
);
$exportFilePath = storage_path(sprintf('export_codes_uniques/%s.zip', $this->exportFileName));
2023-12-05 12:33:25 +01:00
try {
$lastExportedCodeId = Code::query()
->where('code_exported', true)
->orderBy('id', 'desc')->first()->id ?? null;
2023-12-05 12:33:25 +01:00
$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));
}
2023-12-05 12:33:25 +01:00
}
$zip->close();
2023-12-05 12:33:25 +01:00
}
// * 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(
2023-12-05 12:33:25 +01:00
$this->nbCodeFic,
$this->nbCodeTot,
$this->nbCodeLigne,
$exportFilePath
2023-12-05 12:33:25 +01:00
));
} catch (\Exception $e) {
// * Clear exported file in case of Failure.
if (File::exists($exportFilePath)) {
File::delete($exportFilePath);
}
throw $e;
} //end try
2023-12-05 12:33:25 +01:00
});
}
/**
* Generate CSV File using the job parameters
*
* @param integer|null $lastExportedCodeId
* @return array Array of temp files
2023-08-24 12:59:51 +00:00
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
*/
private function generateCsvFiles($lastExportedCodeId = null): array
2021-08-18 17:08:02 +02:00
{
2023-08-24 12:59:51 +00:00
// phpcs:enable
$outPaths = [];
$totalExportedCodes = 0;
$currentFilename = 1;
$currentExportedCodes = 0;
$currentLineCodes = ($this->nbCodeLigne === 1 ? 1 : 0);
2021-04-02 12:06:39 +02:00
$paginate = intval(config('codes.paginate', 65535));
2021-04-02 12:06:39 +02:00
// ! Guess code ids instead of getting thme from base.
$codes = [];
$id = ($lastExportedCodeId ?? 0) + 1;
for ($i = 0; $i < $this->nbCodeTot; $i++) {
$codes[] = $id++;
}
2021-04-02 12:06:39 +02:00
$exportedCodesId = [];
$tempFilePath = \storage_path(\sprintf(
'export_codes_uniques/tmp/%s_%s.csv',
$this->exportFileName,
$currentFilename
));
$outPaths[] = $tempFilePath;
foreach ($codes as $code) {
if ($currentExportedCodes < $this->nbCodeTot) {
if ($currentLineCodes === ($this->nbCodeLigne === 1 ? 1 : $this->nbCodeLigne - 1)) {
File::append($tempFilePath, Code::intToSerial($code) . "\n");
$currentLineCodes = ($this->nbCodeLigne === 1 ? 1 : 0);
2021-08-18 17:08:02 +02:00
} else {
File::append($tempFilePath, Code::intToSerial($code) . ';');
$currentLineCodes++;
} //end if
$totalExportedCodes++;
$currentExportedCodes++;
2021-04-02 12:06:39 +02:00
$exportedCodesId[] = $code;
2021-04-02 12:06:39 +02:00
if ($currentExportedCodes === $this->nbCodeFic) {
$currentFilename++;
$currentExportedCodes = 0;
$tempFilePath = \storage_path(\sprintf(
'export_codes_uniques/tmp/%s_%s.csv',
$this->exportFileName,
$currentFilename
));
$outPaths[] = $tempFilePath;
}
// ? Have we reached pagination limit for mass saving ?
if (count($exportedCodesId) === $paginate) {
2023-11-23 17:31:28 +01:00
Code::query()->whereIn('id', $exportedCodesId)->update(['code_exported' => true]);
$exportedCodesId = [];
}
} //end if
} //end foreach
// ? Are there exported codes to save ?
if (count($exportedCodesId)) {
2023-11-23 17:31:28 +01:00
Code::query()->whereIn('id', $exportedCodesId)->update(['code_exported' => true]);
}
return $outPaths;
}
2023-12-05 12:33:25 +01:00
/**
* In case of job failure
*
2023-12-05 12:33:25 +01:00
* @param \Throwable $exception
* @return void
*/
2023-12-05 12:33:25 +01:00
public function failed(\Throwable $exception)
2021-08-18 17:08:02 +02:00
{
2023-12-05 12:33:25 +01:00
Mail::to(config('mail.admin'))->send(
new JobFailed(__CLASS__, [
2023-11-23 17:31:28 +01:00
'message' => $exception->getMessage(),
'trace' => $exception->getTraceAsString()
2023-12-05 12:33:25 +01:00
])
);
}
}