Test Failed
Push — master ( a81572...90f7a4 )
by Daniel
04:32
created

PageDataNormalizer::getPageDataProps()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 17
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 24
ccs 0
cts 19
cp 0
crap 12
rs 9.7

1 Method

Rating   Name   Duplication   Size   Complexity  
A PageDataNormalizer::hasCacheableSupportsMethod() 0 3 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 Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
19
use Symfony\Component\PropertyAccess\PropertyAccess;
20
use Symfony\Component\PropertyAccess\PropertyAccessor;
21
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
22
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
23
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
24
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
25
26
/**
27
 * @author Daniel West <[email protected]>
28
 */
29
final class PageDataNormalizer implements ContextAwareNormalizerInterface, CacheableSupportsMethodInterface, NormalizerAwareInterface
30
{
31
    use NormalizerAwareTrait;
32
33
    private const ALREADY_CALLED = 'PAGE_DATA_NORMALIZER_ALREADY_CALLED';
34
35
    private PropertyAccessor $propertyAccessor;
36
    private PageDataMetadataFactoryInterface $pageDataMetadataFactory;
37
38
    public function __construct(PageDataMetadataFactoryInterface $pageDataMetadataFactory)
39
    {
40
        $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
41
        $this->pageDataMetadataFactory = $pageDataMetadataFactory;
42
    }
43
44
    public function normalize($object, $format = null, array $context = [])
45
    {
46
        $context[self::ALREADY_CALLED][] = $this->propertyAccessor->getValue($object, 'id');
47
        $metadata = $this->pageDataMetadataFactory->create(\get_class($object));
48
        $context[MetadataNormalizer::METADATA_CONTEXT]['page_data_metadata'] = $this->normalizer->normalize($metadata, $format, $context);
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
        if (!isset($context[self::ALREADY_CALLED])) {
59
            $context[self::ALREADY_CALLED] = [];
60
        }
61
        try {
62
            $id = $this->propertyAccessor->getValue($data, 'id');
63
        } catch (NoSuchPropertyException $e) {
64
            return false;
65
        }
66
67
        return !\in_array($id, $context[self::ALREADY_CALLED], true) &&
68
            is_a($data, AbstractPageData::class);
69
    }
70
71
    public function hasCacheableSupportsMethod(): bool
72
    {
73
        return false;
74
    }
75
}
76