/
RecommendationEventSubscriber.php
125 lines (102 loc) · 5.04 KB
/
RecommendationEventSubscriber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\PersonalizationClient\Event\Subscriber;
use Ibexa\Core\MVC\Symfony\Locale\LocaleConverterInterface;
use Ibexa\PersonalizationClient\Event\RecommendationResponseEvent;
use Ibexa\PersonalizationClient\Helper\ContentTypeHelper;
use Ibexa\PersonalizationClient\Helper\LocationHelper;
use Ibexa\PersonalizationClient\Request\BasicRecommendationRequest as Request;
use Ibexa\PersonalizationClient\Service\RecommendationServiceInterface;
use Ibexa\PersonalizationClient\SPI\RecommendationRequest;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Response;
final class RecommendationEventSubscriber implements EventSubscriberInterface
{
private const LOCALE_REQUEST_KEY = '_locale';
private const DEFAULT_LOCALE = 'eng-GB';
/** @var \Ibexa\PersonalizationClient\Service\RecommendationServiceInterface */
private $recommendationService;
/** @var \Ibexa\Core\MVC\Symfony\Locale\LocaleConverterInterface */
private $localeConverter;
/** @var \Psr\Log\LoggerInterface */
private $logger;
/** @var \Ibexa\PersonalizationClient\Helper\ContentTypeHelper */
private $contentTypeHelper;
/** @var \Ibexa\PersonalizationClient\Helper\LocationHelper */
private $locationHelper;
public function __construct(
RecommendationServiceInterface $recommendationService,
LocaleConverterInterface $localeConverter,
LoggerInterface $logger,
ContentTypeHelper $contentTypeHelper,
LocationHelper $locationHelper
) {
$this->recommendationService = $recommendationService;
$this->localeConverter = $localeConverter;
$this->logger = $logger;
$this->contentTypeHelper = $contentTypeHelper;
$this->locationHelper = $locationHelper;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array
{
return [
RecommendationResponseEvent::class => ['onRecommendationResponse', 10],
];
}
/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
*/
public function onRecommendationResponse(RecommendationResponseEvent $event): void
{
$recommendationRequest = $this->getRecommendationRequest($event->getParameterBag());
$response = $this->recommendationService->getRecommendations($recommendationRequest);
if (!$response) {
return;
}
$event->setRecommendationItems($this->extractRecommendationItems($response));
}
/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
*/
private function getRecommendationRequest(ParameterBag $parameterBag): RecommendationRequest
{
$contextItems = (int) $parameterBag->get(Request::CONTEXT_ITEMS_KEY, 0);
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::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, []),
]);
}
private function getRequestLanguage(?string $locale): string
{
return $this->localeConverter->convertToEz($locale) ?? self::DEFAULT_LOCALE;
}
private function extractRecommendationItems(ResponseInterface $response): array
{
if ($response->getStatusCode() !== Response::HTTP_OK) {
$this->logger->warning('RecommendationApi: StatusCode: ' . $response->getStatusCode() . ' Message: ' . $response->getReasonPhrase());
}
$recommendations = $response->getBody()->getContents();
$recommendationItems = json_decode($recommendations, true);
return $this->recommendationService->getRecommendationItems($recommendationItems['recommendationItems']);
}
}
class_alias(RecommendationEventSubscriber::class, 'EzSystems\EzRecommendationClient\Event\Subscriber\RecommendationEventSubscriber');