vendor/shopware/core/Content/Product/SalesChannel/Suggest/ProductSuggestRoute.php line 69

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Product\SalesChannel\Suggest;
  3. use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition;
  4. use Shopware\Core\Content\Product\Events\ProductSuggestCriteriaEvent;
  5. use Shopware\Core\Content\Product\Events\ProductSuggestResultEvent;
  6. use Shopware\Core\Content\Product\ProductEvents;
  7. use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingLoader;
  8. use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingResult;
  9. use Shopware\Core\Content\Product\SalesChannel\ProductAvailableFilter;
  10. use Shopware\Core\Content\Product\SearchKeyword\ProductSearchBuilderInterface;
  11. use Shopware\Core\Framework\Context;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\Feature;
  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. class ProductSuggestRoute extends AbstractProductSuggestRoute
  27. {
  28.     /**
  29.      * @var EventDispatcherInterface
  30.      */
  31.     private $eventDispatcher;
  32.     /**
  33.      * @var ProductSearchBuilderInterface
  34.      */
  35.     private $searchBuilder;
  36.     /**
  37.      * @var ProductListingLoader
  38.      */
  39.     private $productListingLoader;
  40.     /**
  41.      * @internal
  42.      */
  43.     public function __construct(
  44.         ProductSearchBuilderInterface $searchBuilder,
  45.         EventDispatcherInterface $eventDispatcher,
  46.         ProductListingLoader $productListingLoader
  47.     ) {
  48.         $this->eventDispatcher $eventDispatcher;
  49.         $this->searchBuilder $searchBuilder;
  50.         $this->productListingLoader $productListingLoader;
  51.     }
  52.     public function getDecorated(): AbstractProductSuggestRoute
  53.     {
  54.         throw new DecorationPatternException(self::class);
  55.     }
  56.     /**
  57.      * @Since("6.2.0.0")
  58.      * @Entity("product")
  59.      * @Route("/store-api/search-suggest", name="store-api.search.suggest", methods={"POST"})
  60.      */
  61.     public function load(Request $requestSalesChannelContext $contextCriteria $criteria): ProductSuggestRouteResponse
  62.     {
  63.         if (!$request->get('search')) {
  64.             throw new MissingRequestParameterException('search');
  65.         }
  66.         $criteria->addFilter(
  67.             new ProductAvailableFilter($context->getSalesChannel()->getId(), ProductVisibilityDefinition::VISIBILITY_SEARCH)
  68.         );
  69.         $criteria->addState(Criteria::STATE_ELASTICSEARCH_AWARE);
  70.         if (!Feature::isActive('v6.5.0.0')) {
  71.             $context->getContext()->addState(Context::STATE_ELASTICSEARCH_AWARE);
  72.         }
  73.         $this->searchBuilder->build($request$criteria$context);
  74.         $this->eventDispatcher->dispatch(
  75.             new ProductSuggestCriteriaEvent($request$criteria$context),
  76.             ProductEvents::PRODUCT_SUGGEST_CRITERIA
  77.         );
  78.         $result $this->productListingLoader->load($criteria$context);
  79.         $result ProductListingResult::createFrom($result);
  80.         $this->eventDispatcher->dispatch(
  81.             new ProductSuggestResultEvent($request$result$context),
  82.             ProductEvents::PRODUCT_SUGGEST_RESULT
  83.         );
  84.         return new ProductSuggestRouteResponse($result);
  85.     }
  86. }