<?php
declare(strict_types=1);
namespace App\Module\Cms\Subscriber;
use App\Module\Account\Action\GetDataAction;
use App\Module\Account\Action\GetOrdersAction;
use App\Module\Account\Action\GetOverviewAction;
use App\Module\Address\Action\GetAddressAction;
use App\Module\Cms\Provider\CmsContentProvider;
use App\Module\ShoppingList\Action\GetShoppingListAction;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Class AccountSlotSubscriber
*
* @author André Varelmann <andre.varelmann@bestit-online.de>
* @package App\Module\Cms\Subscriber
*/
class AccountSlotSubscriber implements EventSubscriberInterface
{
/**
* Account action list this subscriber is active on
*/
private const ACCOUNT_CONTROLLER_NAMES = [
GetOverviewAction::class,
GetDataAction::class,
GetOrdersAction::class,
GetAddressAction::class,
GetShoppingListAction::class
];
private CmsContentProvider $provider;
/**
* AccountSlotSubscriber constructor.
*
* @param CmsContentProvider $provider
*/
public function __construct(
CmsContentProvider $provider
) {
$this->provider = $provider;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [KernelEvents::VIEW => 'onKernelResponse'];
}
/**
* @param ViewEvent $event
*
* @return void
*/
public function onKernelResponse(ViewEvent $event): void
{
if (!$this->isAccountController($event->getRequest()->attributes->get('_controller'))) {
return;
}
$controllerResult = $event->getControllerResult();
$slot = $this->provider->getAccountSlots();
if ($slot !== null) {
$controllerResult['cms'] = $slot;
$event->setControllerResult($controllerResult);
}
}
/**
* Check if the event is fired after a account action
*
* @param string $actionName
*
* @return bool
*/
private function isAccountController(string $actionName): bool
{
foreach (self::ACCOUNT_CONTROLLER_NAMES as $controllerName) {
if (strpos($actionName, $controllerName) !== false) {
return true;
}
}
return false;
}
}