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

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