Passed
Pull Request — main (#155)
by Daniel
05:05
created

PersistedNormalizer::normalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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