ldc-bogt/app/Jobs/CafelEggBreederJob.php

118 lines
4.5 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Jobs;
use App\Exceptions\CafelApiException;
use App\Exceptions\CafelApiImageFetchException;
use App\Lib\CafelApi\Models\CafelEggBreeder as ApiCafelEggBreeder;
use App\Lib\ToolboxHelper;
use App\Models\CafelEggBreeder;
use Illuminate\Validation\ValidationException;
class CafelEggBreederJob extends CafelJobs
{
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// ! We have to remove all breeders to reimport them all in order to make sure
// ! to delete breeders that dont exists anymore (Api fetch constrain)
// ! vu avec Sylvain 27/10/2022
// * Destroy breeder one by one to trigger destroy event
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\CafelEggBreeder> */
$eggBreeders = CafelEggBreeder::all();
$eggBreeders->each(function (CafelEggBreeder $breeder) {
$breeder->deleteOrFail();
});
$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) {
2022-10-28 15:15:09 +02:00
report($e);
$validationException = ValidationException::withMessages([
'path_picture_1' => $e->getMessage()
]);
ToolboxHelper::useReflectionToUpdateProperty(
$validationException->validator,
'data',
['path_picture_1' => $row['path_picture_1']]
);
ToolboxHelper::useReflectionToUpdateProperty(
$validationException->validator,
'failedRules',
['path_picture_1' => $e->getMessage()]
);
throw $validationException;
}//end try
try {
$row['path_picture_2'] = !empty($row['path_picture_2']) ?
ApiCafelEggBreeder::fetchPicture(
(new CafelEggBreeder())->getTable(),
$row['path_picture_2']
) : null;
} catch (CafelApiImageFetchException | CafelApiException $e) {
2022-10-28 15:15:09 +02:00
report($e);
$validationException = ValidationException::withMessages([
'path_picture_2' => $e->getMessage()
]);
ToolboxHelper::useReflectionToUpdateProperty(
$validationException->validator,
'data',
['path_picture_2' => $row['path_picture_2']]
);
ToolboxHelper::useReflectionToUpdateProperty(
$validationException->validator,
'failedRules',
['path_picture_2' => $e->getMessage()]
);
throw $validationException;
}//end try
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);
}
}