129 lines
4.1 KiB
PHP
129 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\BreedersDatas;
|
|
use App\Models\TraceabilityDatas;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use SimpleXMLElement;
|
|
|
|
class XmlTracaReader extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'tracabilite: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 int
|
|
*/
|
|
public function handle()
|
|
{
|
|
$this->info('Démarrage du traitement');
|
|
$files = Storage::disk('tracabilite')->files();
|
|
|
|
// Suppression des fichiers dans archives qui ont plus de 2j
|
|
$archives = Storage::disk('tracabilite')->files('archives');
|
|
foreach ($archives as $archive) {
|
|
if($archive == ".gitignore" || $archive == ".gitkeep") continue;
|
|
if(Storage::disk('tracabilite')->lastModified($archive) < strtotime(Carbon::now()->subDays(2)))
|
|
Storage::disk('tracabilite')->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
|
|
$file = Storage::disk('tracabilite')->get($filename);
|
|
|
|
// TODO : Voir si on laisse comme ça / Ajout des entités Root pour le XML au moment du chargement (non présente dans les fichiers envoyés)
|
|
$fileContent = simplexml_load_string('<Root>' . $file . '</Root>');
|
|
|
|
// Transforme le fichier en tableau de données
|
|
$tracas = $this->extract($fileContent);
|
|
|
|
// Mise en BDD des données du fichier xml
|
|
$fields = [];
|
|
foreach ($tracas['ENRLOT'] as $traca)
|
|
$fields[] = $this->processFields($traca);
|
|
|
|
// Mise en base des données
|
|
foreach ($fields as $field) {
|
|
$checkFields = ['no_lot' => $field["no_lot"], 'no_opa' => $field['no_opa'], 'no_elev' => $field['no_elev']];
|
|
TraceabilityDatas::updateOrCreate($checkFields, $field);
|
|
}
|
|
|
|
Storage::disk('tracabilite')->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)) && strval($value) !== '0' ? null : strval($value);
|
|
else
|
|
$extract[$key] = empty(strval($value)) && strval($value) !== '0' ? null : strval($value);
|
|
}
|
|
}
|
|
|
|
return $extract;
|
|
}
|
|
|
|
private function processFields($fields) {
|
|
$result = [];
|
|
foreach ($fields as $key => $field) {
|
|
// Le ctype_upper est la pour les balise entièrement en majuscule
|
|
$result[strtolower($key)] = $field;
|
|
}
|
|
return $result;
|
|
}
|
|
}
|