<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\OrderManagement\PageBuilder\OrdersByStatus;
use Ibexa\FieldTypePage\FieldType\Page\Block\Definition\BlockDefinitionFactoryInterface;
use Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\BlockRenderEvents;
use Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\Event\PreRenderEvent;
use Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\Twig\TwigRenderRequest;
use Ibexa\OrderManagement\Repository\OrderStatisticsService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class BlockSubscriber implements EventSubscriberInterface
{
private QueryFactoryInterface $queryFactory;
private OrderStatisticsService $orderStatisticsService;
private BlockDefinitionFactoryInterface $blockDefinitionFactory;
public function __construct(
QueryFactoryInterface $queryFactory,
OrderStatisticsService $orderStatisticsService,
BlockDefinitionFactoryInterface $blockDefinitionFactory
) {
$this->queryFactory = $queryFactory;
$this->orderStatisticsService = $orderStatisticsService;
$this->blockDefinitionFactory = $blockDefinitionFactory;
}
public static function getSubscribedEvents(): array
{
return [
BlockRenderEvents::getBlockPreRenderEventName(Block::BLOCK_IDENTIFIER) => 'onPreRender',
];
}
public function onPreRender(PreRenderEvent $event): void
{
$request = $event->getRenderRequest();
if (!$request instanceof TwigRenderRequest) {
return;
}
$block = new Block($event->getBlockValue());
$query = $this->queryFactory->createQuery($block);
$ordersByStatus = $this->orderStatisticsService->getOrdersByStatus($query);
$request->addParameter('orders_by_status', $ordersByStatus);
$blockDefinition = $this->blockDefinitionFactory->getBlockDefinition(Block::BLOCK_IDENTIFIER);
$blockAttributeDefinition = $blockDefinition->getAttributes()[Block::ATTRIBUTE_PERIODS];
$request->addParameter(
'period_label',
array_search($block->getPeriod(), $blockAttributeDefinition->getOptions()['choices'], true)
);
}
}