215 lines
7.4 KiB
PHP
215 lines
7.4 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Mail\ExportCodesUniques as MailExportCodesUniques;
|
|
use App\Models\Code;
|
|
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
|
|
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
|
|
*/
|
|
public function handle()
|
|
{
|
|
// phpcs:enable
|
|
// 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);
|
|
|
|
$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.admin'))
|
|
->send(new MailExportCodesUniques($nbCodeFic, $nbCodeTot, $nbCodeLigne));
|
|
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param integer $nbCodeFic
|
|
* @param integer $nbCodeTot
|
|
* @param integer $nbCodeLigne
|
|
* @param integer $totalExportedCodes
|
|
* @param string $currentFilename
|
|
* @param integer $currentExportedCodes
|
|
* @param integer $currentLineCodes
|
|
* @param integer $lastExportedCodeId
|
|
* @return mixed
|
|
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
|
|
*/
|
|
private function process(
|
|
int $nbCodeFic,
|
|
int $nbCodeTot,
|
|
int $nbCodeLigne,
|
|
int $totalExportedCodes,
|
|
string $currentFilename,
|
|
int $currentExportedCodes,
|
|
int $currentLineCodes,
|
|
int $lastExportedCodeId = null
|
|
) {
|
|
// phpcs:enable
|
|
$paginate = config('codes.paginate');
|
|
|
|
if ($nbCodeTot < $paginate) {
|
|
$nbCodeToRetrieve = $nbCodeTot;
|
|
} else {
|
|
if ($totalExportedCodes + $paginate > $nbCodeTot) {
|
|
$nbCodeToRetrieve = $nbCodeTot - $totalExportedCodes;
|
|
} else {
|
|
$nbCodeToRetrieve = $paginate;
|
|
}
|
|
}
|
|
|
|
if ($lastExportedCodeId === null) {
|
|
$codes = Code::where('code_exported', 0)->take($nbCodeToRetrieve)->get();
|
|
} else {
|
|
$codes = Code::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) {
|
|
Code::whereIn('id', $exportedCodesId)->update(['code_exported' => 1]);
|
|
return $this->process(
|
|
$nbCodeFic,
|
|
$nbCodeTot,
|
|
$nbCodeLigne,
|
|
$totalExportedCodes,
|
|
$currentFilename,
|
|
$currentExportedCodes,
|
|
$currentLineCodes,
|
|
$lastExportedCodeId
|
|
);
|
|
}
|
|
if (($k + 1) === $countCodes) {
|
|
Code::whereIn('id', $exportedCodesId)->update(['code_exported' => 1]);
|
|
}
|
|
} //end if
|
|
} //end foreach
|
|
}
|
|
}
|