vendor/ibexa/order-management/src/lib/PageBuilder/OrdersByStatus/BlockSubscriber.php line 43

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\OrderManagement\PageBuilder\OrdersByStatus;
  8. use Ibexa\FieldTypePage\FieldType\Page\Block\Definition\BlockDefinitionFactoryInterface;
  9. use Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\BlockRenderEvents;
  10. use Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\Event\PreRenderEvent;
  11. use Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\Twig\TwigRenderRequest;
  12. use Ibexa\OrderManagement\Repository\OrderStatisticsService;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. final class BlockSubscriber implements EventSubscriberInterface
  15. {
  16. private QueryFactoryInterface $queryFactory;
  17. private OrderStatisticsService $orderStatisticsService;
  18. private BlockDefinitionFactoryInterface $blockDefinitionFactory;
  19. public function __construct(
  20. QueryFactoryInterface $queryFactory,
  21. OrderStatisticsService $orderStatisticsService,
  22. BlockDefinitionFactoryInterface $blockDefinitionFactory
  23. ) {
  24. $this->queryFactory = $queryFactory;
  25. $this->orderStatisticsService = $orderStatisticsService;
  26. $this->blockDefinitionFactory = $blockDefinitionFactory;
  27. }
  28. public static function getSubscribedEvents(): array
  29. {
  30. return [
  31. BlockRenderEvents::getBlockPreRenderEventName(Block::BLOCK_IDENTIFIER) => 'onPreRender',
  32. ];
  33. }
  34. public function onPreRender(PreRenderEvent $event): void
  35. {
  36. $request = $event->getRenderRequest();
  37. if (!$request instanceof TwigRenderRequest) {
  38. return;
  39. }
  40. $block = new Block($event->getBlockValue());
  41. $query = $this->queryFactory->createQuery($block);
  42. $ordersByStatus = $this->orderStatisticsService->getOrdersByStatus($query);
  43. $request->addParameter('orders_by_status', $ordersByStatus);
  44. $blockDefinition = $this->blockDefinitionFactory->getBlockDefinition(Block::BLOCK_IDENTIFIER);
  45. $blockAttributeDefinition = $blockDefinition->getAttributes()[Block::ATTRIBUTE_PERIODS];
  46. $request->addParameter(
  47. 'period_label',
  48. array_search($block->getPeriod(), $blockAttributeDefinition->getOptions()['choices'], true)
  49. );
  50. }
  51. }