RequestDtoExceptionNormalizer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 15
c 1
b 0
f 1
dl 0
loc 42
ccs 15
cts 15
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsNormalization() 0 3 1
A normalize() 0 14 2
A __construct() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nelexa\RequestDtoBundle\Normalizer;
6
7
use Nelexa\RequestDtoBundle\Exception\RequestDtoValidationException;
8
use Symfony\Component\Serializer\Exception\ExceptionInterface;
9
use Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer;
10
use Symfony\Component\Serializer\Normalizer\ProblemNormalizer;
11
12
class RequestDtoExceptionNormalizer extends ProblemNormalizer
13
{
14
    private ConstraintViolationListNormalizer $normalizer;
15
16
    private bool $debug;
17
18 26
    public function __construct(
19
        ConstraintViolationListNormalizer $normalizer,
20
        bool $debug = false,
21
        array $defaultContext = []
22
    ) {
23 26
        parent::__construct($debug, $defaultContext);
24 26
        $this->normalizer = $normalizer;
25 26
        $this->debug = $debug;
26 26
    }
27
28
    /**
29
     * @param RequestDtoValidationException $exception
30
     *
31
     * @throws ExceptionInterface
32
     *
33
     * @return array|\ArrayObject|bool|float|int|string|null
34
     */
35 4
    public function normalize($exception, ?string $format = null, array $context = [])
36
    {
37
        $context += [
38 4
            'type' => 'https://tools.ietf.org/html/rfc7807',
39
        ];
40 4
        $data = $this->normalizer->normalize($exception->getErrors(), $format, $context);
41 4
        $data['status'] = $exception->getStatusCode();
42
43 4
        if ($this->debug) {
44 4
            $data['class'] = \get_class($exception);
45 4
            $data['trace'] = $exception->getTrace();
46
        }
47
48 4
        return $data;
49
    }
50
51 25
    public function supportsNormalization($data, ?string $format = null): bool
52
    {
53 25
        return $data instanceof RequestDtoValidationException;
54
    }
55
}
56