Passed
Push — master ( 7276f5...3cc058 )
by Daniel
09:58 queued 02:47
created

supportsDenormalization()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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