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