Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
91.67% covered (success)
91.67%
11 / 12
83.33% covered (warning)
83.33%
10 / 12
77.78% covered (warning)
77.78%
7 / 9
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
DomainExceptionNormalizer
91.67% covered (success)
91.67%
11 / 12
83.33% covered (warning)
83.33%
10 / 12
77.78% covered (warning)
77.78%
7 / 9
80.00% covered (warning)
80.00%
4 / 5
11.10
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%
6 / 6
83.33% covered (warning)
83.33%
5 / 6
75.00% covered (warning)
75.00%
3 / 4
100.00% covered (success)
100.00%
1 / 1
5.39
 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\Shared\Infrastructure\Normalizer;
4
5use App\Shared\Domain\Exception\DomainException;
6use App\Shared\Domain\Exception\EntityAlreadyExistsException;
7use App\Shared\Domain\Exception\EntityNotFoundException;
8use App\Shared\Domain\Exception\FileStorageException;
9use App\Shared\Domain\Exception\ValidationException;
10use InvalidArgumentException;
11use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
12use Symfony\Component\HttpFoundation\Response;
13use Throwable;
14
15#[AutoconfigureTag('app.exception_normalizer')]
16class 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}