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

CollectionNormalizer::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 0 Features 0
Metric Value
eloc 1
nc 1
nop 0
dl 0
loc 3
c 1
b 0
f 0
cc 1
ccs 0
cts 2
cp 0
crap 2
rs 10
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\Component\Collection;
17
use Silverback\ApiComponentsBundle\Serializer\ResourceMetadata\ResourceMetadataProvider;
18
use Silverback\ApiComponentsBundle\Utility\ClassInfoTrait;
19
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
20
use Symfony\Component\PropertyAccess\PropertyAccess;
21
use Symfony\Component\PropertyAccess\PropertyAccessor;
22
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
23
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
24
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
25
26
/**
27
 * @author Daniel West <[email protected]>
28
 */
29
class CollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface
30
{
31
    use ClassInfoTrait;
32
    use NormalizerAwareTrait;
33
34
    private const ALREADY_CALLED = 'COLLECTION_NORMALIZER_ALREADY_CALLED';
35
36
    private PropertyAccessor $propertyAccessor;
37
38
    public function __construct(private readonly ResourceMetadataProvider $resourceMetadataProvider)
39
    {
40
        $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
41
    }
42
43
    public function normalize($object, $format = null, array $context = []): float|array|\ArrayObject|bool|int|string|null
44
    {
45
        $context[self::ALREADY_CALLED][] = $this->propertyAccessor->getValue($object, 'id');
46
47
        $resourceMetadata = $this->resourceMetadataProvider->findResourceMetadata($object);
48
        $resourceMetadata->setCollection(true);
49
50
        return $this->normalizer->normalize($object, $format, $context);
51
    }
52
53
    public function supportsNormalization($data, $format = null, $context = []): bool
54
    {
55
        if (!\is_object($data) || $data instanceof \Traversable) {
56
            return false;
57
        }
58
59
        if (!isset($context[self::ALREADY_CALLED])) {
60
            $context[self::ALREADY_CALLED] = [];
61
        }
62
63
        try {
64
            $id = $this->propertyAccessor->getValue($data, 'id');
65
        } catch (NoSuchPropertyException $e) {
66
            return false;
67
        }
68
69
        return !\in_array($id, $context[self::ALREADY_CALLED], true)
70
            && $data instanceof Collection;
71
    }
72
73
    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

73
    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...
74
    {
75
        return ['object' => false];
76
    }
77
}
78