PageDataNormalizer   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 47
ccs 0
cts 20
cp 0
rs 10
c 3
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSupportedTypes() 0 3 1
A supportsNormalization() 0 16 6
A __construct() 0 5 1
A normalize() 0 9 1
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