Evolution make sure Cafel Calls validation error return incriminated Data.

This commit is contained in:
Jérémy Munsch 2022-10-27 17:12:30 +02:00
parent 35aab2df1f
commit 9db75e5b22
No known key found for this signature in database
GPG key ID: 18361AF3BFEC0FEC
6 changed files with 115 additions and 20 deletions

View file

@ -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;

View file

@ -73,16 +73,21 @@ abstract class CafelJobs implements ShouldQueue
/** @var \Illuminate\Validation\Validator $validator */
$validator = $e->validator;
/** @var array<string,integer|string> */
$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));
}

View file

@ -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;

View file

@ -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

View file

@ -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;

View file

@ -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
*