From ee85d79d0ecec24d8e412346a1dc06090e56e384 Mon Sep 17 00:00:00 2001 From: ciastektk Date: Thu, 17 Feb 2022 08:28:36 +0100 Subject: [PATCH] IBX-2332: Added remoteId support in RecommendationEventSubscriber to fetch recommendations (#105) * Added repository.content.use_remote_id parameter * Added Ibexa\Personalization namespace * Added RepositoryConfigResolver * Update src/bundle/DependencyInjection/ConfigurationMapper.php Co-authored-by: Konrad Oboza * IBX-2332: Added remoteId support in RecommendationEventSubscriber to fetch recommendations https://issues.ibexa.co/browse/IBX-2332 Co-authored-by: Konrad Oboza --- .../RecommendationEventSubscriber.php | 53 +++++++++++-------- src/lib/Helper/LocationHelper.php | 34 +----------- 2 files changed, 34 insertions(+), 53 deletions(-) diff --git a/src/lib/Event/Subscriber/RecommendationEventSubscriber.php b/src/lib/Event/Subscriber/RecommendationEventSubscriber.php index 6962c835..8e8d06b5 100644 --- a/src/lib/Event/Subscriber/RecommendationEventSubscriber.php +++ b/src/lib/Event/Subscriber/RecommendationEventSubscriber.php @@ -8,13 +8,13 @@ namespace EzSystems\EzRecommendationClient\Event\Subscriber; +use eZ\Publish\API\Repository\Values\Content\Content; use eZ\Publish\Core\MVC\Symfony\Locale\LocaleConverterInterface; use EzSystems\EzRecommendationClient\Event\RecommendationResponseEvent; -use EzSystems\EzRecommendationClient\Helper\ContentTypeHelper; -use EzSystems\EzRecommendationClient\Helper\LocationHelper; use EzSystems\EzRecommendationClient\Request\BasicRecommendationRequest as Request; use EzSystems\EzRecommendationClient\Service\RecommendationServiceInterface; use EzSystems\EzRecommendationClient\SPI\RecommendationRequest; +use Ibexa\Personalization\Config\Repository\RepositoryConfigResolverInterface; use Psr\Http\Message\ResponseInterface; use Psr\Log\LoggerInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -23,11 +23,8 @@ final class RecommendationEventSubscriber implements EventSubscriberInterface { - private const LOCALE_REQUEST_KEY = '_locale'; private const DEFAULT_LOCALE = 'eng-GB'; - - /** @var \EzSystems\EzRecommendationClient\Service\RecommendationServiceInterface */ - private $recommendationService; + private const LOCALE_REQUEST_KEY = '_locale'; /** @var \eZ\Publish\Core\MVC\Symfony\Locale\LocaleConverterInterface */ private $localeConverter; @@ -35,24 +32,22 @@ final class RecommendationEventSubscriber implements EventSubscriberInterface /** @var \Psr\Log\LoggerInterface */ private $logger; - /** @var \EzSystems\EzRecommendationClient\Helper\ContentTypeHelper */ - private $contentTypeHelper; + /** @var \EzSystems\EzRecommendationClient\Service\RecommendationServiceInterface */ + private $recommendationService; - /** @var \EzSystems\EzRecommendationClient\Helper\LocationHelper */ - private $locationHelper; + /** @var \Ibexa\Personalization\Config\Repository\RepositoryConfigResolverInterface */ + private $repositoryConfigResolver; public function __construct( - RecommendationServiceInterface $recommendationService, LocaleConverterInterface $localeConverter, LoggerInterface $logger, - ContentTypeHelper $contentTypeHelper, - LocationHelper $locationHelper + RecommendationServiceInterface $recommendationService, + RepositoryConfigResolverInterface $repositoryConfigResolver ) { - $this->recommendationService = $recommendationService; $this->localeConverter = $localeConverter; $this->logger = $logger; - $this->contentTypeHelper = $contentTypeHelper; - $this->locationHelper = $locationHelper; + $this->recommendationService = $recommendationService; + $this->repositoryConfigResolver = $repositoryConfigResolver; } /** @@ -88,15 +83,19 @@ public function onRecommendationResponse(RecommendationResponseEvent $event): vo */ private function getRecommendationRequest(ParameterBag $parameterBag): RecommendationRequest { - $contextItems = (int) $parameterBag->get(Request::CONTEXT_ITEMS_KEY, 0); + $contextItem = null; + $content = $parameterBag->get(Request::CONTEXT_ITEMS_KEY); + if ($content instanceof Content) { + $contextItem = $this->repositoryConfigResolver->useRemoteId() ? $content->contentInfo->remoteId : $content->id; + } return new Request([ RecommendationRequest::SCENARIO => $parameterBag->get(RecommendationRequest::SCENARIO, ''), Request::LIMIT_KEY => $parameterBag->get(Request::LIMIT_KEY, 3), - Request::CONTEXT_ITEMS_KEY => $contextItems, - Request::CONTENT_TYPE_KEY => $this->contentTypeHelper->getContentTypeId($this->contentTypeHelper->getContentTypeIdentifier($contextItems)), - Request::OUTPUT_TYPE_ID_KEY => $this->contentTypeHelper->getContentTypeId($parameterBag->get(Request::OUTPUT_TYPE_ID_KEY, '')), - Request::CATEGORY_PATH_KEY => $this->locationHelper->getParentLocationPathString($contextItems), + Request::CONTEXT_ITEMS_KEY => $contextItem, + Request::CONTENT_TYPE_KEY => $content->getContentType()->id, + Request::OUTPUT_TYPE_ID_KEY => $parameterBag->get(Request::OUTPUT_TYPE_ID_KEY), + Request::CATEGORY_PATH_KEY => $this->getCategoryPath($content), Request::LANGUAGE_KEY => $this->getRequestLanguage($parameterBag->get(self::LOCALE_REQUEST_KEY)), Request::ATTRIBUTES_KEY => $parameterBag->get(Request::ATTRIBUTES_KEY, []), Request::FILTERS_KEY => $parameterBag->get(Request::FILTERS_KEY, []), @@ -120,4 +119,16 @@ private function extractRecommendationItems(ResponseInterface $response): array return $this->recommendationService->getRecommendationItems($recommendationItems['recommendationItems']); } + + private function getCategoryPath(Content $content): ?string + { + $mainLocation = $content->contentInfo->getMainLocation(); + if (null === $mainLocation) { + return null; + } + + $parentLocation = $mainLocation->getParentLocation(); + + return null !== $parentLocation ? $parentLocation->pathString : null; + } } diff --git a/src/lib/Helper/LocationHelper.php b/src/lib/Helper/LocationHelper.php index a65c961d..3283a3f7 100644 --- a/src/lib/Helper/LocationHelper.php +++ b/src/lib/Helper/LocationHelper.php @@ -8,8 +8,6 @@ namespace EzSystems\EzRecommendationClient\Helper; -use eZ\Publish\API\Repository\ContentService as ContentServiceInterface; -use eZ\Publish\API\Repository\Exceptions\NotFoundException; use eZ\Publish\API\Repository\LocationService as LocationServiceInterface; use eZ\Publish\API\Repository\Values\Content\ContentInfo; @@ -18,15 +16,9 @@ final class LocationHelper /** @var \eZ\Publish\API\Repository\LocationService */ private $locationService; - /** @var \eZ\Publish\API\Repository\ContentService */ - private $contentService; - - public function __construct( - LocationServiceInterface $locationService, - ContentServiceInterface $contentService - ) { + public function __construct(LocationServiceInterface $locationService) + { $this->locationService = $locationService; - $this->contentService = $contentService; } /** @@ -44,26 +36,4 @@ public function areLocationsVisible(ContentInfo $contentInfo): bool return false; } - - /** - * Returns location path string based on $contentId. - * - * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException - */ - public function getParentLocationPathString(int $contentId): ?string - { - try { - $content = $this->contentService->loadContent($contentId); - $mainLocation = $content->contentInfo->getMainLocation(); - if (null === $mainLocation) { - return null; - } - - $parentLocation = $mainLocation->getParentLocation(); - - return null !== $parentLocation ? $parentLocation->pathString : null; - } catch (NotFoundException $exception) { - return null; - } - } }