- add code parameters into config - add code length parameter into settings - add total possible codes to generate in generation page - add GenerateRequest validation - changed job ProcessUniqueCode to increase performances using mass insert and pagination - changed singletoned Settings instance to increase performances - Fix process export code put datetime in file name - aded storage to gitignore file - fixed export to guess export codes, also non recursive now - changed refactorize a bit EnterCodeController::participate + add request - Add request to export and generate codes requests
212 lines
6 KiB
PHP
212 lines
6 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Mail\ExportCodesUniques;
|
|
use App\Mail\JobFailed;
|
|
use App\Models\Codes;
|
|
use Exception;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
class ProcessExportCodesUniques implements ShouldQueue
|
|
{
|
|
use Dispatchable;
|
|
use InteractsWithQueue;
|
|
use Queueable;
|
|
use SerializesModels;
|
|
|
|
/**
|
|
* The number of seconds the job can run before timing out.
|
|
*
|
|
* 7200 seconds (2 hours)
|
|
*
|
|
* @var integer
|
|
*/
|
|
public $timeout = 7200;
|
|
|
|
/**
|
|
* Nombre de codes par fichiers.
|
|
* @var integer
|
|
*/
|
|
private $nbCodeFic;
|
|
|
|
/**
|
|
* Nombre total de codes à exporter.
|
|
*
|
|
* @var integer
|
|
*/
|
|
private $nbCodeTot;
|
|
|
|
/**
|
|
* Nombre de codes par ligne.
|
|
*
|
|
* @var integer
|
|
*/
|
|
private $nbCodeLigne;
|
|
|
|
/**
|
|
* The export file name
|
|
*
|
|
* @var string
|
|
*/
|
|
private $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
|
|
*/
|
|
public function __construct(int $nbCodeFic, int $nbCodeTot, int $nbCodeLigne)
|
|
{
|
|
$this->nbCodeFic = $nbCodeFic;
|
|
$this->nbCodeTot = $nbCodeTot;
|
|
$this->nbCodeLigne = $nbCodeLigne;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
|
|
$this->exportFileName = sprintf(
|
|
'CodesUniques_%s',
|
|
date('dmY_His')
|
|
);
|
|
|
|
$lastExportedCodeId = Codes::whereCodeExported(true)
|
|
->orderBy('id', 'desc')->first()->id ?? null;
|
|
|
|
$filesPath = $this->generateCsvFiles($lastExportedCodeId);
|
|
|
|
// * Compression des fichiers.
|
|
$zip = new \ZipArchive();
|
|
if (
|
|
$zip->open(
|
|
storage_path(sprintf('export_codes_uniques/%s.zip', $this->exportFileName)),
|
|
\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)) {
|
|
File::delete($filePath);
|
|
}
|
|
}
|
|
|
|
// * Envoi de mail à MAIL_ADMIN avec le nom du ZIP "CodesUniques_". date('dmY_His') .".zip".
|
|
Mail::to(config('mail.MAIL_ADMIN'))
|
|
->send(new ExportCodesUniques(
|
|
$this->nbCodeFic,
|
|
$this->nbCodeTot,
|
|
$this->nbCodeLigne
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Generate CSV File using the job parameters
|
|
*
|
|
* @param integer|null $lastExportedCodeId
|
|
* @return array Array of temp files
|
|
*/
|
|
private function generateCsvFiles($lastExportedCodeId = null): array
|
|
{
|
|
$outPaths = [];
|
|
$totalExportedCodes = 0;
|
|
$currentFilename = 1;
|
|
$currentExportedCodes = 0;
|
|
$currentLineCodes = ($this->nbCodeLigne === 1 ? 1 : 0);
|
|
|
|
$paginate = intval(config('codes.paginate', 65535));
|
|
|
|
// ! Guess code ids instead of getting thme from base.
|
|
$codes = [];
|
|
$id = ($lastExportedCodeId ?? 0) + 1;
|
|
for ($i = 0; $i < $this->nbCodeTot; $i++) {
|
|
$codes[] = $id++;
|
|
}
|
|
|
|
$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, Codes::intToSerial($code) . "\n");
|
|
$currentLineCodes = ($this->nbCodeLigne === 1 ? 1 : 0);
|
|
} else {
|
|
File::append($tempFilePath, Codes::intToSerial($code) . ';');
|
|
$currentLineCodes++;
|
|
} //end if
|
|
|
|
$totalExportedCodes++;
|
|
$currentExportedCodes++;
|
|
|
|
$exportedCodesId[] = $code;
|
|
|
|
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) {
|
|
Codes::whereIn('id', $exportedCodesId)->update(['code_exported' => 1]);
|
|
$exportedCodesId = [];
|
|
}
|
|
} //end if
|
|
} //end foreach
|
|
|
|
// ? Are there exported codes to save ?
|
|
if (count($exportedCodesId)) {
|
|
Codes::whereIn('id', $exportedCodesId)->update(['code_exported' => 1]);
|
|
}
|
|
return $outPaths;
|
|
}
|
|
|
|
/**
|
|
* In case of job failure
|
|
*
|
|
* @param Exception $exception
|
|
* @return void
|
|
*/
|
|
public function failed(Exception $exception)
|
|
{
|
|
Mail::to(config('mail.MAIL_ADMIN'))
|
|
->send(new JobFailed("ProcessExportCodeUnique", [
|
|
'message' => $exception->getMessage(),
|
|
'trace' => $exception->getTraceAsString()
|
|
]));
|
|
}
|
|
}
|