Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
90.00% |
9 / 10 |
|
75.00% |
3 / 4 |
|
75.00% |
3 / 4 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ExceptionNormalizer | |
90.00% |
9 / 10 |
|
75.00% |
3 / 4 |
|
75.00% |
3 / 4 |
|
75.00% |
3 / 4 |
4.25 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| normalizeErrors | n/a |
0 / 0 |
n/a |
0 / 0 |
n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||||
| getStatus | n/a |
0 / 0 |
n/a |
0 / 0 |
n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||||
| getErrorCode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getMessage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| normalize | |
100.00% |
7 / 7 |
|
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\Translation\TranslatorInterface; |
| 7 | use Symfony\Component\Clock\ClockAwareTrait; |
| 8 | use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag; |
| 9 | use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
| 10 | |
| 11 | /** |
| 12 | * @author Wilhelm Zwertvaegher |
| 13 | */ |
| 14 | #[AutoconfigureTag('app.exception_normalizer')] |
| 15 | abstract class ExceptionNormalizer implements NormalizerInterface |
| 16 | { |
| 17 | use ClockAwareTrait; |
| 18 | |
| 19 | public function __construct(protected readonly TranslatorInterface $translator) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * @return array<string, array<string, string[]|int[]>> |
| 25 | */ |
| 26 | abstract protected function normalizeErrors(\Throwable $throwable): array; |
| 27 | |
| 28 | abstract protected function getStatus(\Throwable $throwable): int; |
| 29 | |
| 30 | protected function getErrorCode(\Throwable $throwable): ErrorCode |
| 31 | { |
| 32 | return ErrorCode::INTERNAL; |
| 33 | } |
| 34 | |
| 35 | protected function getMessage(\Throwable $throwable): string |
| 36 | { |
| 37 | return $this->translator->translate($throwable->getMessage()); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @param array<string, mixed> $context |
| 42 | * |
| 43 | * @return array<string, mixed> |
| 44 | */ |
| 45 | final public function normalize(mixed $data, ?string $format = null, array $context = []): array |
| 46 | { |
| 47 | return [ |
| 48 | 'timestamp' => $this->now()->format(\DateTimeInterface::RFC3339_EXTENDED), |
| 49 | 'status' => $this->getStatus($data), |
| 50 | 'error' => $this->getErrorCode($data), |
| 51 | 'message' => $this->translator->translate($this->getMessage($data)), |
| 52 | 'errors' => $this->normalizeErrors($data), |
| 53 | ]; |
| 54 | } |
| 55 | } |