<?php
declare(strict_types=1);
namespace App\Module\Cms\Subscriber;
use App\Module\Catalog\Action\GetCatalogAction;
use App\Module\Catalog\Action\GetListingAction;
use App\Module\Cms\Provider\CmsContentProvider;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use function array_merge;
/**
* Class CommerceSlotSubscriber
*
* @author André Varelmann <andre.varelmann@bestit-online.de>
* @package App\Module\Cms\Subscriber
*/
class CategorySlotSubscriber implements EventSubscriberInterface
{
/**
* Catalog action list this subscriber is active on
*/
private const CATEGORY_CONTROLLER_NAMES = [
GetCatalogAction::class,
GetListingAction::class
];
private CmsContentProvider $provider;
/**
* CommerceSlotSubscriber 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->isCategoryController($event->getRequest()->attributes->get('_controller'))) {
return;
}
$cms = $this->provider->getContentBySlugForCurrentPage('commerce');
if ($cms !== null) {
$event->setControllerResult(array_merge($event->getControllerResult(), ['slots' => $cms->content]));
}
}
/**
* Check if the event is fired after a commerce action
*
* @param string $actionName
*
* @return bool
*/
private function isCategoryController(string $actionName): bool
{
foreach (self::CATEGORY_CONTROLLER_NAMES as $controllerName) {
if (strpos($actionName, $controllerName) !== false) {
return true;
}
}
return false;
}
}