<?php declare(strict_types=1);
namespace Acris\Faq\Storefront\Subscriber;
use Acris\Faq\Components\ContentSearch\FaqSearchService;
use Acris\Faq\Components\ContentSearch\Struct\ContentSearchSynonyms;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\PageLoadedEvent;
use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
use Shopware\Storefront\Page\Suggest\SuggestPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ContentSearchSubscriber implements EventSubscriberInterface
{
/**
* @var SystemConfigService
*/
private $configService;
/**
* @var FaqSearchService
*/
private FaqSearchService $faqSearchService;
public function __construct(
SystemConfigService $configService,
FaqSearchService $faqSearchService
)
{
$this->configService = $configService;
$this->faqSearchService = $faqSearchService;
}
public static function getSubscribedEvents()
{
return [
SuggestPageLoadedEvent::class => [
['onSearchPage', 200]
],
SearchPageLoadedEvent::class => [
['onSearchPage', 200]
]
];
}
public function onSearchPage(PageLoadedEvent $event)
{
$page = $event->getPage();
$request = $event->getRequest();
$term = $request->get('search');
$maxResults = 16;
if ($event->getContext()->hasExtension(ContentSearchSynonyms::EXTENSION_KEY)) {
$tokens = $event->getContext()->getExtension(ContentSearchSynonyms::EXTENSION_KEY)->getSynonyms();
$tokensString = \implode(" ", $tokens);
} else {
$tokensString = $term;
}
if($this->configService->get('AcrisFaqCS.config.faq', $event->getSalesChannelContext()->getSalesChannel()->getId()) === true) {
if($event instanceof SuggestPageLoadedEvent) {
$maxResults = $this->configService->get('AcrisFaqCS.config.faqSuggestMaxResults', $event->getSalesChannelContext()->getSalesChannel()->getId());
}
$faqSearchResults = $this->faqSearchService->search($tokensString, $event->getSalesChannelContext(), $maxResults);
$page->addExtension('acrisFaqSearchResult', $faqSearchResults);
}
}
}