ldc-dam/app/Jobs/Arcadia/XmlArcadiaMediasIn.php
2026-05-06 09:29:16 +02:00

117 lines
3.5 KiB
PHP

<?php
namespace App\Jobs\Arcadia;
use App\Exceptions\AbortXmlImportException;
use App\Models\ArcadiaLink;
use App\Models\Media;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class XmlArcadiaMediasIn extends XmlJobs
{
/**
* The xsd file path used to validate files
*
* @var string|null
*/
protected $xsdSchemaPath = 'resources/documents/arcadia/incoming_medias.xsd';
/**
* Disk name from config/filesystems.php
*
* @var string
*/
protected $disk = 'arcadia_link_r';
/**
* The columns to take from the XML subcolumns can be added with a dot
*
* @var array
*/
protected $takeColumns = [
'code_DAM' => 'id',
'codeArcadia' => 'arcadia_id',
'version' => 'version'
];
/**
* The columns allowed to be null
*
* @var array
*/
protected $nullableFields = [];
/**
* Fields that are unique
*
* @var array
*/
protected $uniqueKeyField = [
'id'
];
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
$this->parseData('MEDIA_VISUEL', Media::class, function (\SimpleXMLElement $xmlData, array &$line) {
// * After XML parse Callback.
// * Unlock media
$line['isLocked'] = false;
}, function (array $uniqueColumnsKey, $line) {
// * DO the DB save yourself callback
try {
/** @var \App\Models\Media $media */
$media = Media::withTrashed()->where($uniqueColumnsKey)->firstOrFail();
$media->arcadia_id = $line['arcadia_id'];
$media->version = $line['version'];
$media->isLocked = false;
$media->saveOrFail();
} catch (ModelNotFoundException $e) {
$e = new AbortXmlImportException(
sprintf('La fiche media \'%s\' ne peut être trouvé', \json_encode($uniqueColumnsKey)),
0,
$e
);
throw $e;
}//end try
//phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
}, function (\SimpleXMLElement $xmlData, array &$line) {
// * After DB save Callback.
$media = null;
try {
/** @var \App\Models\Media $media */
$media = Media::withTrashed()->findOrFail($line['id']);
} catch (ModelNotFoundException $e) {
$e = new AbortXmlImportException(
\sprintf('La fiche media \'%s\' ne peut être trouvé', $line['id']),
0,
$e
);
throw $e;
}
try {
// * Suppression dans la supervision Arcadia Link
/** @var \App\Models\ArcadiaLink|null */
$arcadiaLink = ArcadiaLink::where('media_id', $media->id)->first();
if ($arcadiaLink) {
$arcadiaLink->deleteOrFail();
}
// * Save into elastic if media is not soft deleted, remove otherwise.
!$media->trashed() ? $media->addToElasticSearch() : $media->removeFromElasticSearch();
} catch (\RuntimeException $e) {
throw new AbortXmlImportException($e->getMessage());
} //end try
});
}
}