ldc-gen-game-web/app/Console/Commands/ExportCodesUniques.php
Jérémy Munsch 9ee2123928
- composer require kwaadpepper/serial-int-caster
- 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
2022-01-07 19:03:25 +01:00

167 lines
6.4 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Mail\ExportCodesUniques as MailExportCodesUniques;
use App\Models\Codes;
use App\Models\Settings;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
class ExportCodesUniques extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'export:codeunique';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$nbCodeTot = $this->ask("Combien de code doivent être exporté ?"); // Nombre total de code à exporter
while (empty(intval($nbCodeTot)) || !is_int(intval($nbCodeTot))) {
$this->info("Valeur incorrect, veuillez entrer un nombre entier.");
$nbCodeTot = $this->ask("Veuillez entrer une nouvelle valeur");
}
$nbCodeTot = intval($nbCodeTot);
$nbCodeFic = $this->ask("Combien de code par fichier ?"); // Nombre de code par fichier
while (empty(intval($nbCodeFic)) || !is_int(intval($nbCodeFic))) {
$this->info("Valeur incorrect, veuillez entrer un nombre entier.");
$nbCodeFic = $this->ask("Veuillez entrer une nouvelle valeur");
}
$nbCodeFic = intval($nbCodeFic);
$nbCodeLigne = $this->ask("Combien de code par ligne dans un fichier ?"); // Nombre de code par ligne
while (empty(intval($nbCodeLigne)) || !is_int(intval($nbCodeLigne))) {
$this->info("Valeur incorrect, veuillez entrer un nombre entier.");
$nbCodeLigne = $this->ask("Veuillez entrer une nouvelle valeur");
}
$nbCodeLigne = intval($nbCodeLigne);
$totalExportedCodes = 0;
$currentFilename = 1;
$currentExportedCodes = 0;
$currentLineCodes = ($nbCodeLigne === 1 ? 1 : 0);
$this->info("Traitement en cours, veuillez patienter...");
$this->process($nbCodeFic, $nbCodeTot, $nbCodeLigne, $totalExportedCodes, $currentFilename, $currentExportedCodes, $currentLineCodes);
$this->info("Fin du traitement, creation de l'archive...");
// Compression des fichiers
$zip = new \ZipArchive();
if ($zip->open(storage_path('export_codes_uniques/') . "CodesUniques_" . date('dmY') . ".zip", \ZipArchive::CREATE) === true) {
if ($handle = opendir(storage_path('export_codes_uniques/tmp'))) {
while (false !== ($file_to_zip = readdir($handle))) {
if (pathinfo($file_to_zip, PATHINFO_EXTENSION) != 'csv') {
continue;
}
$zip->addFile(storage_path('export_codes_uniques/tmp/') . $file_to_zip, "$file_to_zip");
}
closedir($handle);
}
$zip->close();
}
// On vide le dossier temporaire des fichiers csv
if ($handle = opendir(storage_path('export_codes_uniques/tmp'))) {
while (false !== ($file_to_zip = readdir($handle))) {
if (pathinfo($file_to_zip, PATHINFO_EXTENSION) != 'csv') {
continue;
}
unlink(storage_path('export_codes_uniques/tmp/') . $file_to_zip);
}
closedir($handle);
}
$this->info("L'archive CodesUniques_" . date('dmY') . ".zip a ete creer.");
// Envoi de mail à MAIL_ADMIN avec le nom du ZIP "CodesUniques_". date('dmY') .".zip"
Mail::to(config('mail.MAIL_ADMIN'))->send(new MailExportCodesUniques(app(Settings::class)->name, $nbCodeFic, $nbCodeTot, $nbCodeLigne));
}
private function process($nbCodeFic, $nbCodeTot, $nbCodeLigne, $totalExportedCodes, $currentFilename, $currentExportedCodes, $currentLineCodes, $lastExportedCodeId = null)
{
$paginate = config('codes.paginate');
if ($nbCodeTot < $paginate) {
$nbCodeToRetrieve = $nbCodeTot;
} else {
if ($totalExportedCodes + $paginate > $nbCodeTot) {
$nbCodeToRetrieve = $nbCodeTot - $totalExportedCodes;
} else {
$nbCodeToRetrieve = $paginate;
}
}
if ($lastExportedCodeId === null) {
$codes = Codes::where('code_exported', 0)->take($nbCodeToRetrieve)->get();
} else {
$codes = Codes::where([
['code_exported', '=', 0],
['id', '>', $lastExportedCodeId]
])->take($nbCodeToRetrieve)->get();
}
$countCodes = count($codes);
$exportedCodesId = [];
foreach ($codes as $k => $code) {
if ($currentExportedCodes < $nbCodeTot) {
if ($currentLineCodes === ($nbCodeLigne === 1 ? 1 : $nbCodeLigne - 1)) {
file_put_contents(storage_path('export_codes_uniques/tmp/') . $currentFilename . ".csv", $code->code_unique . PHP_EOL, FILE_APPEND);
$totalExportedCodes++;
$currentExportedCodes++;
$currentLineCodes = ($nbCodeLigne === 1 ? 1 : 0);
} else {
file_put_contents(storage_path('export_codes_uniques/tmp/') . $currentFilename . ".csv", $code->code_unique . ";", FILE_APPEND);
$totalExportedCodes++;
$currentExportedCodes++;
$currentLineCodes++;
}
array_push($exportedCodesId, $code->id);
$lastExportedCodeId = $code->id;
if ($currentExportedCodes === $nbCodeFic) {
$currentFilename++;
$currentExportedCodes = 0;
}
if ((($k + 1) === $countCodes) && $totalExportedCodes < $nbCodeTot) {
Codes::whereIn('id', $exportedCodesId)->update(['code_exported' => 1]);
return $this->process($nbCodeFic, $nbCodeTot, $nbCodeLigne, $totalExportedCodes, $currentFilename, $currentExportedCodes, $currentLineCodes, $lastExportedCodeId);
}
if (($k + 1) === $countCodes) {
Codes::whereIn('id', $exportedCodesId)->update(['code_exported' => 1]);
}
}
}
}
}