ldc-bogt/app/Jobs/CafelEggBreederJob.php
Jérémy Munsch 7cb304e967
Full traça poulets
Cafel APi Eat
Xml flux new
Full Models Rework
Refactorize
2022-06-08 11:44:01 +02:00

75 lines
2.6 KiB
PHP

<?php
namespace App\Jobs;
use App\Exceptions\CafelApiException;
use App\Exceptions\CafelApiImageFetchException;
use App\Lib\CafelApi\Models\CafelEggBreeder as ApiCafelEggBreeder;
use App\Models\CafelEggBreeder;
class CafelEggBreederJob extends CafelJobs
{
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$this->syncWithCafelApi(
ApiCafelEggBreeder::class,
'cafel.ldc.eleveursoeufs',
[],
function (array $row) {
// * Explode inuav
if (\array_key_exists('inuavs', $row)) {
$row['inuavs'] = explode(',', $row['inuavs']);
}
// * Validate row data
ApiCafelEggBreeder::validateDataRow($row);
// * Cast numeric index
$row['cafel_code'] = intval($row['cafel_code']);
// * Fetch pictures
try {
$row['path_picture_1'] = !empty($row['path_picture_1']) ?
ApiCafelEggBreeder::fetchPicture(
(new CafelEggBreeder())->getTable(),
$row['path_picture_1']
) : null;
} catch (CafelApiImageFetchException | CafelApiException $e) {
report($e);
$row['path_picture_1'] = null;
}
try {
$row['path_picture_2'] = !empty($row['path_picture_2']) ?
ApiCafelEggBreeder::fetchPicture(
(new CafelEggBreeder())->getTable(),
$row['path_picture_2']
) : null;
} catch (CafelApiImageFetchException | CafelApiException $e) {
report($e);
$row['path_picture_2'] = null;
}
foreach (['exploitation', 'aboutyou', 'vocation', 'likeinjob', 'loue_means'] as $fieldName) {
$row[$fieldName] = !empty($row[$fieldName]) ? $row[$fieldName] : null;
}
/** @var array Unique Key columns */
$uniqueColumnsKey = [];
foreach (ApiCafelEggBreeder::UNIQUE_COLUMN_KEYS as $uField) {
$uniqueColumnsKey[$uField] = $row[$uField];
}
forward_static_call_array(
[CafelEggBreeder::class, 'updateOrCreate'],
[$uniqueColumnsKey, $row]
);
}
);
$this->mailInvalidRows(ApiCafelEggBreeder::UNIQUE_COLUMN_KEYS, ApiCafelEggBreeder::class);
}
}