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

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