Passed
Push — master ( 4cb23e...bf17ee )
by Daniel
06:11
created

CollectionNormalizer::__construct()   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 ApiPlatform\Core\Util\ClassInfoTrait;
17
use Silverback\ApiComponentsBundle\Entity\Component\Collection;
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
use Traversable;
26
27
/**
28
 * @author Daniel West <[email protected]>
29
 */
30
class CollectionNormalizer implements ContextAwareNormalizerInterface, CacheableSupportsMethodInterface, NormalizerAwareInterface
31
{
32
    use ClassInfoTrait;
33
    use NormalizerAwareTrait;
34
35
    private const ALREADY_CALLED = 'COLLECTION_NORMALIZER_ALREADY_CALLED';
36
37
    private PropertyAccessor $propertyAccessor;
38
39
    public function __construct()
40
    {
41
        $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
42
    }
43
44
    public function normalize($object, $format = null, array $context = [])
45
    {
46
        $context[self::ALREADY_CALLED][] = $this->propertyAccessor->getValue($object, 'id');
47
        $context[MetadataNormalizer::METADATA_CONTEXT]['collection'] = true;
48
49
        return $this->normalizer->normalize($object, $format, $context);
50
    }
51
52
    public function supportsNormalization($data, $format = null, $context = []): bool
53
    {
54
        if (!\is_object($data) || $data instanceof Traversable) {
55
            return false;
56
        }
57
58
        if (!isset($context[self::ALREADY_CALLED])) {
59
            $context[self::ALREADY_CALLED] = [];
60
        }
61
62
        try {
63
            $id = $this->propertyAccessor->getValue($data, 'id');
64
        } catch (NoSuchPropertyException $e) {
65
            return false;
66
        }
67
68
        return !\in_array($id, $context[self::ALREADY_CALLED], true) &&
69
            $data instanceof Collection;
70
    }
71
72
    public function hasCacheableSupportsMethod(): bool
73
    {
74
        return false;
75
    }
76
}
77