<?php
namespace App\Controller\Custom;
use App\Repository\ProductRepository;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class SearchController extends AbstractController
{
protected ProductRepository $productRepository;
protected ChannelContextInterface $channelContext;
protected LocaleContextInterface $localeContext;
/**
* @param ProductRepository $productRepository
* @param ChannelContextInterface $channelContext
* @param LocaleContextInterface $localeContext
*/
public function __construct(ProductRepository $productRepository, ChannelContextInterface $channelContext, LocaleContextInterface $localeContext)
{
$this->productRepository = $productRepository;
$this->channelContext = $channelContext;
$this->localeContext = $localeContext;
}
/**
* @param Request $request
* @return Response
*/
public function search(Request $request): Response
{
$searchTerm = $request->query->get('q');
$channel = $this->channelContext->getChannel();
$localeCode = $this->localeContext->getLocaleCode();
$total = count($this->productRepository->findByTerm($channel, $localeCode, $searchTerm));
$products = $this->productRepository->findByTerm($channel, $localeCode, $searchTerm, 3);
return $this->render('bundles/SyliusShopBundle/Product/Search/index.html.twig', [
'products' => $products,
'total' => $total,
'term' => $searchTerm
]);
}
}