ldc-dam/app/Jobs/Arcadia/XmlArcadiaMediasIn.php
Jérémy Munsch 3adefc6fdc
Ticket 754: Rewrite Arcadia Xml in job:
- composer require kwaadpepper/enum
- add MAIL_LIST_SERVICEWEB var to .env
- add XmlJobs, XArcadiaFlux
- ArcadiaLink is now a Job instead of a simple comand
- Add real progressbar to XArcadiaFlux
- Add XmlArcadiaMediasIn to Kernel to execute twice daily on one server
- Add Lib\Utils class with sendEmailErrorManually until laravel bump is done
- Changed XmlJobs can now handle xml files which have only one entity within
- Add guessed XSD
- Changed XmlJobs can now validate xml with xsd before parse
- Changed XmlJobs Report Query Exceptions on save
- Added XmlJobs Add save callback to handle save
- Extensive tests
2022-03-25 13:59:30 +01:00

145 lines
4.4 KiB
PHP

<?php
namespace App\Jobs\Arcadia;
use App\Exceptions\AbortXmlImportException;
use App\Exceptions\ElasticSearchException;
use App\Lib\Utils;
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'] = 0;
}, 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 = 0;
$media->saveOrFail();
} catch (ModelNotFoundException $e) {
$e = new AbortXmlImportException(
sprintf('La fiche media \'%s\' ne peut être trouvé', \json_encode($uniqueColumnsKey)),
0,
1,
__FILE__,
__LINE__,
$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(
"La fiche media '%s' ne peut être trouvé",
0,
1,
__FILE__,
__LINE__,
$e
);
throw $e;
}
try {
// * Suppression dans la supervision Arcadia Link
$arcadiaLink = ArcadiaLink::whereHas('media', function ($q) use ($media) {
$q->whereId($media->id);
})->first();
if ($arcadiaLink) {
$arcadiaLink->delete();
}
// * Index ElasticSearch
$esExist = Media::searchByQuery([
'match' => ['arcadia_id' => $media->arcadia_id ?? '']
]);
// * Remove all from elastic if any duplicates
if ($esExist->totalHits() > 0) {
$media->removeFromIndex();
}
// * Save into elastic
$media->addToIndex();
} catch (\Exception $e) {
$report = new ElasticSearchException(
'Something went wrong when updating media into elastic search',
0,
__FILE__,
__LINE__,
$e
);
// ! Someting went wrong
report($report);
// ! TODO: Remove this after adding kwaadpepper/exception-handler.
Utils::sendEmailErrorManually($report);
throw new AbortXmlImportException($report->getMessage());
} //end try
});
}
}