Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
88.89% covered (warning)
88.89%
8 / 9
66.67% covered (warning)
66.67%
2 / 3
66.67% covered (warning)
66.67%
2 / 3
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExceptionNormalizer
88.89% covered (warning)
88.89%
8 / 9
66.67% covered (warning)
66.67%
2 / 3
66.67% covered (warning)
66.67%
2 / 3
66.67% covered (warning)
66.67%
2 / 3
3.33
0.00% covered (danger)
0.00%
0 / 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% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getMessage
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
 normalize
100.00% covered (success)
100.00%
7 / 7
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\ValidationException;
6use Symfony\Component\Clock\ClockAwareTrait;
7use Symfony\Component\HttpFoundation\Response;
8use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
9
10/**
11 * @author Wilhelm Zwertvaegher
12 */
13abstract class ExceptionNormalizer implements NormalizerInterface
14{
15    use ClockAwareTrait;
16
17    /**
18     * @param \Throwable $throwable
19     * @return array<string, array<string, string[]|int[]>>
20     */
21    abstract protected function normalizeErrors(\Throwable $throwable): array;
22
23    abstract protected function getStatus(\Throwable $throwable): int;
24
25    protected function getErrorCode(\Throwable $throwable): string
26    {
27        return 'internal-error';
28    }
29
30    protected function getMessage(\Throwable $throwable): string
31    {
32        return $throwable->getMessage();
33    }
34
35    /**
36     * @throws \InvalidArgumentException
37     */
38
39    /**
40     * @param mixed $data
41     * @param string|null $format
42     * @param array<string, mixed> $context
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->getMessage($data),
52            'errors' => $this->normalizeErrors($data),
53        ];
54    }
55
56}