Passed
Push — main ( b2d943...872826 )
by Daniel
05:35
created

RouteNormalizer::hasCacheableSupportsMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 1
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\NormalizerAwareInterface;
19
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
20
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
21
22
/**
23
 * @author Daniel West <[email protected]>
24
 */
25
class RouteNormalizer implements NormalizerInterface, NormalizerAwareInterface
26
{
27
    use NormalizerAwareTrait;
28
29
    private const ALREADY_CALLED = 'ROUTE_NORMALIZER_ALREADY_CALLED';
30
31
    /**
32
     * @param Route      $object
33
     * @param mixed|null $format
34
     */
35
    public function normalize($object, $format = null, array $context = []): float|array|\ArrayObject|bool|int|string|null
36
    {
37
        $context[self::ALREADY_CALLED] = true;
38
39
        $finalRoute = $object;
40
41
        $redirectedRoutes = [$finalRoute->getId()];
42
        while ($nextRedirect = $finalRoute->getRedirect()) {
43
            if (\in_array($nextRedirect->getId(), $redirectedRoutes, true)) {
44
                throw new CircularReferenceException(sprintf('The redirect routes result in a circular reference: %s', implode(' -> ', $redirectedRoutes)));
45
            }
46
            $redirectedRoutes[] = $nextRedirect->getId();
47
            $finalRoute = $nextRedirect;
48
        }
49
50
        $isRedirect = $finalRoute !== $object;
51
        if ($isRedirect) {
52
            $object->setPage($finalRoute->getPage());
53
            $object->setPageData($finalRoute->getPageData());
54
        }
55
56
        $normalized = $this->normalizer->normalize($object, $format, $context);
57
58
        if ($isRedirect) {
59
            $normalized['redirectPath'] = $finalRoute->getPath();
60
        }
61
62
        $operationName = $context['operation_name'] ?? null;
63
        if ('_api_/routes_manifest/{id}{._format}_get' === $operationName) {
64
            return [
65
                'resource_iris' => $this->getResourceIrisFromArray($normalized),
66
            ];
67
        }
68
69
        return $normalized;
70
    }
71
72
    private function getResourceIrisFromArray(array $resource, array $iris = []): array
73
    {
74
        $resourceId = $resource['@id'] ?? null;
75
        if (
76
            $resourceId
77
            && !str_starts_with($resource['@id'] ?? null, '/.well-known/')
0 ignored issues
show
Bug introduced by
It seems like $resource['@id'] ?? null can also be of type null; however, parameter $haystack of str_starts_with() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
            && !str_starts_with(/** @scrutinizer ignore-type */ $resource['@id'] ?? null, '/.well-known/')
Loading history...
78
            && '/_/resource_metadatas' !== $resourceId
79
            && !\in_array($resourceId, $iris, true)
80
        ) {
81
            $iris[] = $resourceId;
82
        }
83
        foreach ($resource as $resourceKey => $resourceValue) {
84
            // may be a string or simple
85
            // may be an array representing a resource
86
            // may be an array of any other values
87
            // may be an array of arrays
88
            if (\is_array($resourceValue)) {
89
                // check if the array is representing a new resource
90
                if (isset($resourceValue['@id'])) {
91
                    $iris = $this->getResourceIrisFromArray($resourceValue, $iris);
92
                }
93
                // check if the array contains more resources
94
                foreach ($resourceValue as $nestedValue) {
95
                    if (isset($nestedValue['@id'])) {
96
                        $iris = $this->getResourceIrisFromArray($nestedValue, $iris);
97
                    }
98
                }
99
            }
100
        }
101
102
        return $iris;
103
    }
104
105
    public function supportsNormalization($data, $format = null, $context = []): bool
106
    {
107
        return !isset($context[self::ALREADY_CALLED]) && $data instanceof Route;
108
    }
109
110
    public function getSupportedTypes(?string $format): array
0 ignored issues
show
Unused Code introduced by
The parameter $format is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

110
    public function getSupportedTypes(/** @scrutinizer ignore-unused */ ?string $format): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111
    {
112
        return [Route::class => false];
113
    }
114
}
115