Completed
Push — main ( bdd72f...bbe282 )
by Niels
15s queued 12s
created

removeDetailsToPreventInformationDisclosure()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 3
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the OpenapiBundle package.
7
 *
8
 * (c) Niels Nijens <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Nijens\OpenapiBundle\ExceptionHandling\Normalizer;
15
16
use Nijens\OpenapiBundle\ExceptionHandling\Exception\ProblemExceptionInterface;
17
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
18
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
19
use Symfony\Component\Serializer\Exception\LogicException;
20
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
21
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
22
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
23
use Throwable;
24
25
/**
26
 * Normalizes a {@see Throwable} implementing the {@see ProblemExceptionInterface}.
27
 *
28
 * @author Niels Nijens <[email protected]>
29
 */
30
final class ProblemExceptionNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
31
{
32
    use NormalizerAwareTrait;
33
34
    private const ALREADY_CALLED = 'nijens_openapi.problem_exception_normalizer.already_called';
35
36
    /**
37
     * @var bool
38
     */
39
    private $debug;
40
41
    public function __construct(bool $debug = false)
42
    {
43
        $this->debug = $debug;
44
    }
45
46
    public function normalize($object, $format = null, array $context = [])
47
    {
48
        if ($object instanceof ProblemExceptionInterface === false) {
49
            throw new InvalidArgumentException(sprintf('The object must implement "%s".', ProblemExceptionInterface::class));
50
        }
51
52
        if (isset($context[self::ALREADY_CALLED])) {
53
            throw new LogicException(sprintf('The normalizer "%s" can only be called once.', self::class));
54
        }
55
56
        $context[self::ALREADY_CALLED] = true;
57
58
        $data = $this->normalizer->normalize($object, $format, $context);
59
60
        $this->removeDetailsToPreventInformationDisclosure($object, $data);
61
62
        return $this->unsetKeysWithNullValue($data);
63
    }
64
65
    public function supportsNormalization($data, $format = null, array $context = []): bool
66
    {
67
        if (isset($context[self::ALREADY_CALLED])) {
68
            return false;
69
        }
70
71
        return $data instanceof ProblemExceptionInterface;
72
    }
73
74
    private function removeDetailsToPreventInformationDisclosure(ProblemExceptionInterface $object, array &$data): void
75
    {
76
        if ($this->debug) {
77
            return;
78
        }
79
80
        if ($object->getPrevious() === null || $object->getPrevious() instanceof HttpExceptionInterface) {
81
            return;
82
        }
83
84
        unset($data['detail']);
85
    }
86
87
    private function unsetKeysWithNullValue(array $data): array
88
    {
89
        foreach ($data as $key => $value) {
90
            if (is_array($value)) {
91
                $data[$key] = $this->unsetKeysWithNullValue($value);
92
            }
93
94
            if ($value === null) {
95
                unset($data[$key]);
96
            }
97
        }
98
99
        return $data;
100
    }
101
}
102