Passed
Push — master ( e28a79...63969d )
by Daniel
08:32
created

ComponentPositionNormalizer::denormalize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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