vendor/ibexa/product-catalog/src/bundle/EventSubscriber/PageBuilder/ProductsWithLowestStockBlockSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. declare(strict_types=1);
  7. namespace Ibexa\Bundle\ProductCatalog\EventSubscriber\PageBuilder;
  8. use Ibexa\Contracts\ProductCatalog\ProductServiceInterface;
  9. use Ibexa\Contracts\ProductCatalog\QueryTypeRegistryInterface;
  10. use Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\BlockRenderEvents;
  11. use Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\Event\PreRenderEvent;
  12. use Ibexa\ProductCatalog\QueryType\Product\ProductsWithLowestStockQueryType;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. final class ProductsWithLowestStockBlockSubscriber implements EventSubscriberInterface
  15. {
  16.     private const BLOCK_IDENTIFIER 'products_with_lowest_stock';
  17.     private ProductServiceInterface $productService;
  18.     private QueryTypeRegistryInterface $queryTypeRegistry;
  19.     public function __construct(
  20.         ProductServiceInterface $productService,
  21.         QueryTypeRegistryInterface $queryTypeRegistry
  22.     ) {
  23.         $this->productService $productService;
  24.         $this->queryTypeRegistry $queryTypeRegistry;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             BlockRenderEvents::getBlockPreRenderEventName(self::BLOCK_IDENTIFIER) => 'onBlockPreRender',
  30.         ];
  31.     }
  32.     public function onBlockPreRender(PreRenderEvent $event): void
  33.     {
  34.         /** @var \Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\Twig\TwigRenderRequest $request */
  35.         $request $event->getRenderRequest();
  36.         $request->addParameter('block_name'$event->getBlockValue()->getName());
  37.         $queryType $this->queryTypeRegistry->getQueryType(ProductsWithLowestStockQueryType::NAME);
  38.         $productList $this->productService->findProducts($queryType->getQuery($request->getParameters()));
  39.         $request->addParameter(
  40.             'products',
  41.             $productList
  42.         );
  43.     }
  44. }