vendor/bluue/inventories-bundle/src/EventSubscriber/ConfigureMenuSubscriber.php line 61

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Léo BANNHOLTZER (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\InventoriesBundle\EventSubscriber;
  9. use App\Event\ConfigureMenuEvent;
  10. use App\Services\CheckBundleInstall;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\Security\Core\Security;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class ConfigureMenuSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var TranslatorInterface
  19.      */
  20.     private TranslatorInterface $tr;
  21.     /**
  22.      * @var RequestStack
  23.      */
  24.     protected RequestStack $requestStack;
  25.     /**
  26.      * @var Security
  27.      */
  28.     protected Security $security;
  29.     /**
  30.      * @param RequestStack $requestStack
  31.      * @param TranslatorInterface $tr
  32.      * @param Security $security
  33.      */
  34.     public function __construct(
  35.         RequestStack $requestStack,
  36.         TranslatorInterface $tr,
  37.         Security $security
  38.     ) {
  39.         $this->requestStack $requestStack;
  40.         $this->tr $tr;
  41.         $this->security $security;
  42.     }
  43.     /**
  44.      * @return array
  45.      */
  46.     public static function getSubscribedEvents(): array
  47.     {
  48.         return [
  49.             ConfigureMenuEvent::NAME => 'onLoad'
  50.         ];
  51.     }
  52.     public function onLoad(ConfigureMenuEvent $event)
  53.     {
  54.         if (
  55.             !$this->security->isGranted('ROLE__INVENTORIES__READ_ONLY')
  56.             || !$this->security->isGranted('ROLE__STOCKS__READ_ONLY')
  57.         ) {
  58.             return;
  59.         }
  60.         $menu $event->getMenu();
  61.         $path $this->requestStack->getCurrentRequest()->getPathInfo();
  62.         $currentRoute $this->requestStack->getCurrentRequest()->attributes->get('_route');
  63.         $inventoriesMenuChilds = [
  64.             [
  65.                 'key' => 'inventories',
  66.                 'name' => $this->tr->trans('Inventories', [], 'InventoriesBundle'),
  67.                 'route' => 'inventories_bundle__inventory_list',
  68.                 'matchPath' => [
  69.                     'bundles/inventories-bundle/inventory'
  70.                 ]
  71.             ]
  72.         ];
  73.         foreach ($inventoriesMenuChilds as $inventoriesMenuChild) {
  74.             $current false;
  75.             if ($inventoriesMenuChild['route'] == $currentRoute) {
  76.                 $current true;
  77.             } else {
  78.                 foreach ($inventoriesMenuChild['matchPath'] as $matchPath) {
  79.                     if (strpos($path$matchPath) !== false) {
  80.                         $current true;
  81.                     }
  82.                 }
  83.             }
  84.             $menu['StocksMenu']->addChild(
  85.                 $inventoriesMenuChild['key'],
  86.                 [
  87.                     'label' => $inventoriesMenuChild['name'],
  88.                     'route' => $inventoriesMenuChild['route'],
  89.                     'current' => $current
  90.                 ]
  91.             );
  92.         }
  93.     }
  94. }