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

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\Log\Package;
  14. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  15. use Shopware\Core\Framework\Routing\Annotation\Entity;
  16. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  17. use Shopware\Core\Framework\Routing\Annotation\Since;
  18. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  19. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  23. /**
  24.  * @Route(defaults={"_routeScope"={"store-api"}})
  25.  */
  26. #[Package('system-settings')]
  27. class ProductSearchRoute extends AbstractProductSearchRoute
  28. {
  29.     private EventDispatcherInterface $eventDispatcher;
  30.     private ProductSearchBuilderInterface $searchBuilder;
  31.     private ProductListingLoader $productListingLoader;
  32.     /**
  33.      * @internal
  34.      */
  35.     public function __construct(
  36.         ProductSearchBuilderInterface $searchBuilder,
  37.         EventDispatcherInterface $eventDispatcher,
  38.         ProductListingLoader $productListingLoader
  39.     ) {
  40.         $this->eventDispatcher $eventDispatcher;
  41.         $this->searchBuilder $searchBuilder;
  42.         $this->productListingLoader $productListingLoader;
  43.     }
  44.     public function getDecorated(): AbstractProductSearchRoute
  45.     {
  46.         throw new DecorationPatternException(self::class);
  47.     }
  48.     /**
  49.      * @Since("6.2.0.0")
  50.      * @Entity("product")
  51.      * @Route("/store-api/search", name="store-api.search", methods={"POST"})
  52.      */
  53.     public function load(Request $requestSalesChannelContext $contextCriteria $criteria): ProductSearchRouteResponse
  54.     {
  55.         if (!$request->get('search')) {
  56.             throw new MissingRequestParameterException('search');
  57.         }
  58.         $criteria->addState(Criteria::STATE_ELASTICSEARCH_AWARE);
  59.         if (!Feature::isActive('v6.5.0.0')) {
  60.             $context->getContext()->addState(Context::STATE_ELASTICSEARCH_AWARE);
  61.         }
  62.         $criteria->addFilter(
  63.             new ProductAvailableFilter($context->getSalesChannel()->getId(), ProductVisibilityDefinition::VISIBILITY_SEARCH)
  64.         );
  65.         $this->searchBuilder->build($request$criteria$context);
  66.         $result $this->productListingLoader->load($criteria$context);
  67.         $result ProductListingResult::createFrom($result);
  68.         $this->eventDispatcher->dispatch(
  69.             new ProductSearchResultEvent($request$result$context),
  70.             ProductEvents::PRODUCT_SEARCH_RESULT
  71.         );
  72.         $result->addCurrentFilter('search'$request->get('search'));
  73.         return new ProductSearchRouteResponse($result);
  74.     }
  75. }