src/Module/Cms/Subscriber/CategorySlotSubscriber.php line 58

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Module\Cms\Subscriber;
  4. use App\Module\Catalog\Action\GetCatalogAction;
  5. use App\Module\Catalog\Action\GetListingAction;
  6. use App\Module\Cms\Provider\CmsContentProvider;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpKernel\Event\ViewEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use function array_merge;
  11. /**
  12. * Class CommerceSlotSubscriber
  13. *
  14. * @author André Varelmann <andre.varelmann@bestit-online.de>
  15. * @package App\Module\Cms\Subscriber
  16. */
  17. class CategorySlotSubscriber implements EventSubscriberInterface
  18. {
  19. /**
  20. * Catalog action list this subscriber is active on
  21. */
  22. private const CATEGORY_CONTROLLER_NAMES = [
  23. GetCatalogAction::class,
  24. GetListingAction::class
  25. ];
  26. private CmsContentProvider $provider;
  27. /**
  28. * CommerceSlotSubscriber constructor.
  29. *
  30. * @param CmsContentProvider $provider
  31. */
  32. public function __construct(
  33. CmsContentProvider $provider
  34. ) {
  35. $this->provider = $provider;
  36. }
  37. /**
  38. * @return array
  39. */
  40. public static function getSubscribedEvents(): array
  41. {
  42. return [KernelEvents::VIEW => 'onKernelResponse'];
  43. }
  44. /**
  45. * @param ViewEvent $event
  46. *
  47. * @return void
  48. */
  49. public function onKernelResponse(ViewEvent $event): void
  50. {
  51. if (!$this->isCategoryController($event->getRequest()->attributes->get('_controller'))) {
  52. return;
  53. }
  54. $cms = $this->provider->getContentBySlugForCurrentPage('commerce');
  55. if ($cms !== null) {
  56. $event->setControllerResult(array_merge($event->getControllerResult(), ['slots' => $cms->content]));
  57. }
  58. }
  59. /**
  60. * Check if the event is fired after a commerce action
  61. *
  62. * @param string $actionName
  63. *
  64. * @return bool
  65. */
  66. private function isCategoryController(string $actionName): bool
  67. {
  68. foreach (self::CATEGORY_CONTROLLER_NAMES as $controllerName) {
  69. if (strpos($actionName, $controllerName) !== false) {
  70. return true;
  71. }
  72. }
  73. return false;
  74. }
  75. }