Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
94.74% |
18 / 19 |
|
95.24% |
20 / 21 |
|
29.17% |
7 / 24 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| UnprocessableEntityHttpExceptionNormalizer | |
94.74% |
18 / 19 |
|
95.24% |
20 / 21 |
|
29.17% |
7 / 24 |
|
83.33% |
5 / 6 |
63.18 | |
0.00% |
0 / 1 |
| supportsNormalization | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| normalizeErrors | |
92.86% |
13 / 14 |
|
93.75% |
15 / 16 |
|
10.53% |
2 / 19 |
|
0.00% |
0 / 1 |
42.10 | |||
| getSupportedTypes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getErrorCode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMessage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getStatus | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Normalizer; |
| 4 | |
| 5 | use App\Exception\ErrorCode; |
| 6 | use App\Exception\ErrorMessage; |
| 7 | use Symfony\Component\Clock\ClockAwareTrait; |
| 8 | use Symfony\Component\HttpFoundation\Response; |
| 9 | use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; |
| 10 | use Symfony\Component\Validator\Constraint; |
| 11 | use Symfony\Component\Validator\Exception\ValidationFailedException; |
| 12 | |
| 13 | class UnprocessableEntityHttpExceptionNormalizer extends ExceptionNormalizer |
| 14 | { |
| 15 | use ClockAwareTrait; |
| 16 | |
| 17 | /** |
| 18 | * @param array<string, mixed> $context |
| 19 | */ |
| 20 | public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool |
| 21 | { |
| 22 | return $data instanceof UnprocessableEntityHttpException; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @return array<string, array<string, string[]|int[]>> |
| 27 | */ |
| 28 | protected function normalizeErrors(\Throwable $throwable): array |
| 29 | { |
| 30 | if (!$throwable instanceof UnprocessableEntityHttpException) { |
| 31 | throw new \InvalidArgumentException(); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @var ValidationFailedException $validationFailedException |
| 36 | */ |
| 37 | $validationFailedException = $throwable->getPrevious(); |
| 38 | |
| 39 | $errorsAsArray = []; |
| 40 | foreach ($validationFailedException->getViolations() as $violation) { |
| 41 | $propertyPath = $violation->getPropertyPath(); |
| 42 | /** @var class-string<Constraint> $constraintClass */ |
| 43 | $constraintClass = $violation->getConstraint() ? $violation->getConstraint()::class : null; |
| 44 | $detailCode = $constraintClass ? $constraintClass::getErrorName($violation->getCode()) : 'UNKNOWN_ERROR'; |
| 45 | if (!isset($errorsAsArray[$propertyPath])) { |
| 46 | $errorsAsArray[$propertyPath] = []; |
| 47 | } |
| 48 | if (!isset($errorsAsArray[$propertyPath][$detailCode])) { |
| 49 | $errorsAsArray[$propertyPath][$detailCode] = []; |
| 50 | } |
| 51 | $errorsAsArray[$propertyPath][$detailCode][strtolower($detailCode)] = $this->translator->translate($violation->getMessage()); |
| 52 | } |
| 53 | |
| 54 | return $errorsAsArray; |
| 55 | } |
| 56 | |
| 57 | public function getSupportedTypes(?string $format): array |
| 58 | { |
| 59 | return [UnprocessableEntityHttpException::class => true]; |
| 60 | } |
| 61 | |
| 62 | #[\Override] |
| 63 | protected function getErrorCode(\Throwable $throwable): ErrorCode |
| 64 | { |
| 65 | return ErrorCode::VALIDATION_ERROR; |
| 66 | } |
| 67 | |
| 68 | protected function getMessage(\Throwable $throwable): string |
| 69 | { |
| 70 | return ErrorMessage::VALIDATION_FAILED; |
| 71 | } |
| 72 | |
| 73 | #[\Override] |
| 74 | protected function getStatus(\Throwable $throwable): int |
| 75 | { |
| 76 | return Response::HTTP_UNPROCESSABLE_ENTITY; |
| 77 | } |
| 78 | } |