117 lines
4.6 KiB
PHP
117 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Exceptions\CafelApiException;
|
|
use App\Exceptions\CafelApiImageFetchException;
|
|
use App\Lib\CafelApi\Models\CafelPoultryBreeder as ApiCafelPoultryBreeder;
|
|
use App\Lib\ToolboxHelper;
|
|
use App\Models\CafelPoultryBreeder;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class CafelPoultryBreederJob 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> */
|
|
$poultryBreeders = CafelPoultryBreeder::all();
|
|
$poultryBreeders->each(function (CafelPoultryBreeder $breeder) {
|
|
$breeder->deleteOrFail();
|
|
});
|
|
|
|
$this->syncWithCafelApi(
|
|
ApiCafelPoultryBreeder::class,
|
|
'cafel.ldc.eleveursvolailles',
|
|
[],
|
|
function (array $row) {
|
|
// * Explode inuav
|
|
if (\array_key_exists('inuavs', $row)) {
|
|
$row['inuavs'] = explode(',', $row['inuavs']);
|
|
}
|
|
|
|
// * Validate row data
|
|
ApiCafelPoultryBreeder::validateDataRow($row);
|
|
|
|
// * Cast numeric index
|
|
$row['cafel_code'] = intval($row['cafel_code']);
|
|
|
|
// * Fetch pictures
|
|
try {
|
|
$row['path_picture_1'] = !empty($row['path_picture_1']) ?
|
|
ApiCafelPoultryBreeder::fetchPicture(
|
|
(new CafelPoultryBreeder())->getTable(),
|
|
$row['path_picture_1']
|
|
) : null;
|
|
} catch (CafelApiImageFetchException | CafelApiException $e) {
|
|
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']) ?
|
|
ApiCafelPoultryBreeder::fetchPicture(
|
|
(new CafelPoultryBreeder())->getTable(),
|
|
$row['path_picture_2']
|
|
) : null;
|
|
} catch (CafelApiImageFetchException | CafelApiException $e) {
|
|
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 (['issue_1', 'issue_2'] as $fieldName) {
|
|
$row[$fieldName] = !empty($row[$fieldName]) ? $row[$fieldName] : null;
|
|
}
|
|
|
|
/** @var array Unique Key columns */
|
|
$uniqueColumnsKey = [];
|
|
foreach (ApiCafelPoultryBreeder::UNIQUE_COLUMN_KEYS as $uField) {
|
|
$uniqueColumnsKey[$uField] = $row[$uField];
|
|
}
|
|
|
|
forward_static_call_array(
|
|
[CafelPoultryBreeder::class, 'updateOrCreate'],
|
|
[$uniqueColumnsKey, $row]
|
|
);
|
|
}
|
|
);
|
|
$this->mailInvalidRows(ApiCafelPoultryBreeder::UNIQUE_COLUMN_KEYS, ApiCafelPoultryBreeder::class);
|
|
}
|
|
}
|