-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathComponentPositionNormalizer.php
More file actions
188 lines (157 loc) · 7.46 KB
/
Copy pathComponentPositionNormalizer.php
File metadata and controls
188 lines (157 loc) · 7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/*
* This file is part of the Silverback API Components Bundle Project
*
* (c) Daniel West <daniel@silverback.is>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silverback\ApiComponentsBundle\Serializer\Normalizer;
use ApiPlatform\Metadata\IriConverterInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\Persistence\ManagerRegistry;
use Silverback\ApiComponentsBundle\DataProvider\PageDataProvider;
use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent;
use Silverback\ApiComponentsBundle\Entity\Core\ComponentPosition;
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException;
use Silverback\ApiComponentsBundle\Helper\ComponentPosition\ComponentPositionSortValueHelper;
use Silverback\ApiComponentsBundle\Helper\Publishable\PublishableStatusChecker;
use Silverback\ApiComponentsBundle\Serializer\ResourceMetadata\ResourceMetadataProvider;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* When creating a new component position the sort value should be set if not already explicitly set in the request.
*
* @author Daniel West <daniel@silverback.is>
*/
class ComponentPositionNormalizer implements DenormalizerInterface, DenormalizerAwareInterface, NormalizerInterface, NormalizerAwareInterface
{
use DenormalizerAwareTrait;
use NormalizerAwareTrait;
private const ALREADY_CALLED = 'COMPONENT_POSITION_NORMALIZER_ALREADY_CALLED';
public function __construct(
private readonly PageDataProvider $pageDataProvider,
private readonly ComponentPositionSortValueHelper $componentPositionSortValueHelper,
private readonly RequestStack $requestStack,
private readonly PublishableStatusChecker $publishableStatusChecker,
private readonly ManagerRegistry $registry,
private readonly IriConverterInterface $iriConverter,
private readonly ResourceMetadataProvider $resourceMetadataProvider,
) {
}
public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
{
return !isset($context[self::ALREADY_CALLED]) && ComponentPosition::class === $type;
}
public function denormalize($data, $type, $format = null, array $context = []): ComponentPosition
{
$context[self::ALREADY_CALLED] = true;
$originalObject = $context[AbstractNormalizer::OBJECT_TO_POPULATE] ?? null;
$originalSortValue = $originalObject ? $originalObject->sortValue : null;
/** @var ComponentPosition $object */
$object = $this->denormalizer->denormalize($data, $type, $format, $context);
$this->componentPositionSortValueHelper->calculateSortValue($object, $originalSortValue);
return $object;
}
public function supportsNormalization($data, $format = null, array $context = []): bool
{
return $data instanceof ComponentPosition && !isset($context[self::ALREADY_CALLED]);
}
public function normalize($object, $format = null, array $context = []): float|array|\ArrayObject|bool|int|string|null
{
/* @var ComponentPosition $object */
/* @var mixed|null $format */
$context[self::ALREADY_CALLED] = true;
$staticComponent = $object->component;
$resourceMetadata = $this->resourceMetadataProvider->findResourceMetadata($object);
$object = $this->normalizeForPageData($object);
if ($object->pageDataProperty) {
$resourceMetadata->setIsDynamicPosition(true);
try {
$resourceMetadata->setPageDataPath($this->pageDataProvider->getOriginalRequestPath());
} catch (UnprocessableEntityHttpException $e) {
// the path header may not exist
}
} else {
$resourceMetadata->setIsDynamicPosition(false);
}
if ($object->component) {
$object->component = $this->getPublishableComponent($object->component);
}
if ($staticComponent) {
$resourceMetadata->setStaticComponent($this->iriConverter->getIriFromResource($this->getPublishableComponent($staticComponent)));
}
return $this->normalizer->normalize($object, $format, $context);
}
private function getPublishableComponent($component)
{
if (
$component
&& $this->publishableStatusChecker->getAttributeReader()->isConfigured($component)
&& $this->publishableStatusChecker->isGranted($component)
) {
return $this->normalizePublishableComponent($component);
}
return $component;
}
private function normalizePublishableComponent(AbstractComponent $component)
{
$configuration = $this->publishableStatusChecker->getAttributeReader()->getConfiguration($type = $component::class);
$em = $this->registry->getManagerForClass($component::class);
if (!$em) {
throw new InvalidArgumentException(\sprintf('Could not find entity manager for class %s', $type));
}
/** @var ClassMetadata $classMetadata */
$classMetadata = $em->getClassMetadata($type);
$draft = $classMetadata->getFieldValue($component, $configuration->reverseAssociationName);
return $draft ?? $component;
}
private function normalizeForPageData(ComponentPosition $object): ComponentPosition
{
if (!$object->pageDataProperty || !$this->requestStack->getCurrentRequest()) {
return $object;
}
try {
$pageData = $this->pageDataProvider->getPageData();
} catch (UnprocessableEntityHttpException $e) {
// when serializing for mercure, we do not need the path header
return $object;
}
if (!$pageData) {
return $object;
}
$propertyAccessor = PropertyAccess::createPropertyAccessor();
try {
$component = $propertyAccessor->getValue($pageData, $object->pageDataProperty);
} catch (UnexpectedTypeException|NoSuchIndexException|NoSuchPropertyException $e) {
return $object;
}
// optional to have the page data component found
if (!$component) {
return $object;
}
// it must be a component if it is found though
if (!$component instanceof AbstractComponent) {
throw new InvalidArgumentException(\sprintf('The page data property %s is not a component', $object->pageDataProperty));
}
// populate the position
$object->setComponent($component);
return $object;
}
public function getSupportedTypes(?string $format): array
{
return [ComponentPosition::class => false];
}
}