Skip to content

Commit

Permalink
IBX-2332: Added remoteId support in RecommendationEventSubscriber to …
Browse files Browse the repository at this point in the history
…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 <[email protected]>

* IBX-2332: Added remoteId support in RecommendationEventSubscriber to fetch recommendations

https://issues.ibexa.co/browse/IBX-2332

Co-authored-by: Konrad Oboza <[email protected]>
  • Loading branch information
ciastektk and Konrad Oboza authored Feb 17, 2022
1 parent 41d6c0a commit ee85d79
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 53 deletions.
53 changes: 32 additions & 21 deletions src/lib/Event/Subscriber/RecommendationEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,36 +23,31 @@

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;

/** @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;
}

/**
Expand Down Expand Up @@ -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, []),
Expand All @@ -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;
}
}
34 changes: 2 additions & 32 deletions src/lib/Helper/LocationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

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

0 comments on commit ee85d79

Please sign in to comment.