2424use Silverback \ApiComponentsBundle \Exception \InvalidArgumentException ;
2525use Silverback \ApiComponentsBundle \Helper \Publishable \PublishableStatusChecker ;
2626use Silverback \ApiComponentsBundle \Helper \Uploadable \UploadableFileManager ;
27+ use Silverback \ApiComponentsBundle \Metadata \Provider \PageDataMetadataProvider ;
2728use Silverback \ApiComponentsBundle \Serializer \ResourceMetadata \ResourceMetadataProvider ;
2829use Silverback \ApiComponentsBundle \Validator \PublishableValidator ;
2930use 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