ldc-gen-game-web/app/Console/Commands/ExportCodesUniques.php

193 lines
6.9 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Console\Commands;
use App\Mail\ExportCodesUniques as MailExportCodesUniques;
use App\Models\Code;
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';
/**
* Execute the console command.
*
* @return integer
*/
public function handle()
{
// Nombre total de code à exporter.
$nbCodeTot = $this->ask("Combien de code doivent être exporté ?");
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);
// Nombre de code par fichier.
$nbCodeFic = $this->ask("Combien 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);
// Nombre de code par ligne.
$nbCodeLigne = $this->ask("Combien de code par ligne dans un fichier ?");
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);
2021-08-18 17:08:02 +02:00
$totalExportedCodes = 0;
$currentFilename = 1;
$currentExportedCodes = 0;
2021-08-18 17:08:02 +02:00
$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))) {
2021-08-18 17:08:02 +02:00
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))) {
2021-08-18 17:08:02 +02:00
if (pathinfo($file_to_zip, PATHINFO_EXTENSION) != 'csv') {
continue;
}
unlink(storage_path('export_codes_uniques/tmp/') . $file_to_zip);
}
closedir($handle);
}
2021-08-18 17:08:02 +02:00
$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));
return 0;
}
2021-08-18 17:08:02 +02:00
private function process($nbCodeFic, $nbCodeTot, $nbCodeLigne, $totalExportedCodes, $currentFilename, $currentExportedCodes, $currentLineCodes, $lastExportedCodeId = null)
{
2021-04-02 15:01:11 +02:00
$paginate = config('codes.paginate');
2021-08-18 17:08:02 +02:00
if ($nbCodeTot < $paginate) {
2021-04-02 15:01:11 +02:00
$nbCodeToRetrieve = $nbCodeTot;
2021-08-18 17:08:02 +02:00
} else {
if ($totalExportedCodes + $paginate > $nbCodeTot) {
2021-04-02 15:01:11 +02:00
$nbCodeToRetrieve = $nbCodeTot - $totalExportedCodes;
2021-08-18 17:08:02 +02:00
} else {
2021-04-02 15:01:11 +02:00
$nbCodeToRetrieve = $paginate;
}
}
if ($lastExportedCodeId === null) {
$codes = Code::where('code_exported', 0)->take($nbCodeToRetrieve)->get();
2021-08-18 17:08:02 +02:00
} else {
$codes = Code::where([
2021-04-02 15:01:11 +02:00
['code_exported', '=', 0],
['id', '>', $lastExportedCodeId]
])->take($nbCodeToRetrieve)->get();
}
$countCodes = count($codes);
2021-04-02 15:01:11 +02:00
$exportedCodesId = [];
2021-08-18 17:08:02 +02:00
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);
2021-08-18 17:08:02 +02:00
} else {
file_put_contents(
storage_path('export_codes_uniques/tmp/') . $currentFilename . ".csv",
$code->code_unique . ";",
FILE_APPEND
);
$totalExportedCodes++;
$currentExportedCodes++;
$currentLineCodes++;
}
2021-04-02 15:01:11 +02:00
array_push($exportedCodesId, $code->id);
$lastExportedCodeId = $code->id;
if ($currentExportedCodes === $nbCodeFic) {
$currentFilename++;
$currentExportedCodes = 0;
}
2021-08-18 17:08:02 +02:00
if ((($k + 1) === $countCodes) && $totalExportedCodes < $nbCodeTot) {
Code::whereIn('id', $exportedCodesId)->update(['code_exported' => 1]);
return $this->process(
$nbCodeFic,
$nbCodeTot,
$nbCodeLigne,
$totalExportedCodes,
$currentFilename,
$currentExportedCodes,
$currentLineCodes,
$lastExportedCodeId
);
2021-04-02 15:01:11 +02:00
}
2021-08-18 17:08:02 +02:00
if (($k + 1) === $countCodes) {
Code::whereIn('id', $exportedCodesId)->update(['code_exported' => 1]);
}
} //end if
}//end foreach
}
}