Skip to content

Commit 65de12f

Browse files
committed
Extend locationCount to include PageData property usages
`_metadata.publishable.locationCount` now sums direct ComponentPosition references AND AbstractPageData subclass instances that hold this component as a typed association property (the source resolved by pageDataProperty positions at render time).
1 parent 120d017 commit 65de12f

4 files changed

Lines changed: 35 additions & 1 deletion

File tree

features/bootstrap/DoctrineContext.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@ public function thereIsAPageDataWithPublishedComponentInPageDataPropertyPosition
862862
$publishedComponent = new DummyPublishableComponent();
863863
$publishedComponent->setPublishedAt(new \DateTime());
864864
$this->manager->persist($publishedComponent);
865+
$this->restContext->resources['publishable_component'] = $this->iriConverter->getIriFromResource($publishedComponent);
865866

866867
$pageData = new PageDataWithComponent();
867868
$pageData->publishableComponent = $publishedComponent;

features/publishable/publishable.feature

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,10 @@ Feature: Access to unpublished/draft resources should be configurable
445445
When I send a "GET" request to the resource "publishable_component"
446446
Then the response status code should be 200
447447
And the JSON node "_metadata.publishable.locationCount" should be equal to the number 2
448+
449+
@loginAdmin
450+
Scenario: A publishable component used as a pageData property is included in the location count
451+
Given there is a PageData resource with a published component in a pageDataProperty position and the route path "/test-page-data"
452+
When I send a "GET" request to the resource "publishable_component"
453+
Then the response status code should be 200
454+
And the JSON node "_metadata.publishable.locationCount" should be equal to the number 1

src/Resources/config/services_normalizers.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@
162162
new Reference(UploadableFileManager::class),
163163
new Reference(ResourceMetadataProvider::class),
164164
new Reference(EventDispatcherInterface::class),
165+
new Reference('silverback.metadata_provider.page_data'),
165166
]
166167
)->tag('serializer.normalizer', ['priority' => -400]);
167168
$services->alias(PublishableNormalizer::class, 'silverback.api_components.serializer.normalizer.publishable');

src/Serializer/Normalizer/PublishableNormalizer.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException;
2525
use Silverback\ApiComponentsBundle\Helper\Publishable\PublishableStatusChecker;
2626
use Silverback\ApiComponentsBundle\Helper\Uploadable\UploadableFileManager;
27+
use Silverback\ApiComponentsBundle\Metadata\Provider\PageDataMetadataProvider;
2728
use Silverback\ApiComponentsBundle\Serializer\ResourceMetadata\ResourceMetadataProvider;
2829
use Silverback\ApiComponentsBundle\Validator\PublishableValidator;
2930
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -64,6 +65,7 @@ public function __construct(
6465
private readonly UploadableFileManager $uploadableFileManager,
6566
private readonly ResourceMetadataProvider $resourceMetadataProvider,
6667
private readonly EventDispatcherInterface $eventDispatcher,
68+
private readonly PageDataMetadataProvider $pageDataMetadataProvider,
6769
) {
6870
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
6971
}
@@ -78,7 +80,7 @@ public function normalize($object, $format = null, array $context = []): float|a
7880

7981
$isPublished = $this->publishableStatusChecker->isActivePublishedAt($object);
8082
$locationCount = method_exists($object, 'getComponentPositions')
81-
? $this->registry->getManagerForClass(ComponentPosition::class)?->getRepository(ComponentPosition::class)->count(['component' => $object])
83+
? $this->countLocations($object)
8284
: null;
8385

8486
$resourceMetadata = $this->resourceMetadataProvider->findResourceMetadata($object);
@@ -266,6 +268,29 @@ public function supportsDenormalization($data, $type, $format = null, array $con
266268
return !isset($context[self::ALREADY_CALLED]) && $this->publishableStatusChecker->getAttributeReader()->isConfigured($type) && \is_array($data);
267269
}
268270

271+
private function countLocations(object $component): int
272+
{
273+
$count = $this->registry->getManagerForClass(ComponentPosition::class)
274+
?->getRepository(ComponentPosition::class)->count(['component' => $component])
275+
?? 0;
276+
277+
foreach ($this->pageDataMetadataProvider->createAll() as $pageDataMetadata) {
278+
$pageDataClass = $pageDataMetadata->getResourceClass();
279+
$em = $this->registry->getManagerForClass($pageDataClass);
280+
if (!$em) {
281+
continue;
282+
}
283+
foreach ($pageDataMetadata->getProperties() as $propertyMetadata) {
284+
if (!is_a($component, $propertyMetadata->getComponentClass(), true)) {
285+
continue;
286+
}
287+
$count += $em->getRepository($pageDataClass)->count([$propertyMetadata->getProperty() => $component]);
288+
}
289+
}
290+
291+
return $count;
292+
}
293+
269294
private function getManagerFromType(string $type): ObjectManager
270295
{
271296
$em = $this->registry->getManagerForClass($type);

0 commit comments

Comments
 (0)