159 lines
5.2 KiB
PHP
159 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\IncoDatas;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use SimpleXMLElement;
|
|
|
|
class XmlIncoReader implements ShouldQueue
|
|
{
|
|
use Dispatchable;
|
|
use InteractsWithQueue;
|
|
use Queueable;
|
|
use SerializesModels;
|
|
|
|
public $console = null;
|
|
|
|
public function handle()
|
|
{
|
|
$this->log(__CLASS__ . ' 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);
|
|
}
|
|
}
|
|
|
|
$progress = 0;
|
|
$total = count($files) - 1;
|
|
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 enregistrer 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);
|
|
$this->log(__CLASS__ . sprintf(' traitement %d sur %s ', $progress++, $total));
|
|
}
|
|
$this->log(__CLASS__ . ' Fin du traitement');
|
|
}
|
|
|
|
private function log(string $message)
|
|
{
|
|
if ($this->console) {
|
|
$this->console->info($message);
|
|
}
|
|
Log::debug($message);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|