Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
90.00% |
9 / 10 |
|
72.73% |
8 / 11 |
|
71.43% |
5 / 7 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| DomainExceptionNormalizer | |
90.00% |
9 / 10 |
|
72.73% |
8 / 11 |
|
71.43% |
5 / 7 |
|
80.00% |
4 / 5 |
9.49 | |
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% |
4 / 4 |
|
60.00% |
3 / 5 |
|
50.00% |
1 / 2 |
|
100.00% |
1 / 1 |
4.12 | |||
| 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\Normalizer; |
| 4 | |
| 5 | use App\Exception\DomainException; |
| 6 | use App\Exception\ErrorCode; |
| 7 | use App\Exception\WordAlreadyExistsException; |
| 8 | use Symfony\Component\HttpFoundation\Response; |
| 9 | |
| 10 | class DomainExceptionNormalizer extends ExceptionNormalizer |
| 11 | { |
| 12 | /** |
| 13 | * @var array<class-string, int> |
| 14 | */ |
| 15 | private static array $status_codes = [ |
| 16 | WordAlreadyExistsException::class => Response::HTTP_CONFLICT, |
| 17 | ]; |
| 18 | |
| 19 | /** |
| 20 | * @param array<string, mixed> $context |
| 21 | */ |
| 22 | public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool |
| 23 | { |
| 24 | return $data instanceof DomainException; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * @return array<string, array<string, string[]|int[]>> |
| 29 | */ |
| 30 | protected function normalizeErrors(\Throwable $throwable): array |
| 31 | { |
| 32 | if (!$throwable instanceof DomainException) { |
| 33 | throw new \InvalidArgumentException(); |
| 34 | } |
| 35 | |
| 36 | return []; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @param DomainException $throwable |
| 41 | */ |
| 42 | #[\Override] |
| 43 | protected function getErrorCode(\Throwable $throwable): ErrorCode |
| 44 | { |
| 45 | return match ($throwable::class) { |
| 46 | WordAlreadyExistsException::class => ErrorCode::ENTITY_EXISTS, |
| 47 | default => parent::getErrorCode($throwable), |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | public function getSupportedTypes(?string $format): array |
| 52 | { |
| 53 | return [DomainException::class => true]; |
| 54 | } |
| 55 | |
| 56 | protected function getStatus(\Throwable $throwable): int |
| 57 | { |
| 58 | return self::$status_codes[$throwable::class] ?? Response::HTTP_BAD_REQUEST; |
| 59 | } |
| 60 | } |