Files
kupshop/bundles/KupShop/LuigisBoxBundle/View/SearchPreloadView.php
2025-08-02 16:30:27 +02:00

61 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\LuigisBoxBundle\View;
use KupShop\LuigisBoxBundle\Api\Builder\TopItemsUrlBuilder;
use KupShop\LuigisBoxBundle\Api\TopItemsRequest;
use KupShop\LuigisBoxBundle\Search\FulltextLuigisBox;
class SearchPreloadView extends \KupShop\CatalogBundle\View\SearchPreloadView
{
private TopItemsRequest $searchRequest;
/**
* @required
*/
public function setSearchRequest(TopItemsRequest $searchRequest): void
{
$this->searchRequest = $searchRequest;
}
protected function getPreloadData()
{
$urlBuilder = new TopItemsUrlBuilder();
$urlBuilder
->setSearchTypes([
[FulltextLuigisBox::INDEX_PRODUCTS],
[FulltextLuigisBox::INDEX_SECTIONS],
[FulltextLuigisBox::INDEX_PRODUCERS],
]);
$searchResponse = $this->searchRequest->search($urlBuilder);
$typesIds = [];
foreach ($searchResponse['hits'] ?? [] as $result) {
$type = $result['type'] ?? false;
$id = $result['attributes']['item_group_id'][0] ?? $result['attributes']['id'][0] ?? $result['attributes']['identity'][0] ?? $result['attributes']['original_url'] ?? false;
if ($type && $id) {
$typesIds[$type][] = (int) $id;
}
}
$typesMap = array_flip(FulltextLuigisBox::LB_TYPES);
$vars = [];
foreach ($typesIds as $type => $ids) {
$type = $typesMap[$type] ?? '';
$method = 'preload'.ucfirst($type);
if (method_exists($this, $method)) {
$vars[$type] = $this->{$method}($ids ?? []);
}
}
return $vars;
}
}