<?php
declare(strict_types=1);
namespace Swag\EnterpriseSearch\Action\MultiSearch;
use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
use Swag\EnterpriseSearch\Action\ActionSearchProcessor;
use Swag\EnterpriseSearch\Search\MultiSearchGatewayEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
class MultiSearchActionSubscriber implements EventSubscriberInterface
{
/**
* @var ActionSearchProcessor
*/
private $actionSearchProcessor;
public function __construct(ActionSearchProcessor $actionSearchProcessor)
{
$this->actionSearchProcessor = $actionSearchProcessor;
}
public static function getSubscribedEvents(): array
{
return [
MultiSearchGatewayEvent::class => 'processActions',
];
}
public function processActions(MultiSearchGatewayEvent $event): void
{
$context = $event->getContext();
$request = $event->getRequest();
$multiSearchActionProcessorEnvironment = new MultiSearchActionProcessorEnvironment($context, $request);
$this->actionSearchProcessor->applySearchActions($context, $this->getSearchTerm($request), $multiSearchActionProcessorEnvironment);
}
private function getSearchTerm(Request $request): string
{
$term = $request->query->get('search');
if (is_array($term)) {
$term = implode(' ', $term);
}
$term = trim((string) $term);
if (empty($term)) {
throw new MissingRequestParameterException('search');
}
return $term;
}
}