Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
91.67% |
11 / 12 |
|
83.33% |
10 / 12 |
|
77.78% |
7 / 9 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| DomainExceptionNormalizer | |
91.67% |
11 / 12 |
|
83.33% |
10 / 12 |
|
77.78% |
7 / 9 |
|
80.00% |
4 / 5 |
11.10 | |
0.00% |
0 / 1 |
| supportsNormalization | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| normalizeErrors | |
66.67% |
2 / 3 |
|
66.67% |
2 / 3 |
|
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
2.50 | |||
| getErrorCode | |
100.00% |
6 / 6 |
|
83.33% |
5 / 6 |
|
75.00% |
3 / 4 |
|
100.00% |
1 / 1 |
5.39 | |||
| getSupportedTypes | |
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\Shared\Infrastructure\Normalizer; |
| 4 | |
| 5 | use App\Shared\Domain\Exception\DomainException; |
| 6 | use App\Shared\Domain\Exception\EntityAlreadyExistsException; |
| 7 | use App\Shared\Domain\Exception\EntityNotFoundException; |
| 8 | use App\Shared\Domain\Exception\FileStorageException; |
| 9 | use App\Shared\Domain\Exception\ValidationException; |
| 10 | use InvalidArgumentException; |
| 11 | use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag; |
| 12 | use Symfony\Component\HttpFoundation\Response; |
| 13 | use Throwable; |
| 14 | |
| 15 | #[AutoconfigureTag('app.exception_normalizer')] |
| 16 | class DomainExceptionNormalizer extends ExceptionNormalizer |
| 17 | { |
| 18 | /** |
| 19 | * @var array<class-string, int> |
| 20 | */ |
| 21 | private static array $status_codes = [ |
| 22 | EntityNotFoundException::class => Response::HTTP_NOT_FOUND, |
| 23 | EntityAlreadyExistsException::class => Response::HTTP_CONFLICT, |
| 24 | FileStorageException::class => Response::HTTP_INTERNAL_SERVER_ERROR |
| 25 | ]; |
| 26 | |
| 27 | /** |
| 28 | * @param mixed $data |
| 29 | * @param string|null $format |
| 30 | * @param array<string, mixed> $context |
| 31 | * @return bool |
| 32 | */ |
| 33 | public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool |
| 34 | { |
| 35 | return $data instanceof DomainException; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @param Throwable $throwable |
| 40 | * @return array<string, array<string, string[]|int[]>> |
| 41 | */ |
| 42 | protected function normalizeErrors(Throwable $throwable): array |
| 43 | { |
| 44 | if (!$throwable instanceof DomainException) { |
| 45 | throw new InvalidArgumentException(); |
| 46 | } |
| 47 | return []; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @param DomainException $throwable |
| 52 | * @return string |
| 53 | */ |
| 54 | protected function getErrorCode(Throwable $throwable): string |
| 55 | { |
| 56 | return match ($throwable::class) { |
| 57 | EntityNotFoundException::class => 'entity-not-found', |
| 58 | EntityAlreadyExistsException::class => 'entity-exists', |
| 59 | FileStorageException::class => 'file-storage', |
| 60 | default => 'internal-error' |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | public function getSupportedTypes(?string $format): array |
| 65 | { |
| 66 | return [ValidationException::class => true]; |
| 67 | } |
| 68 | |
| 69 | protected function getStatus(Throwable $throwable): int |
| 70 | { |
| 71 | return self::$status_codes[$throwable::class] ?? Response::HTTP_BAD_REQUEST; |
| 72 | } |
| 73 | } |