ldc-legaulois/app/Console/Commands/XmlIncoReader.php

158 lines
5.1 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\IncoDatas;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use SimpleXMLElement;
class XmlIncoReader extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'inco:readxml';
/**
* 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 mixed
*/
public function handle()
{
$this->info('Démarrage du traitement');
$files = Storage::disk('inco')->files();
// Suppression des fichiers dans archives qui ont plus de 2j
$archives = Storage::disk('inco')->files('archives');
foreach ($archives as $archive) {
if($archive == ".gitignore" || $archive == ".gitkeep") continue;
if(Storage::disk('inco')->lastModified($archive) < strtotime(Carbon::now()->subDays(2)))
Storage::disk('inco')->delete($archive);
}
$bar = $this->output->createProgressBar(count($files)-1);
$bar->start();
foreach ($files as $filename) {
if($filename == ".gitignore" || $filename == ".gitkeep") continue;
// ---- Récupération du contenu
$files = Storage::disk('inco')->get($filename);
$fileContent = simplexml_load_string($files, null, LIBXML_NOCDATA);
// Transforme le fichier en tableau de données
$inco = $this->extract($fileContent->INCO);
// Récupérer et enregistré l'image
$imageEncoded = $fileContent->INCO->MEDIA_VISUEL_EAI->MEDIA_VISUEL_EAI !== null ? (string) $fileContent->INCO->MEDIA_VISUEL_EAI->MEDIA_VISUEL_EAI : null;
if ($imageEncoded !== null) {
$imgName = strval($fileContent->INCO->INCO_COMPO->TBL_INCO_COMPO->ID_INCO);
$fileFullPath = (storage_path('inco/images/') . $imgName);
file_put_contents($fileFullPath, base64_decode($imageEncoded));
$info = getimagesize($fileFullPath);
$mime = $info['mime'];
switch ($mime) {
case 'image/jpeg':
$imgExtension = 'jpg';
break;
case 'image/png':
$imgExtension = 'png';
break;
case 'image/gif':
$imgExtension = 'gif';
break;
default:
throw new \Exception('Unknown image type.');
}
rename($fileFullPath, $fileFullPath . '.' . $imgExtension);
}
// Mise en BDD des données du fichier xml
$fields = [];
$fields = $this->processFields($inco, $fields);
// Ajout du chemin de l'image dans les champs a envoyé en base
if ($imageEncoded !== null)
$fields['picture'] = 'inco/images/' . $imgName . '.' . $imgExtension;
$checkFields = ['id_inco' => $fields["id_inco"], 'no_art' => $fields['no_art']];
IncoDatas::updateOrCreate($checkFields, $fields);
Storage::disk('inco')->move($filename, 'archives/' . $filename);
$bar->advance();
}
$bar->finish();
$this->line("");
$this->info('Fin du traitement');
}
private function extract($sxe = null) {
if (!$sxe instanceOf SimpleXMLElement)
return array();
$extract = array();
foreach ($sxe->children() as $key => $value) {
if (array_key_exists($key, $extract)) {
if (!isset($extract[$key][0])) {
$tmp_extract = $extract[$key];
$extract[$key] = array();
$extract[$key][0] = $tmp_extract;
} else {
$extract[$key] = (array) $extract[$key];
}
}
if ($value->count()) {
if (isset($extract[$key]) && is_array($extract[$key]))
$extract[$key][] = $this->extract($value);
else
$extract[$key] = $this->extract($value);
} else {
if (isset($extract[$key]) && is_array($extract[$key]))
$extract[$key][] = empty(strval($value)) ? null : strval($value);
else
$extract[$key] = empty(strval($value)) ? null : strval($value);
}
}
return $extract;
}
private function processFields($fields, $body) {
foreach ($fields as $key => $field) {
if (is_array($field)) {
$body = $this->processFields($field, $body);
} else {
$body[strtolower($key)] = $field;
}
}
return $body;
}
}