ldc-dam/app/Console/Commands/ImportArcadiaModelFiles.php
2025-10-24 11:19:49 +02:00

317 lines
11 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Traits\Http\HandlesMedia;
use App\Models\Article;
use App\Models\Marque;
use App\Models\Site;
use App\Models\Societe;
use App\Models\Sujet;
use App\Models\TypeMedia;
use App\Models\TypeVue;
use App\Models\Variante;
use Illuminate\Console\Command;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
class ImportArcadiaModelFiles extends Command
{
use HandlesMedia;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'arcadia:import';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Import des modèles xml Arcadia';
/**
* Execute the console command.
*
* @return mixed
* @throws \League\Flysystem\FileNotFoundException If a file could not be read.
* @throws \ErrorException If a xml file could not be parsed.
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
*/
public function handle()
{
/** @var \Illuminate\Support\Collection<string> */
$files = collect(Storage::disk('arcadia')->files())
->filter(function (string $fileName) {
return !\in_array($fileName, ['.gitignore']);
});
if (!$files->count()) {
$this->info('Pas de nouveau fichiers');
return 0;
}
$this->info('Démarrage du traitement');
$bar = $this->output->createProgressBar($files->count() - 1);
$bar->start();
// ---- Pour chaque fichier
foreach ($files as $filename) {
// ---- Récupération du contenu
if (!($fileContent = Storage::disk('arcadia')->get($filename))) {
throw new FileNotFoundException($filename);
}
if (!($fileContent = simplexml_load_string($fileContent))) {
throw new \ErrorException("Could not parse $filename from arcadia storage.");
}
// ---- traitement du fichier
switch (true) {
case substr($filename, 0, 11) == 'DAM_ARTICLE':
$this->insertDamArticle($fileContent);
$this->deleteFile($filename);
break;
case substr($filename, 0, 10) == 'DAM_MARQUE':
$this->insertDamMarque($fileContent);
$this->deleteFile($filename);
break;
case substr($filename, 0, 15) == 'DAM_MEDIA_SUJET':
$this->insertDamMediaSujet($fileContent);
$this->deleteFile($filename);
break;
case substr($filename, 0, 20) == 'DAM_MEDIA_TYPE_MEDIA':
$this->insertDamMediaTypeMedia($fileContent);
$this->deleteFile($filename);
break;
case substr($filename, 0, 17) == 'DAM_MEDIA_TYPEVUE':
$this->insertDamMediaTypeVue($fileContent);
$this->deleteFile($filename);
break;
case substr($filename, 0, 18) == 'DAM_MEDIA_VARIANTE':
$this->insertDamMediaVariante($fileContent);
$this->deleteFile($filename);
break;
case substr($filename, 0, 20) == 'DAM_SITE_PROD_GROUPE':
$this->insertDamSiteProdGroupe($fileContent);
$this->deleteFile($filename);
break;
case substr($filename, 0, 11) == 'DAM_SOCIETE':
$this->insertDamSociete($fileContent);
$this->deleteFile($filename);
break;
default:
throw new \ErrorException("Unhandled file $filename");
} //end switch
$bar->advance();
} //end foreach
$bar->finish();
$this->newLine();
$this->info('Fin du traitement');
}
/**
* Update or Create Dam Article
*
* @param \SimpleXMLElement $fileContent
* @return void
* @throws \Throwable On save failure.
*/
private function insertDamArticle(\SimpleXMLElement $fileContent)
{
/** @var \SimpleXMLElement $row */
foreach ($fileContent->dam as $row) {
/** @var \App\Models\Article|null $record */
$record = Article::where('noArt', (int)$row->noArt)->first();
if (!$record) {
$record = new Article();
$record->noArt = (int)$row->noArt;
}
$record->libCcial = (string)$row->libCcial;
$record->codMarq = (int)$row->codMarq;
$record->libMarq = (string)$row->libMarq;
$record->codFamVte = (int)$row->codFamVte;
$record->libFamVte = (string)$row->libFamVte;
$record->codGencodAdm = (string)$row->codGencodAdm;
$record->codGencodSymb = (string)$row->codGencodSymb;
$record->codGencodPds = (string)$row->codGencodPds;
$record->utilisableGroupe = (int)$row->utilisableGroupe;
$record->saveOrFail();
} //end foreach
}
/**
* Update or Create Dam Marque
*
* @param \SimpleXMLElement $fileContent
* @return void
* @throws \Throwable On save failure.
*/
private function insertDamMarque(\SimpleXMLElement $fileContent)
{
/** @var \SimpleXMLElement $row */
foreach ($fileContent->dam as $row) {
/** @var \App\Models\Marque|null $record */
$record = Marque::where('codMarq', (int)$row->codMarq)->first();
if (!$record) {
$record = new Marque();
$record->codMarq = (int)$row->codMarq;
}
$record->libMarq = (string)$row->libMarq;
$record->saveOrFail();
}
}
/**
* Update or Create Dam Media Sujet
*
* @param \SimpleXMLElement $fileContent
* @return void
* @throws \Throwable On save failure.
*/
private function insertDamMediaSujet(\SimpleXMLElement $fileContent)
{
/** @var \SimpleXMLElement $row */
foreach ($fileContent->mediaSujet as $row) {
/** @var \App\Models\Sujet|null $record */
$record = Sujet::where('codeSujet', (int)$row->code)->first();
if (!$record) {
$record = new Sujet();
$record->codeSujet = (int)$row->code;
}
$record->libelle = (string)$row->libelle;
$record->saveOrFail();
}
}
/**
* Update or Create Dam Media Type Media
*
* @param \SimpleXMLElement $fileContent
* @return void
* @throws \Throwable On save failure.
*/
private function insertDamMediaTypeMedia(\SimpleXMLElement $fileContent)
{
/** @var \SimpleXMLElement $row */
foreach ($fileContent->mediatypemedia as $row) {
/** @var \App\Models\TypeMedia|null $record */
$record = TypeMedia::where('codeTypemedia', (int)$row->code)->first();
if (!$record) {
$record = new TypeMedia();
$record->codeTypemedia = (int)$row->code;
}
$record->libelle = (string)$row->libelle;
$record->saveOrFail();
}
}
/**
* Update or Create Dam Media Type Vue
*
* @param \SimpleXMLElement $fileContent
* @return void
* @throws \Throwable On save failure.
*/
private function insertDamMediaTypeVue(\SimpleXMLElement $fileContent)
{
/** @var \SimpleXMLElement $row */
foreach ($fileContent->mediatypesvue as $row) {
/** @var \App\Models\TypeVue|null $record */
$record = TypeVue::where('codeTypevue', (int)$row->code)->first();
if (!$record) {
$record = new TypeVue();
$record->codeTypevue = (int)$row->code;
}
$record->libelle = (string)$row->libelle;
$record->sujet_id = (int)$row->sujet_id->mediasujet->code;
$record->saveOrFail();
}
}
/**
* Update or Create Dam Media Variante
*
* @param \SimpleXMLElement $fileContent
* @return void
* @throws \Throwable On save failure.
*/
private function insertDamMediaVariante(\SimpleXMLElement $fileContent)
{
/** @var \SimpleXMLElement $row */
foreach ($fileContent->mediavariante as $row) {
/** @var \App\Models\TypeVue|null $record */
$record = Variante::where('codeVariante', (int)$row->code)->first();
if (!$record) {
$record = new Variante();
$record->codeVariante = (int)$row->code;
}
$record->libelle = (string)$row->libelle;
$record->sujet_id = (int)$row->sujet_id->children()[0]->code;
$record->type_vue_id = (int)$row->CodeTypeVue->mediatypesvue->code;
$record->saveOrFail();
}
}
/**
* Update or Create Dam Site Prod Groupe
*
* @param \SimpleXMLElement $fileContent
* @return void
* @throws \Throwable On save failure.
*/
private function insertDamSiteProdGroupe(\SimpleXMLElement $fileContent)
{
/** @var \SimpleXMLElement $row */
foreach ($fileContent->SITE_PROD_GRP as $row) {
/** @var \App\Models\TypeVue|null $record */
$record = Site::where('id', (int)$row->code)->first();
if (!$record) {
$record = new Site();
$record->id = (int)$row->code;
}
$record->libelle = (string)$row->libelle;
$record->saveOrFail();
}
}
/**
* Update or Create Dam Societe
*
* @param \SimpleXMLElement $fileContent
* @return void
* @throws \Throwable On save failure.
*/
private function insertDamSociete(\SimpleXMLElement $fileContent)
{
/** @var \SimpleXMLElement $row */
foreach ($fileContent->dam as $row) {
/** @var \App\Models\TypeVue|null $record */
$record = Societe::where('id', (int)$row->codSociete)->first();
if (!$record) {
$record = new Societe();
$record->id = (int)$row->codSociete;
}
$record->libelle = (string)$row->nomSociete;
$record->saveOrFail();
}
}
/**
* Delete file
*
* @param string $filename
* @return void
*/
private function deleteFile(string $filename)
{
if (!Storage::disk('arcadia')->delete($filename)) {
Log::warning("Could not delete $filename from arcadia storage.");
}
}
}