vendor/shopware/core/Content/Product/SalesChannel/Search/ProductSearchRoute.php line 79

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Product\SalesChannel\Search;
  3. use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition;
  4. use Shopware\Core\Content\Product\Events\ProductSearchResultEvent;
  5. use Shopware\Core\Content\Product\ProductEvents;
  6. use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingLoader;
  7. use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingResult;
  8. use Shopware\Core\Content\Product\SalesChannel\ProductAvailableFilter;
  9. use Shopware\Core\Content\Product\SearchKeyword\ProductSearchBuilderInterface;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\Feature;
  13. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  14. use Shopware\Core\Framework\Routing\Annotation\Entity;
  15. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  16. use Shopware\Core\Framework\Routing\Annotation\Since;
  17. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  18. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  22. /**
  23.  * @Route(defaults={"_routeScope"={"store-api"}})
  24.  */
  25. class ProductSearchRoute extends AbstractProductSearchRoute
  26. {
  27.     private EventDispatcherInterface $eventDispatcher;
  28.     private ProductSearchBuilderInterface $searchBuilder;
  29.     private ProductListingLoader $productListingLoader;
  30.     /**
  31.      * @internal
  32.      */
  33.     public function __construct(
  34.         ProductSearchBuilderInterface $searchBuilder,
  35.         EventDispatcherInterface $eventDispatcher,
  36.         ProductListingLoader $productListingLoader
  37.     ) {
  38.         $this->eventDispatcher $eventDispatcher;
  39.         $this->searchBuilder $searchBuilder;
  40.         $this->productListingLoader $productListingLoader;
  41.     }
  42.     public function getDecorated(): AbstractProductSearchRoute
  43.     {
  44.         throw new DecorationPatternException(self::class);
  45.     }
  46.     /**
  47.      * @Since("6.2.0.0")
  48.      * @Entity("product")
  49.      * @Route("/store-api/search", name="store-api.search", methods={"POST"})
  50.      */
  51.     public function load(Request $requestSalesChannelContext $contextCriteria $criteria): ProductSearchRouteResponse
  52.     {
  53.         if (!$request->get('search')) {
  54.             throw new MissingRequestParameterException('search');
  55.         }
  56.         $criteria->addState(Criteria::STATE_ELASTICSEARCH_AWARE);
  57.         if (!Feature::isActive('v6.5.0.0')) {
  58.             $context->getContext()->addState(Context::STATE_ELASTICSEARCH_AWARE);
  59.         }
  60.         $criteria->addFilter(
  61.             new ProductAvailableFilter($context->getSalesChannel()->getId(), ProductVisibilityDefinition::VISIBILITY_SEARCH)
  62.         );
  63.         $this->searchBuilder->build($request$criteria$context);
  64.         $result $this->productListingLoader->load($criteria$context);
  65.         $result ProductListingResult::createFrom($result);
  66.         $this->eventDispatcher->dispatch(
  67.             new ProductSearchResultEvent($request$result$context),
  68.             ProductEvents::PRODUCT_SEARCH_RESULT
  69.         );
  70.         $result->addCurrentFilter('search'$request->get('search'));
  71.         return new ProductSearchRouteResponse($result);
  72.     }
  73. }