vendor/bluue/statistics-bundle/src/EventSubscriber/ConfigureMenuSubscriber.php line 75

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Thomas HERISSON (contact@scaledev.fr)
  4.  * @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
  5.  * @license commercial
  6.  */
  7. declare(strict_types=1);
  8. namespace Bluue\StatisticsBundle\EventSubscriber;
  9. use App\Event\ConfigureMenuEvent;
  10. use Bluue\StatisticsBundle\Event\MenuStatisticPageEvent;
  11. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\Security\Core\Security;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class ConfigureMenuSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var TranslatorInterface
  20.      */
  21.     private TranslatorInterface $tr;
  22.     /**
  23.      * @var RequestStack
  24.      */
  25.     protected RequestStack $requestStack;
  26.     /**
  27.      * @var Security
  28.      */
  29.     private Security $security;
  30.     /**
  31.      * @var EventDispatcherInterface
  32.      */
  33.     private EventDispatcherInterface $eventDispatcher;
  34.     /**
  35.      * @param RequestStack $requestStack
  36.      * @param TranslatorInterface $tr
  37.      * @param Security $security
  38.      * @param EventDispatcherInterface $eventDispatcher
  39.      */
  40.     public function __construct(
  41.         RequestStack $requestStack,
  42.         TranslatorInterface $tr,
  43.         Security $security,
  44.         EventDispatcherInterface $eventDispatcher
  45.     ) {
  46.         $this->requestStack $requestStack;
  47.         $this->tr $tr;
  48.         $this->security $security;
  49.         $this->eventDispatcher $eventDispatcher;
  50.     }
  51.     /**
  52.      * @return array
  53.      */
  54.     public static function getSubscribedEvents(): array
  55.     {
  56.         return [
  57.             ConfigureMenuEvent::NAME => 'onLoad'
  58.         ];
  59.     }
  60.     /**
  61.      * @param ConfigureMenuEvent $event
  62.      * @return void
  63.      */
  64.     public function onLoad(ConfigureMenuEvent $event): void
  65.     {
  66.         if (!$this->security->isGranted('ROLE__STATISTICS__READ_ONLY')) {
  67.             return;
  68.         }
  69.         $menu $event->getMenu();
  70.         $currentRoute $this->requestStack->getCurrentRequest()->attributes->get('_route');
  71.         $statisticDisplayEvent = new MenuStatisticPageEvent([]);
  72.         $this->eventDispatcher->dispatch(
  73.             $statisticDisplayEvent,
  74.             MenuStatisticPageEvent::NAME
  75.         );
  76.         $firstPage current($statisticDisplayEvent->getPages());
  77.         if ($firstPage && array_key_exists('id'$firstPage)) {
  78.             $menu->addChild(
  79.                 'StatisticsMenu',
  80.                 [
  81.                     'route' => $firstPage['id'],
  82.                     'label' => $this->tr->trans('Statistics', [], 'StatisticsBundle')
  83.                 ]
  84.             )->setExtra('icon''fas fa-chart-pie');
  85.             if (str_contains($currentRoute'__statistics')) {
  86.                 $menu['StatisticsMenu']->setCurrent(true);
  87.             }
  88.         }
  89.     }
  90. }