supportsNormalization()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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