Passed
Pull Request — feature/publishable (#43)
by Vincent
06:05
created

PublishableNormalizer::supportsNormalization()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
ccs 0
cts 6
cp 0
rs 10
cc 4
nc 3
nop 3
crap 20
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component 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\ApiComponentBundle\Serializer;
15
16
use ApiPlatform\Core\Api\IriConverterInterface;
17
use Doctrine\Persistence\ManagerRegistry;
18
use Silverback\ApiComponentBundle\Publishable\ClassMetadataTrait;
19
use Silverback\ApiComponentBundle\Publishable\PublishableHelper;
20
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
21
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
22
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
23
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
24
25
/**
26
 * Adds `isPublished` property on response, if not set.
27
 *
28
 * @author Vincent Chalamon <[email protected]>
29
 */
30
final class PublishableNormalizer implements ContextAwareNormalizerInterface, CacheableSupportsMethodInterface, NormalizerAwareInterface
31
{
32
    use NormalizerAwareTrait;
33
    use ClassMetadataTrait;
34
35
    private const ALREADY_CALLED = 'PUBLISHABLE_NORMALIZER_ALREADY_CALLED';
36
37
    private PublishableHelper $publishableHelper;
38
    private ManagerRegistry $registry;
39
    private IriConverterInterface $iriConverter;
40
41
    public function __construct(PublishableHelper $publishableHelper, ManagerRegistry $registry, IriConverterInterface $iriConverter)
42
    {
43
        $this->publishableHelper = $publishableHelper;
44
        $this->registry = $registry;
45
        $this->iriConverter = $iriConverter;
46
    }
47
48
    public function normalize($object, $format = null, array $context = [])
49
    {
50
        $context[self::ALREADY_CALLED] = true;
51
        $data = $this->normalizer->normalize($object, $format, $context);
52
        $configuration = $this->publishableHelper->getConfiguration($object);
53
54
        if (!\array_key_exists('published', $data)) {
55
            $data['published'] = $this->publishableHelper->isPublished($object);
56
        }
57
58
        if (!\array_key_exists($configuration->fieldName, $data)) {
59
            $data[$configuration->fieldName] = $this->getClassMetadata($object)->getFieldValue($object, $configuration->fieldName);
60
        }
61
62
        if (!\array_key_exists($configuration->associationName, $data)) {
63
            $value = $this->getClassMetadata($object)->getFieldValue($object, $configuration->associationName);
64
            $data[$configuration->fieldName] = $value ? $this->iriConverter->getIriFromItem($value) : null;
65
        }
66
67
        return $data;
68
    }
69
70
    public function supportsNormalization($data, $format = null, $context = []): bool
71
    {
72
        if (isset($context[self::ALREADY_CALLED])) {
73
            return false;
74
        }
75
76
        if (!\is_object($data) || $data instanceof \Traversable) {
77
            return false;
78
        }
79
80
        return $this->publishableHelper->isPublishable($data);
81
    }
82
83
    public function hasCacheableSupportsMethod(): bool
84
    {
85
        return false;
86
    }
87
}
88