vendor/shopware/storefront/Page/Search/SearchPageLoader.php line 60

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Page\Search;
  3. use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
  4. use Shopware\Core\Content\Product\SalesChannel\Search\AbstractProductSearchRoute;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  10. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. class SearchPageLoader
  13. {
  14.     /**
  15.      * @var GenericPageLoaderInterface
  16.      */
  17.     private $genericLoader;
  18.     /**
  19.      * @var EventDispatcherInterface
  20.      */
  21.     private $eventDispatcher;
  22.     /**
  23.      * @var AbstractProductSearchRoute
  24.      */
  25.     private $productSearchRoute;
  26.     /**
  27.      * @internal
  28.      */
  29.     public function __construct(
  30.         GenericPageLoaderInterface $genericLoader,
  31.         AbstractProductSearchRoute $productSearchRoute,
  32.         EventDispatcherInterface $eventDispatcher
  33.     ) {
  34.         $this->genericLoader $genericLoader;
  35.         $this->productSearchRoute $productSearchRoute;
  36.         $this->eventDispatcher $eventDispatcher;
  37.     }
  38.     /**
  39.      * @throws CategoryNotFoundException
  40.      * @throws InconsistentCriteriaIdsException
  41.      * @throws MissingRequestParameterException
  42.      */
  43.     public function load(Request $requestSalesChannelContext $salesChannelContext): SearchPage
  44.     {
  45.         $page $this->genericLoader->load($request$salesChannelContext);
  46.         $page SearchPage::createFrom($page);
  47.         if ($page->getMetaInformation()) {
  48.             $page->getMetaInformation()->setRobots('noindex,follow');
  49.         }
  50.         if (!$request->query->has('search')) {
  51.             throw new MissingRequestParameterException('search');
  52.         }
  53.         $criteria = new Criteria();
  54.         $criteria->setTitle('search-page');
  55.         $result $this->productSearchRoute
  56.             ->load($request$salesChannelContext$criteria)
  57.             ->getListingResult();
  58.         $page->setListing($result);
  59.         $page->setSearchTerm(
  60.             (string) $request->query->get('search')
  61.         );
  62.         $this->eventDispatcher->dispatch(
  63.             new SearchPageLoadedEvent($page$salesChannelContext$request)
  64.         );
  65.         return $page;
  66.     }
  67. }