Passed
Pull Request — main (#1)
by Morteza
07:13
created

hasCacheableSupportsMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * This file is part of the bugloos/error-response-bundle project.
5
 * (c) Bugloos <https://bugloos.com/>
6
 * For the full copyright and license information, please view
7
 * the LICENSE file that was distributed with this source code.
8
 */
9
10
namespace Bugloos\ErrorResponseBundle\Normalizer;
11
12
use Bugloos\ErrorResponseBundle\Exception\FormException;
13
use Symfony\Component\ErrorHandler\Exception\FlattenException;
14
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
15
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
16
17
class ErrorResponseNormalizer extends AbstractErrorNormalizer implements CacheableSupportsMethodInterface
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function normalize($object, string $format = null, array $context = []): array
23
    {
24
        if (!$object instanceof FlattenException) {
25
            throw new InvalidArgumentException(sprintf('The object must implement "%s".', FlattenException::class));
26
        }
27
        $context += $this->defaultContext;
28
        $debug = $this->debug && ($context['debug'] ?? true);
29
30
        return $this->createResponse($debug, $object, $context);
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     *
36
     * @param array $context
37
     */
38
    public function supportsNormalization($data, string $format = null, array $context = []): bool
39
    {
40
        return $data instanceof FlattenException && !$context['exception'] instanceof FormException;
41
    }
42
43
    public function hasCacheableSupportsMethod(): bool
44
    {
45
        return true;
46
    }
47
}
48