src/Module/Cms/Subscriber/AccountSlotSubscriber.php line 62

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