diff --git a/src/lib/Event/Subscriber/AbstractRepositoryEventSubscriber.php b/src/lib/Event/Subscriber/AbstractRepositoryEventSubscriber.php index eab48c9d..23b6163e 100644 --- a/src/lib/Event/Subscriber/AbstractRepositoryEventSubscriber.php +++ b/src/lib/Event/Subscriber/AbstractRepositoryEventSubscriber.php @@ -10,7 +10,9 @@ use eZ\Publish\API\Repository\ContentService as ContentServiceInterface; use eZ\Publish\API\Repository\LocationService as LocationServiceInterface; +use eZ\Publish\API\Repository\SearchService; use eZ\Publish\API\Repository\Values\Content\Location; +use eZ\Publish\Core\Query\QueryFactoryInterface; use EzSystems\EzRecommendationClient\Helper\ContentHelper; use EzSystems\EzRecommendationClient\Helper\LocationHelper; use EzSystems\EzRecommendationClient\Service\NotificationService; @@ -29,33 +31,64 @@ abstract class AbstractRepositoryEventSubscriber extends AbstractCoreEventSubscr /** @var \EzSystems\EzRecommendationClient\Helper\ContentHelper */ protected $contentHelper; + /** @var \eZ\Publish\Core\Query\QueryFactoryInterface */ + private $queryFactory; + + /** @var \eZ\Publish\API\Repository\SearchService */ + private $searchService; + public function __construct( NotificationService $notificationService, ContentServiceInterface $contentService, LocationServiceInterface $locationService, LocationHelper $locationHelper, - ContentHelper $contentHelper + ContentHelper $contentHelper, + QueryFactoryInterface $queryFactory, + SearchService $searchService ) { parent::__construct($notificationService); $this->contentService = $contentService; $this->locationService = $locationService; $this->locationHelper = $locationHelper; $this->contentHelper = $contentHelper; + $this->queryFactory = $queryFactory; + $this->searchService = $searchService; } /** + * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException */ protected function updateLocationSubtree(Location $location, string $method, string $action): void { - $subtree = $this->locationService->loadLocationChildren($location); + $locationChildren = $this->loadLocationChildren($location); - /** @var \eZ\Publish\API\Repository\Values\Content\Location $content */ - foreach ($subtree as $content) { + foreach ($locationChildren as $locationChild) { $this->notificationService->sendNotification( - $method, $action, $content->getContentInfo() + $method, $action, $locationChild->getContentInfo() ); } } + + /** + * @return array<\eZ\Publish\API\Repository\Values\Content\Location> + * + * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException + */ + protected function loadLocationChildren(Location $location): array + { + /** @var \eZ\Publish\API\Repository\Values\Content\LocationQuery $locationChildrenQuery */ + $locationChildrenQuery = $this->queryFactory->create('Children', ['location' => $location]); + $searchResult = $this->searchService->findLocations($locationChildrenQuery); + + $locations = []; + foreach ($searchResult->searchHits as $searchHit) { + /** @var \eZ\Publish\API\Repository\Values\Content\Location $locationChild */ + $locationChild = $searchHit->valueObject; + $locations[] = $locationChild; + } + + return $locations; + } } diff --git a/src/lib/Event/Subscriber/LocationEventSubscriber.php b/src/lib/Event/Subscriber/LocationEventSubscriber.php index 30fb4135..859c316b 100644 --- a/src/lib/Event/Subscriber/LocationEventSubscriber.php +++ b/src/lib/Event/Subscriber/LocationEventSubscriber.php @@ -128,15 +128,13 @@ public function onUpdateLocation(UpdateLocationEvent $event): void /** * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException + * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException */ private function hideLocation(Location $location, bool $isChild = false): void { - $children = $this->locationService->loadLocationChildren($location)->locations; - - /** @var \eZ\Publish\API\Repository\Values\Content\Location $child */ - foreach ($children as $child) { + foreach ($this->loadLocationChildren($location) as $child) { $this->hideLocation($child, true); } @@ -158,15 +156,13 @@ private function hideLocation(Location $location, bool $isChild = false): void } /** + * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException */ private function updateLocationWithChildren(Location $location, string $method, string $action): void { - $children = $this->locationService->loadLocationChildren($location)->locations; - - /** @var \eZ\Publish\API\Repository\Values\Content\Location $child */ - foreach ($children as $child) { + foreach ($this->loadLocationChildren($location) as $child) { $this->updateLocationWithChildren($child, $method, $action); }