Test Failed
Pull Request — main (#149)
by Daniel
17:45
created

RouteNormalizer::getResourceIrisFromArray()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 7
nc 6
nop 1
dl 0
loc 13
ccs 0
cts 1
cp 0
crap 30
rs 9.6111
c 1
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Serializer\Normalizer;
15
16
use Silverback\ApiComponentsBundle\Entity\Core\Route;
17
use Symfony\Component\Serializer\Exception\CircularReferenceException;
18
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
19
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
20
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
21
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
22
23
/**
24
 * @author Daniel West <[email protected]>
25
 */
26
class RouteNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface, NormalizerAwareInterface
27
{
28
    use NormalizerAwareTrait;
29
30
    private const ALREADY_CALLED = 'ROUTE_NORMALIZER_ALREADY_CALLED';
31
32
    /**
33
     * @param Route      $object
34
     * @param mixed|null $format
35
     */
36
    public function normalize($object, $format = null, array $context = []): float|array|\ArrayObject|bool|int|string|null
37
    {
38
        $context[self::ALREADY_CALLED] = true;
39
40
        $finalRoute = $object;
41
42
        $redirectedRoutes = [$finalRoute->getId()];
43
        while ($nextRedirect = $finalRoute->getRedirect()) {
44
            if (\in_array($nextRedirect->getId(), $redirectedRoutes, true)) {
45
                throw new CircularReferenceException(sprintf('The redirect routes result in a circular reference: %s', implode(' -> ', $redirectedRoutes)));
46
            }
47
            $redirectedRoutes[] = $nextRedirect->getId();
48
            $finalRoute = $nextRedirect;
49
        }
50
51
        $isRedirect = $finalRoute !== $object;
52
        if ($isRedirect) {
53
            $object->setPage($finalRoute->getPage());
54
            $object->setPageData($finalRoute->getPageData());
55
        }
56
57
        $normalized = $this->normalizer->normalize($object, $format, $context);
58
59
        if ($isRedirect) {
60
            $normalized['redirectPath'] = $finalRoute->getPath();
61
        }
62
63
        $operationName = $context['operation_name'] ?? null;
64
        if ('_api_/routes_manifest/{id}.{_format}_get' === $operationName) {
65
            return [
66
                'resource_iris' => $this->getResourceIrisFromArray($normalized),
67
            ];
68
        }
69
70
        return $normalized;
71
    }
72
73
    private function getResourceIrisFromArray(array $resource): array
74
    {
75
        $iris = [];
76
        if (isset($resource['@id'])) {
77
            $iris[] = $resource['@id'];
78
        }
79
        foreach ($resource as $resourceValue) {
80
            if (\is_array($resourceValue) && isset($resourceValue['@id'])) {
81
                array_push($iris, ...$this->getResourceIrisFromArray($resourceValue));
82
            }
83
        }
84
85
        return $iris;
86
    }
87
88
    public function supportsNormalization($data, $format = null, $context = []): bool
89
    {
90
        return !isset($context[self::ALREADY_CALLED]) && $data instanceof Route;
91
    }
92
93
    public function hasCacheableSupportsMethod(): bool
94
    {
95
        return false;
96
    }
97
}
98