vendor/shopware/core/Content/Product/SalesChannel/Listing/ProductListingRoute.php line 91

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Product\SalesChannel\Listing;
  3. use Shopware\Core\Content\Category\CategoryDefinition;
  4. use Shopware\Core\Content\Category\CategoryEntity;
  5. use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition;
  6. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  7. use Shopware\Core\Content\Product\SalesChannel\ProductAvailableFilter;
  8. use Shopware\Core\Content\ProductStream\Service\ProductStreamBuilderInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  13. use Shopware\Core\Framework\Routing\Annotation\Entity;
  14. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  15. use Shopware\Core\Framework\Routing\Annotation\Since;
  16. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  20. /**
  21.  * @Route(defaults={"_routeScope"={"store-api"}})
  22.  */
  23. class ProductListingRoute extends AbstractProductListingRoute
  24. {
  25.     /**
  26.      * @var ProductListingLoader
  27.      */
  28.     private $listingLoader;
  29.     /**
  30.      * @var EventDispatcherInterface
  31.      */
  32.     private $eventDispatcher;
  33.     /**
  34.      * @var EntityRepositoryInterface
  35.      */
  36.     private $categoryRepository;
  37.     /**
  38.      * @var ProductStreamBuilderInterface
  39.      */
  40.     private $productStreamBuilder;
  41.     /**
  42.      * @internal
  43.      */
  44.     public function __construct(
  45.         ProductListingLoader $listingLoader,
  46.         EventDispatcherInterface $eventDispatcher,
  47.         EntityRepositoryInterface $categoryRepository,
  48.         ProductStreamBuilderInterface $productStreamBuilder
  49.     ) {
  50.         $this->eventDispatcher $eventDispatcher;
  51.         $this->listingLoader $listingLoader;
  52.         $this->categoryRepository $categoryRepository;
  53.         $this->productStreamBuilder $productStreamBuilder;
  54.     }
  55.     public function getDecorated(): AbstractProductListingRoute
  56.     {
  57.         throw new DecorationPatternException(self::class);
  58.     }
  59.     /**
  60.      * @Since("6.2.0.0")
  61.      * @Entity("product")
  62.      * @Route("/store-api/product-listing/{categoryId}", name="store-api.product.listing", methods={"POST"})
  63.      */
  64.     public function load(string $categoryIdRequest $requestSalesChannelContext $contextCriteria $criteria): ProductListingRouteResponse
  65.     {
  66.         $criteria->addFilter(
  67.             new ProductAvailableFilter($context->getSalesChannel()->getId(), ProductVisibilityDefinition::VISIBILITY_ALL)
  68.         );
  69.         $criteria->setTitle('product-listing-route::loading');
  70.         $categoryCriteria = new Criteria([$categoryId]);
  71.         $categoryCriteria->setTitle('product-listing-route::category-loading');
  72.         /** @var CategoryEntity $category */
  73.         $category $this->categoryRepository->search($categoryCriteria$context->getContext())->first();
  74.         $streamId $this->extendCriteria($context$criteria$category);
  75.         $entities $this->listingLoader->load($criteria$context);
  76.         /** @var ProductListingResult $result */
  77.         $result ProductListingResult::createFrom($entities);
  78.         $result->addState(...$entities->getStates());
  79.         $result->addCurrentFilter('navigationId'$categoryId);
  80.         $this->eventDispatcher->dispatch(
  81.             new ProductListingResultEvent($request$result$context)
  82.         );
  83.         $result->setStreamId($streamId);
  84.         return new ProductListingRouteResponse($result);
  85.     }
  86.     private function extendCriteria(SalesChannelContext $salesChannelContextCriteria $criteriaCategoryEntity $category): ?string
  87.     {
  88.         if ($category->getProductAssignmentType() === CategoryDefinition::PRODUCT_ASSIGNMENT_TYPE_PRODUCT_STREAM && $category->getProductStreamId() !== null) {
  89.             $filters $this->productStreamBuilder->buildFilters(
  90.                 $category->getProductStreamId(),
  91.                 $salesChannelContext->getContext()
  92.             );
  93.             $criteria->addFilter(...$filters);
  94.             return $category->getProductStreamId();
  95.         }
  96.         $criteria->addFilter(
  97.             new EqualsFilter('product.categoriesRo.id'$category->getId())
  98.         );
  99.         return null;
  100.     }
  101. }