Completed
Push — master ( 9dfe4a...9f73a9 )
by Alexey
03:27
created

RequestDtoExceptionNormalizer::normalize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 8
c 1
b 0
f 1
nc 2
nop 3
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 2
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 19
    public function __construct(
19
        ConstraintViolationListNormalizer $normalizer,
20
        bool $debug = false,
21
        array $defaultContext = []
22
    ) {
23 19
        parent::__construct($debug, $defaultContext);
24 19
        $this->normalizer = $normalizer;
25 19
        $this->debug = $debug;
26 19
    }
27
28
    /**
29
     * @param RequestDtoValidationException $exception
30
     *
31
     * @throws ExceptionInterface
32
     *
33
     * @return array|\ArrayObject|bool|float|int|string|null
34
     */
35 3
    public function normalize($exception, ?string $format = null, array $context = [])
36
    {
37
        $context += [
38 3
            'type' => 'https://tools.ietf.org/html/rfc7807',
39
        ];
40 3
        $data = $this->normalizer->normalize($exception->getErrors(), $format, $context);
41 3
        $data['status'] = $exception->getStatusCode();
42
43 3
        if ($this->debug) {
44 3
            $data['class'] = \get_class($exception);
45 3
            $data['trace'] = $exception->getTrace();
46
        }
47
48 3
        return $data;
49
    }
50
51 18
    public function supportsNormalization($data, ?string $format = null): bool
52
    {
53 18
        return $data instanceof RequestDtoValidationException;
54
    }
55
}
56