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

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