Skip to content

Commit

Permalink
IBX-8019: Replaced LocationService::loadLocationChildren usages wit…
Browse files Browse the repository at this point in the history
…h `SearchService::findLocations`
  • Loading branch information
webhdx committed Jun 2, 2024
1 parent a74064f commit 12a5a1e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
43 changes: 38 additions & 5 deletions src/lib/Event/Subscriber/AbstractRepositoryEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
12 changes: 4 additions & 8 deletions src/lib/Event/Subscriber/LocationEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down

0 comments on commit 12a5a1e

Please sign in to comment.