diff --git a/app/Jobs/CafelEggBreederJob.php b/app/Jobs/CafelEggBreederJob.php index 23812dd..86265b3 100644 --- a/app/Jobs/CafelEggBreederJob.php +++ b/app/Jobs/CafelEggBreederJob.php @@ -5,7 +5,9 @@ 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 { @@ -19,7 +21,6 @@ class CafelEggBreederJob extends CafelJobs // ! 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(); @@ -52,8 +53,23 @@ class CafelEggBreederJob extends CafelJobs ) : null; } catch (CafelApiImageFetchException | CafelApiException $e) { report($e); - $row['path_picture_1'] = null; - } + $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( @@ -62,8 +78,23 @@ class CafelEggBreederJob extends CafelJobs ) : null; } catch (CafelApiImageFetchException | CafelApiException $e) { report($e); - $row['path_picture_2'] = null; - } + $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; diff --git a/app/Jobs/CafelJobs.php b/app/Jobs/CafelJobs.php index bee0243..ba269ee 100644 --- a/app/Jobs/CafelJobs.php +++ b/app/Jobs/CafelJobs.php @@ -73,16 +73,21 @@ abstract class CafelJobs implements ShouldQueue /** @var \Illuminate\Validation\Validator $validator */ $validator = $e->validator; /** @var array */ - $failed = $validator->invalid(); + $failed = $validator->invalid(); $this->invalidRows[] = collect($row)->put('validation_message', collect($e->errors()) ->mapWithKeys(function (array $messages, string $attribute) use ($failed) { + if (!array_key_exists($attribute, $failed)) { + throw new \RuntimeException( + "ValidationException is missing faling data for '{$attribute}'" + ); + } return [ $attribute => collect($messages)->implode(', ') . ' Valeur vérifiée : ' . \json_encode($failed[$attribute]) ]; })->implode(' '))->all(); } - } + }//end foreach } while (!empty($result)); } diff --git a/app/Jobs/CafelPoultryBreederJob.php b/app/Jobs/CafelPoultryBreederJob.php index dd256c1..1d97797 100644 --- a/app/Jobs/CafelPoultryBreederJob.php +++ b/app/Jobs/CafelPoultryBreederJob.php @@ -5,7 +5,9 @@ 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 { @@ -19,7 +21,6 @@ class CafelPoultryBreederJob extends CafelJobs // ! 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(); @@ -52,8 +53,23 @@ class CafelPoultryBreederJob extends CafelJobs ) : null; } catch (CafelApiImageFetchException | CafelApiException $e) { report($e); - $row['path_picture_1'] = null; - } + $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( @@ -62,8 +78,23 @@ class CafelPoultryBreederJob extends CafelJobs ) : null; } catch (CafelApiImageFetchException | CafelApiException $e) { report($e); - $row['path_picture_2'] = null; - } + $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; diff --git a/app/Lib/CafelApi/Transport/ApiInterface.php b/app/Lib/CafelApi/Transport/ApiInterface.php index 57b9eb4..23fd42e 100644 --- a/app/Lib/CafelApi/Transport/ApiInterface.php +++ b/app/Lib/CafelApi/Transport/ApiInterface.php @@ -105,7 +105,7 @@ class ApiInterface * * @param \App\Lib\CafelApi\Transport\Request $request * @return \App\Lib\CafelApi\Transport\Response - * @throws \App\Exceptions\CafelApiException If reponse status is KO. + * @throws \App\Exceptions\CafelApiException If reponse status is KO or not Json. */ public function call(Request $request): Response { @@ -122,8 +122,9 @@ class ApiInterface /** * Process request * - * @param Request $request - * @return Response + * @param \App\Lib\CafelApi\Transport\Request $request + * @return \App\Lib\CafelApi\Transport\Response + * @throws \App\Exceptions\CafelApiException If HTTP response body in not JSON. */ private function processRequest(Request $request): Response { @@ -136,13 +137,13 @@ class ApiInterface $status = in_array($response->getStatusCode(), [200, 201]) ? ApiStatus::OK() : ApiStatus::KO(); - return new Response($status, $response); + return new Response($status, $response, $request); } /** * Add spécific headers to the request * - * @param Request $request + * @param \App\Lib\CafelApi\Transport\Request $request * @return void */ private function setHeaders(Request &$request): void diff --git a/app/Lib/CafelApi/Transport/Response.php b/app/Lib/CafelApi/Transport/Response.php index a6dcdda..8f6423d 100644 --- a/app/Lib/CafelApi/Transport/Response.php +++ b/app/Lib/CafelApi/Transport/Response.php @@ -56,9 +56,10 @@ class Response * * @param \App\Enums\ApiStatus $status * @param \Psr\Http\Message\ResponseInterface $response + * @param \App\Lib\CafelApi\Transport\Request $request * @throws \App\Exceptions\CafelApiException If HTTP response body in not JSON. */ - public function __construct(ApiStatus $status, ResponseInterface $response) + public function __construct(ApiStatus $status, ResponseInterface $response, Request $request) { $this->response = $response; $this->status = $status; @@ -74,9 +75,18 @@ class Response // * Body should be json data. if (($decodedJson = $this->tryDecodeJson($this->body->getContents())) === false) { if (in_array(config('app.env'), ['testing', 'local'])) { - dump((string)$this->body); + dump( + 'should have been json', + $this->httpCode, + $this->status, + $this->headers, + $request->uri, + (string)$this->body + ); } - throw new CafelApiException('The Api should have returned JSON data'); + throw new CafelApiException( + "The Api should have returned JSON data '{$request->uri}'" + ); } $this->body = $decodedJson; diff --git a/app/Lib/ToolboxHelper.php b/app/Lib/ToolboxHelper.php index 71276b1..4256abb 100644 --- a/app/Lib/ToolboxHelper.php +++ b/app/Lib/ToolboxHelper.php @@ -12,6 +12,23 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; abstract class ToolboxHelper { + /** + * Update any prop using reflexion + * + * @param object $object + * @param string $property + * @param mixed $value + * @return void + * @throws \ReflectionException If the class or property does not exist. + */ + public static function useReflectionToUpdateProperty(object &$object, string $property, $value) + { + $refProperty = new \ReflectionProperty(\get_class($object), $property); + $refProperty->setAccessible(true); + $refProperty->setValue($object, $value); + $refProperty->setAccessible(false); + } + /** * Store an image of different types *