2019-07-11 11:45:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
2022-06-20 14:57:58 +02:00
|
|
|
use App\Traits\Http\HandlesMedia;
|
2019-09-12 11:08:25 +02:00
|
|
|
use App\Models\Article;
|
|
|
|
|
use App\Models\Marque;
|
2022-04-20 15:23:31 +02:00
|
|
|
use App\Models\Site;
|
|
|
|
|
use App\Models\Societe;
|
2019-09-12 11:08:25 +02:00
|
|
|
use App\Models\Sujet;
|
2022-06-20 14:57:58 +02:00
|
|
|
use App\Models\TypeMedia;
|
|
|
|
|
use App\Models\TypeVue;
|
2019-09-12 11:08:25 +02:00
|
|
|
use App\Models\Variante;
|
2022-04-20 15:23:31 +02:00
|
|
|
use Illuminate\Console\Command;
|
2022-06-20 14:57:58 +02:00
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
2022-04-20 15:23:31 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
2019-07-11 11:45:47 +02:00
|
|
|
|
2022-06-20 14:57:58 +02:00
|
|
|
class ImportArcadiaModelFiles extends Command
|
2019-07-11 11:45:47 +02:00
|
|
|
{
|
2022-06-20 14:57:58 +02:00
|
|
|
use HandlesMedia;
|
2019-07-17 15:48:23 +02:00
|
|
|
|
2019-07-11 11:45:47 +02:00
|
|
|
/**
|
|
|
|
|
* The name and signature of the console command.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected $signature = 'arcadia:import';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The console command description.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2022-06-20 14:57:58 +02:00
|
|
|
protected $description = 'Import des modèles xml Arcadia';
|
2019-07-11 11:45:47 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute the console command.
|
|
|
|
|
*
|
|
|
|
|
* @return mixed
|
2022-04-20 15:23:31 +02:00
|
|
|
* @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
|
2019-07-11 11:45:47 +02:00
|
|
|
*/
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
2022-06-20 14:57:58 +02:00
|
|
|
/** @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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-19 16:07:27 +02:00
|
|
|
$this->info('Démarrage du traitement');
|
2022-06-20 14:57:58 +02:00
|
|
|
|
|
|
|
|
$bar = $this->output->createProgressBar($files->count() - 1);
|
2022-04-19 16:07:27 +02:00
|
|
|
$bar->start();
|
|
|
|
|
|
|
|
|
|
// ---- Pour chaque fichier
|
|
|
|
|
foreach ($files as $filename) {
|
|
|
|
|
// ---- Récupération du contenu
|
2022-04-20 15:23:31 +02:00
|
|
|
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.");
|
|
|
|
|
}
|
2022-04-19 16:07:27 +02:00
|
|
|
|
|
|
|
|
// ---- traitement du fichier
|
2022-04-20 15:23:31 +02:00
|
|
|
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
|
2022-04-19 16:07:27 +02:00
|
|
|
$bar->advance();
|
2022-04-20 15:23:31 +02:00
|
|
|
} //end foreach
|
2019-07-11 11:45:47 +02:00
|
|
|
|
2022-04-19 16:07:27 +02:00
|
|
|
$bar->finish();
|
2022-06-20 14:57:58 +02:00
|
|
|
$this->newLine();
|
2022-04-19 16:07:27 +02:00
|
|
|
$this->info('Fin du traitement');
|
2019-07-11 11:45:47 +02:00
|
|
|
}
|
2022-04-20 15:23:31 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 */
|
2025-10-24 11:19:49 +02:00
|
|
|
$record = Article::where('noArt', (int)$row->noArt)->first();
|
2022-06-20 14:57:58 +02:00
|
|
|
if (!$record) {
|
|
|
|
|
$record = new Article();
|
2025-10-24 11:19:49 +02:00
|
|
|
$record->noArt = (int)$row->noArt;
|
2022-06-20 14:57:58 +02:00
|
|
|
}
|
2025-10-24 11:19:49 +02:00
|
|
|
$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;
|
2022-06-20 14:57:58 +02:00
|
|
|
$record->saveOrFail();
|
2022-04-20 15:23:31 +02:00
|
|
|
} //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 */
|
2025-10-24 11:19:49 +02:00
|
|
|
$record = Marque::where('codMarq', (int)$row->codMarq)->first();
|
2022-06-20 14:57:58 +02:00
|
|
|
if (!$record) {
|
2022-04-20 15:23:31 +02:00
|
|
|
$record = new Marque();
|
2025-10-24 11:19:49 +02:00
|
|
|
$record->codMarq = (int)$row->codMarq;
|
2022-04-20 15:23:31 +02:00
|
|
|
}
|
2025-10-24 11:19:49 +02:00
|
|
|
$record->libMarq = (string)$row->libMarq;
|
2022-06-20 14:57:58 +02:00
|
|
|
$record->saveOrFail();
|
2022-04-20 15:23:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 */
|
2025-10-24 11:19:49 +02:00
|
|
|
$record = Sujet::where('codeSujet', (int)$row->code)->first();
|
2022-06-20 14:57:58 +02:00
|
|
|
if (!$record) {
|
2022-04-20 15:23:31 +02:00
|
|
|
$record = new Sujet();
|
2025-10-24 11:19:49 +02:00
|
|
|
$record->codeSujet = (int)$row->code;
|
2022-04-20 15:23:31 +02:00
|
|
|
}
|
2025-10-24 11:19:49 +02:00
|
|
|
$record->libelle = (string)$row->libelle;
|
2022-06-20 14:57:58 +02:00
|
|
|
$record->saveOrFail();
|
2022-04-20 15:23:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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) {
|
2022-06-20 14:57:58 +02:00
|
|
|
/** @var \App\Models\TypeMedia|null $record */
|
2025-10-24 11:19:49 +02:00
|
|
|
$record = TypeMedia::where('codeTypemedia', (int)$row->code)->first();
|
2022-06-20 14:57:58 +02:00
|
|
|
if (!$record) {
|
|
|
|
|
$record = new TypeMedia();
|
2025-10-24 11:19:49 +02:00
|
|
|
$record->codeTypemedia = (int)$row->code;
|
2022-04-20 15:23:31 +02:00
|
|
|
}
|
2025-10-24 11:19:49 +02:00
|
|
|
$record->libelle = (string)$row->libelle;
|
2022-06-20 14:57:58 +02:00
|
|
|
$record->saveOrFail();
|
2022-04-20 15:23:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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) {
|
2022-06-20 14:57:58 +02:00
|
|
|
/** @var \App\Models\TypeVue|null $record */
|
2025-10-24 11:19:49 +02:00
|
|
|
$record = TypeVue::where('codeTypevue', (int)$row->code)->first();
|
2022-06-20 14:57:58 +02:00
|
|
|
if (!$record) {
|
|
|
|
|
$record = new TypeVue();
|
2025-10-24 11:19:49 +02:00
|
|
|
$record->codeTypevue = (int)$row->code;
|
2022-04-20 15:23:31 +02:00
|
|
|
}
|
2025-10-24 11:19:49 +02:00
|
|
|
$record->libelle = (string)$row->libelle;
|
|
|
|
|
$record->sujet_id = (int)$row->sujet_id->mediasujet->code;
|
2022-06-20 14:57:58 +02:00
|
|
|
$record->saveOrFail();
|
2022-04-20 15:23:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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) {
|
2022-06-20 14:57:58 +02:00
|
|
|
/** @var \App\Models\TypeVue|null $record */
|
2025-10-24 11:19:49 +02:00
|
|
|
$record = Variante::where('codeVariante', (int)$row->code)->first();
|
2022-06-20 14:57:58 +02:00
|
|
|
if (!$record) {
|
2022-04-20 15:23:31 +02:00
|
|
|
$record = new Variante();
|
2025-10-24 11:19:49 +02:00
|
|
|
$record->codeVariante = (int)$row->code;
|
2022-04-20 15:23:31 +02:00
|
|
|
}
|
2025-10-24 11:19:49 +02:00
|
|
|
$record->libelle = (string)$row->libelle;
|
|
|
|
|
$record->sujet_id = (int)$row->sujet_id->children()[0]->code;
|
|
|
|
|
$record->type_vue_id = (int)$row->CodeTypeVue->mediatypesvue->code;
|
2022-06-20 14:57:58 +02:00
|
|
|
$record->saveOrFail();
|
2022-04-20 15:23:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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) {
|
2022-06-20 14:57:58 +02:00
|
|
|
/** @var \App\Models\TypeVue|null $record */
|
2025-10-24 11:19:49 +02:00
|
|
|
$record = Site::where('id', (int)$row->code)->first();
|
2022-06-20 14:57:58 +02:00
|
|
|
if (!$record) {
|
|
|
|
|
$record = new Site();
|
2025-10-24 11:19:49 +02:00
|
|
|
$record->id = (int)$row->code;
|
2022-04-20 15:23:31 +02:00
|
|
|
}
|
2025-10-24 11:19:49 +02:00
|
|
|
$record->libelle = (string)$row->libelle;
|
2022-06-20 14:57:58 +02:00
|
|
|
$record->saveOrFail();
|
2022-04-20 15:23:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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) {
|
2022-06-20 14:57:58 +02:00
|
|
|
/** @var \App\Models\TypeVue|null $record */
|
2025-10-24 11:19:49 +02:00
|
|
|
$record = Societe::where('id', (int)$row->codSociete)->first();
|
2022-06-20 14:57:58 +02:00
|
|
|
if (!$record) {
|
|
|
|
|
$record = new Societe();
|
2025-10-24 11:19:49 +02:00
|
|
|
$record->id = (int)$row->codSociete;
|
2022-04-20 15:23:31 +02:00
|
|
|
}
|
2025-10-24 11:19:49 +02:00
|
|
|
$record->libelle = (string)$row->nomSociete;
|
2022-06-20 14:57:58 +02:00
|
|
|
$record->saveOrFail();
|
2022-04-20 15:23:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-11 11:45:47 +02:00
|
|
|
}
|