Passed
Push — master ( 3ec415...9f2f47 )
by Daniel
16:25
created

ComponentPositionNormalizer::denormalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 2
rs 10
c 1
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 Silverback\ApiComponentsBundle\DataProvider\PageDataProvider;
17
use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent;
18
use Silverback\ApiComponentsBundle\Entity\Core\ComponentPosition;
19
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException;
20
use Silverback\ApiComponentsBundle\Exception\UnexpectedValueException;
21
use Silverback\ApiComponentsBundle\Helper\ComponentPosition\ComponentPositionSortValueHelper;
22
use Symfony\Component\PropertyAccess\PropertyAccess;
23
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
24
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
25
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
26
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
27
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
28
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
29
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
30
31
/**
32
 * When creating a new component position the sort value should be set if not already explicitly set in the request.
33
 *
34
 * @author Daniel West <[email protected]>
35
 */
36
class ComponentPositionNormalizer implements CacheableSupportsMethodInterface, ContextAwareDenormalizerInterface, DenormalizerAwareInterface, ContextAwareNormalizerInterface, NormalizerAwareInterface
37
{
38
    use DenormalizerAwareTrait;
39
    use NormalizerAwareTrait;
40
41
    private const ALREADY_CALLED = 'COMPONENT_POSITION_NORMALIZER_ALREADY_CALLED';
42
43
    private PageDataProvider $pageDataProvider;
44
    private ComponentPositionSortValueHelper $componentPositionSortValueHelper;
45
46 7
    public function __construct(PageDataProvider $pageDataProvider, ComponentPositionSortValueHelper $componentPositionSortValueHelper)
47
    {
48 7
        $this->pageDataProvider = $pageDataProvider;
49 7
        $this->componentPositionSortValueHelper = $componentPositionSortValueHelper;
50 7
    }
51
52
    public function hasCacheableSupportsMethod(): bool
53
    {
54
        return false;
55
    }
56
57
    public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
58
    {
59
        return !isset($context[self::ALREADY_CALLED]) && ComponentPosition::class === $type;
60
    }
61
62
    public function denormalize($data, $type, $format = null, array $context = [])
63
    {
64
        $context[self::ALREADY_CALLED] = true;
65
66
        /** @var ComponentPosition $object */
67
        $object = $this->denormalizer->denormalize($data, $type, $format, $context);
68
        $this->componentPositionSortValueHelper->calculateSortValue($object);
69
70
        return $object;
71
    }
72
73
    public function supportsNormalization($data, $format = null, array $context = []): bool
74
    {
75
        return $data instanceof ComponentPosition && $data->pageDataProperty && !isset($context[self::ALREADY_CALLED]);
76
    }
77
78
    /**
79
     * @param ComponentPosition $object
80
     * @param mixed|null        $format
81
     */
82
    public function normalize($object, $format = null, array $context = [])
83
    {
84
        $context[self::ALREADY_CALLED] = true;
85
        $pageData = $this->pageDataProvider->getPageData();
86
        if (!$pageData) {
87
            if ($object->component) {
88
                return $this->normalizer->normalize($object, $format, $context);
89
            }
90
            throw new UnexpectedValueException('Could not find page data.');
91
        }
92
93
        $propertyAccessor = PropertyAccess::createPropertyAccessor();
94
        $component = $propertyAccessor->getValue($pageData, $object->pageDataProperty);
95
        if (!$component) {
96
            throw new UnexpectedValueException(sprintf('Page data does not contain a value at %s', $object->pageDataProperty));
97
        }
98
99
        if (!$component instanceof AbstractComponent) {
100
            throw new InvalidArgumentException(sprintf('The page data property %s is not a component', $object->pageDataProperty));
101
        }
102
103
        $object->setComponent($component);
104
105
        return $this->normalizer->normalize($object, $format, $context);
106
    }
107
}
108