Skip to content

Commit

Permalink
[shopsys] added personalization to Luigi's Box search (#3044)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasLudvik authored Mar 13, 2024
2 parents e2de429 + e125087 commit b05634e
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 19 deletions.
73 changes: 58 additions & 15 deletions src/Component/LuigisBox/LuigisBoxClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Shopsys\LuigisBoxBundle\Component\LuigisBox\Exception\LuigisBoxActionNotRecognizedException;
use Shopsys\LuigisBoxBundle\Component\LuigisBox\Exception\LuigisBoxIndexNotRecognizedException;
use Shopsys\ProductFeed\LuigisBoxBundle\Model\FeedItem\LuigisBoxProductFeedItem;
use Symfony\Bridge\Monolog\Logger;
use Throwable;

class LuigisBoxClient
{
Expand All @@ -27,11 +29,13 @@ class LuigisBoxClient
* @param string $luigisBoxApiUrl
* @param array $trackerIdsByDomainIds
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
* @param \Symfony\Bridge\Monolog\Logger $logger
*/
public function __construct(
protected readonly string $luigisBoxApiUrl,
protected readonly array $trackerIdsByDomainIds,
protected readonly Domain $domain,
protected readonly Logger $logger,
) {
}

Expand All @@ -58,6 +62,7 @@ protected function getTrackerId(): string
* @param int $page
* @param array<string, int> $limitsByType
* @param array $filter
* @param string $userIdentifier
* @param string|null $orderingMode
* @return \Shopsys\LuigisBoxBundle\Component\LuigisBox\LuigisBoxResult[]
*/
Expand All @@ -67,26 +72,46 @@ public function getData(
int $page,
array $limitsByType,
array $filter,
string $userIdentifier,
?string $orderingMode,
): array {
$this->checkNecessaryConfigurationIsSet();
$this->validateActionIsValid($action);

$data = json_decode(
file_get_contents(
$this->getLuigisBoxApiUrl(
$query,
$action,
$page,
$limitsByType,
$filter,
$orderingMode,
try {
$data = json_decode(
file_get_contents(
$this->getLuigisBoxApiUrl(
$query,
$action,
$page,
$limitsByType,
$filter,
$userIdentifier,
$orderingMode,
),
),
),
true,
512,
JSON_THROW_ON_ERROR,
);
true,
512,
JSON_THROW_ON_ERROR,
);
} catch (Throwable $e) {
$this->logger->error(
'Luigi\'s Box API request failed.',
[
'exception' => $e,
'query' => $query,
'action' => $action,
'page' => $page,
'limitsByType' => $limitsByType,
'filter' => $filter,
'userIdentifier' => $userIdentifier,
'orderingMode' => $orderingMode,
],
);

return $this->getEmptyResults(array_keys($limitsByType));
}

if ($action === self::ACTION_SEARCH) {
$data = $data['results'];
Expand All @@ -95,6 +120,21 @@ public function getData(
return $this->getResultsIndexedByItemType($data, $action, array_keys($limitsByType));
}

/**
* @param string[] $types
* @return \Shopsys\LuigisBoxBundle\Component\LuigisBox\LuigisBoxResult[]
*/
protected function getEmptyResults(array $types): array
{
$resultsByType = [];

foreach ($types as $type) {
$resultsByType[$type] = new LuigisBoxResult([], [], 0);
}

return $resultsByType;
}

/**
* @param array $data
* @param string $action
Expand All @@ -115,6 +155,7 @@ protected function getTotalHitsFromData(array $data, string $action): int
* @param int $page
* @param array $limitsByType
* @param array $filter
* @param string $userIdentifier
* @param string|null $orderingMode
* @return string
*/
Expand All @@ -124,13 +165,15 @@ protected function getLuigisBoxApiUrl(
int $page,
array $limitsByType,
array $filter,
string $userIdentifier,
?string $orderingMode,
): string {
$url = $this->luigisBoxApiUrl .
$action . '/' .
'?tracker_id=' . $this->getTrackerId() .
'&q=' . urlencode($query) .
'&hit_fields=url';
'&hit_fields=url' .
'&user_id=' . $userIdentifier;

if ($action === self::ACTION_SEARCH) {
$quicksearchTypesWithLimits = $this->getQuicksearchTypesWithLimits($limitsByType);
Expand Down
10 changes: 10 additions & 0 deletions src/Model/Batch/LuigisBoxBatchLoadData.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class LuigisBoxBatchLoadData
* @param string $endpoint
* @param int $page
* @param array $filter
* @param string $userIdentifier
* @param string|null $orderingMode
*/
public function __construct(
Expand All @@ -22,6 +23,7 @@ public function __construct(
protected readonly string $endpoint,
protected readonly int $page,
protected readonly array $filter,
protected readonly string $userIdentifier,
protected readonly ?string $orderingMode = null,
) {
}
Expand Down Expand Up @@ -81,4 +83,12 @@ public function getOrderingMode(): ?string
{
return $this->orderingMode;
}

/**
* @return string
*/
public function getUserIdentifier(): string
{
return $this->userIdentifier;
}
}
6 changes: 4 additions & 2 deletions src/Model/Batch/LuigisBoxBatchLoadDataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ public function create(
$luigisBoxFilter = $this->productFilterToLuigisBoxFilterMapper->mapOnlyType($type);
}

$search = $argument['search'] ?? '';
$search = $argument['searchInput']['search'] ?? '';
$orderingMode = $argument['orderingMode'];
$endpoint = $argument['isAutocomplete'] === true ? LuigisBoxClient::ACTION_AUTOCOMPLETE : LuigisBoxClient::ACTION_SEARCH;
$endpoint = $argument['searchInput']['isAutocomplete'] === true ? LuigisBoxClient::ACTION_AUTOCOMPLETE : LuigisBoxClient::ACTION_SEARCH;
$userIdentifier = $argument['searchInput']['userIdentifier'];

return new LuigisBoxBatchLoadData(
$type,
Expand All @@ -48,6 +49,7 @@ public function create(
$endpoint,
$page,
$luigisBoxFilter,
$userIdentifier,
$orderingMode,
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Model/Batch/LuigisBoxBatchLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ public function loadByBatchData(array $luigisBoxBatchLoadData): Promise
$page = $mainBatchLoadData->getPage();
$productFilter = $mainBatchLoadData->getFilter();
$productOrderingMode = $mainBatchLoadData->getOrderingMode();
$userIdentifier = $mainBatchLoadData->getUserIdentifier();
$limitsByType = [];

foreach ($luigisBoxBatchLoadData as $luigisBoxBatchLoadDataItem) {
$limitsByType[$luigisBoxBatchLoadDataItem->getType()] = $luigisBoxBatchLoadDataItem->getLimit();
}

return $this->promiseAdapter->all($this->mapDataByTypes(
$this->luigisBoxClient->getData($query, $endpoint, $page, $limitsByType, $productFilter, $productOrderingMode),
$this->luigisBoxClient->getData($query, $endpoint, $page, $limitsByType, $productFilter, $userIdentifier, $productOrderingMode),
$limitsByType,
));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Product/ProductSearchResultsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function getProductsSearchResults(
Argument $argument,
ProductFilterData $productFilterData,
): Promise {
$search = $argument['search'] ?? '';
$search = $argument['searchInput']['search'] ?? '';
$orderingMode = $argument['orderingMode'];
$luigisBoxFilter = $this->productFilterToLuigisBoxFilterMapper->map(LuigisBoxClient::TYPE_IN_LUIGIS_BOX_PRODUCT, $productFilterData, $this->domain);

Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ services:
arguments:
$luigisBoxApiUrl: '%env(string:LUIGIS_BOX_API_URL)%'
$trackerIdsByDomainIds: '%env(json:LUIGIS_BOX_TRACKER_IDS_BY_DOMAIN_IDS)%'
$logger: '@monolog.logger'

Shopsys\LuigisBoxBundle\Model\Product\ProductSearchResultsProvider:
arguments:
Expand Down

0 comments on commit b05634e

Please sign in to comment.