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

PageDataNormalizer::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
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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\Core\AbstractPageData;
17
use Silverback\ApiComponentsBundle\Metadata\Factory\PageDataMetadataFactoryInterface;
18
use Silverback\ApiComponentsBundle\Serializer\ResourceMetadata\ResourceMetadataProvider;
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
final class PageDataNormalizer implements NormalizerInterface, NormalizerAwareInterface
30
{
31
    use NormalizerAwareTrait;
32
33
    private const ALREADY_CALLED = 'PAGE_DATA_NORMALIZER_ALREADY_CALLED';
34
35
    private PropertyAccessor $propertyAccessor;
36
37
    public function __construct(
38
        private readonly PageDataMetadataFactoryInterface $pageDataMetadataFactory,
39
        private readonly ResourceMetadataProvider $resourceMetadataProvider
40
    ) {
41
        $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
42
    }
43
44
    public function normalize($object, $format = null, array $context = []): float|array|\ArrayObject|bool|int|string|null
45
    {
46
        $context[self::ALREADY_CALLED][] = $this->propertyAccessor->getValue($object, 'id');
47
        $metadata = $this->pageDataMetadataFactory->create($object::class);
48
49
        $resourceMetadata = $this->resourceMetadataProvider->findResourceMetadata($object);
50
        $resourceMetadata->setPageDataMetadata($metadata);
51
52
        return $this->normalizer->normalize($object, $format, $context);
53
    }
54
55
    public function supportsNormalization($data, $format = null, $context = []): bool
56
    {
57
        if (!\is_object($data) || $data instanceof \Traversable) {
58
            return false;
59
        }
60
        if (!isset($context[self::ALREADY_CALLED])) {
61
            $context[self::ALREADY_CALLED] = [];
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
            && is_a($data, AbstractPageData::class);
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 [AbstractPageData::class => false];
76
    }
77
}
78