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 Doctrine\ORM\Mapping\ClassMetadataInfo; |
17
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
18
|
|
|
use Silverback\ApiComponentsBundle\DataProvider\PageDataProvider; |
19
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent; |
20
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\ComponentPosition; |
21
|
|
|
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException; |
22
|
|
|
use Silverback\ApiComponentsBundle\Exception\UnexpectedValueException; |
23
|
|
|
use Silverback\ApiComponentsBundle\Helper\ComponentPosition\ComponentPositionSortValueHelper; |
24
|
|
|
use Silverback\ApiComponentsBundle\Helper\Publishable\PublishableStatusChecker; |
25
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
26
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
27
|
|
|
use Symfony\Component\Security\Core\Security; |
28
|
|
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; |
29
|
|
|
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; |
30
|
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface; |
31
|
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; |
32
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
33
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; |
34
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
35
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* When creating a new component position the sort value should be set if not already explicitly set in the request. |
39
|
|
|
* |
40
|
|
|
* @author Daniel West <[email protected]> |
41
|
|
|
*/ |
42
|
|
|
class ComponentPositionNormalizer implements CacheableSupportsMethodInterface, ContextAwareDenormalizerInterface, DenormalizerAwareInterface, ContextAwareNormalizerInterface, NormalizerAwareInterface |
43
|
|
|
{ |
44
|
|
|
use DenormalizerAwareTrait; |
45
|
|
|
use NormalizerAwareTrait; |
46
|
|
|
|
47
|
|
|
private const ALREADY_CALLED = 'COMPONENT_POSITION_NORMALIZER_ALREADY_CALLED'; |
48
|
|
|
|
49
|
|
|
private PageDataProvider $pageDataProvider; |
50
|
|
|
private ComponentPositionSortValueHelper $componentPositionSortValueHelper; |
51
|
|
|
private RequestStack $requestStack; |
52
|
|
|
private Security $security; |
53
|
|
|
private PublishableStatusChecker $publishableStatusChecker; |
54
|
|
|
private ManagerRegistry $registry; |
55
|
|
|
|
56
|
|
|
public function __construct( |
57
|
|
|
PageDataProvider $pageDataProvider, |
58
|
|
|
ComponentPositionSortValueHelper $componentPositionSortValueHelper, |
59
|
|
|
RequestStack $requestStack, |
60
|
|
|
Security $security, |
61
|
|
|
PublishableStatusChecker $publishableStatusChecker, |
62
|
|
|
ManagerRegistry $registry |
63
|
|
|
) { |
64
|
|
|
$this->pageDataProvider = $pageDataProvider; |
65
|
|
|
$this->componentPositionSortValueHelper = $componentPositionSortValueHelper; |
66
|
|
|
$this->requestStack = $requestStack; |
67
|
|
|
$this->security = $security; |
68
|
|
|
$this->publishableStatusChecker = $publishableStatusChecker; |
69
|
|
|
$this->registry = $registry; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function hasCacheableSupportsMethod(): bool |
73
|
|
|
{ |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function supportsDenormalization($data, $type, $format = null, array $context = []): bool |
78
|
|
|
{ |
79
|
|
|
return !isset($context[self::ALREADY_CALLED]) && ComponentPosition::class === $type; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function denormalize($data, $type, $format = null, array $context = []) |
83
|
|
|
{ |
84
|
|
|
$context[self::ALREADY_CALLED] = true; |
85
|
|
|
|
86
|
|
|
$originalObject = $context[AbstractNormalizer::OBJECT_TO_POPULATE] ?? null; |
87
|
|
|
$originalSortValue = $originalObject ? $originalObject->sortValue : null; |
88
|
|
|
|
89
|
|
|
/** @var ComponentPosition $object */ |
90
|
|
|
$object = $this->denormalizer->denormalize($data, $type, $format, $context); |
91
|
|
|
|
92
|
|
|
$this->componentPositionSortValueHelper->calculateSortValue($object, $originalSortValue); |
93
|
|
|
|
94
|
|
|
return $object; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function supportsNormalization($data, $format = null, array $context = []): bool |
98
|
|
|
{ |
99
|
|
|
return $data instanceof ComponentPosition && !isset($context[self::ALREADY_CALLED]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function normalize($object, $format = null, array $context = []) |
103
|
|
|
{ |
104
|
|
|
/* @var ComponentPosition $object */ |
105
|
|
|
/* @var mixed|null $format */ |
106
|
|
|
|
107
|
|
|
$context[self::ALREADY_CALLED] = true; |
108
|
|
|
|
109
|
|
|
if ($object->pageDataProperty && (bool) $this->requestStack->getCurrentRequest()) { |
110
|
|
|
$object = $this->normalizeForPageData($object); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$component = $object->component; |
114
|
|
|
if ($component && $this->publishableStatusChecker->getAnnotationReader()->isConfigured($component) && $this->publishableStatusChecker->isGranted($component)) { |
115
|
|
|
$object->setComponent($this->normalizePublishableComponent($component)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this->normalizer->normalize($object, $format, $context); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function normalizePublishableComponent(AbstractComponent $component) |
122
|
|
|
{ |
123
|
|
|
$configuration = $this->publishableStatusChecker->getAnnotationReader()->getConfiguration($type = \get_class($component)); |
124
|
|
|
$em = $this->registry->getManagerForClass(\get_class($component)); |
125
|
|
|
if (!$em) { |
126
|
|
|
throw new InvalidArgumentException(sprintf('Could not find entity manager for class %s', $type)); |
127
|
|
|
} |
128
|
|
|
/** @var ClassMetadataInfo $classMetadata */ |
129
|
|
|
$classMetadata = $em->getClassMetadata($type); |
130
|
|
|
$draft = $classMetadata->getFieldValue($component, $configuration->reverseAssociationName); |
131
|
|
|
$published = $classMetadata->getFieldValue($component, $configuration->associationName); |
132
|
|
|
|
133
|
|
|
return $draft ?? $published; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
private function normalizeForPageData(ComponentPosition $object): ComponentPosition |
137
|
|
|
{ |
138
|
|
|
$pageData = $this->pageDataProvider->getPageData(); |
139
|
|
|
if (!$pageData) { |
140
|
|
|
if ($object->component || $this->security->isGranted('ROLE_ADMIN')) { |
141
|
|
|
return $object; |
142
|
|
|
} |
143
|
|
|
throw new UnexpectedValueException('Could not find page data for this route.'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$propertyAccessor = PropertyAccess::createPropertyAccessor(); |
147
|
|
|
$component = $propertyAccessor->getValue($pageData, $object->pageDataProperty); |
148
|
|
|
if (!$component) { |
149
|
|
|
throw new UnexpectedValueException(sprintf('Page data does not contain a value at %s', $object->pageDataProperty)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (!$component instanceof AbstractComponent) { |
153
|
|
|
throw new InvalidArgumentException(sprintf('The page data property %s is not a component', $object->pageDataProperty)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$object->setComponent($component); |
157
|
|
|
|
158
|
|
|
return $object; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|