Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
90.00% covered (success)
90.00%
9 / 10
72.73% covered (warning)
72.73%
8 / 11
71.43% covered (warning)
71.43%
5 / 7
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
DomainExceptionNormalizer
90.00% covered (success)
90.00%
9 / 10
72.73% covered (warning)
72.73%
8 / 11
71.43% covered (warning)
71.43%
5 / 7
80.00% covered (warning)
80.00%
4 / 5
9.49
0.00% covered (danger)
0.00%
0 / 1
 supportsNormalization
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 normalizeErrors
66.67% covered (warning)
66.67%
2 / 3
66.67% covered (warning)
66.67%
2 / 3
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
2.50
 getErrorCode
100.00% covered (success)
100.00%
4 / 4
60.00% covered (warning)
60.00%
3 / 5
50.00% covered (danger)
50.00%
1 / 2
100.00% covered (success)
100.00%
1 / 1
4.12
 getSupportedTypes
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getStatus
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Normalizer;
4
5use App\Exception\DomainException;
6use App\Exception\ErrorCode;
7use App\Exception\WordAlreadyExistsException;
8use Symfony\Component\HttpFoundation\Response;
9
10class 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}