vendor/bluue/suppliers-orders-bundle/src/EventSubscriber/ConfigureMenuSubscriber.php line 67

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\SuppliersOrdersBundle\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.     private Security $security;
  29.     /**
  30.      * ConfigureMenuSubscriber constructor.
  31.      * @param RequestStack $requestStack
  32.      * @param TranslatorInterface $tr
  33.      * @param Security $security
  34.      */
  35.     public function __construct(
  36.         RequestStack $requestStack,
  37.         TranslatorInterface $tr,
  38.         Security $security
  39.     ) {
  40.         $this->requestStack $requestStack;
  41.         $this->tr $tr;
  42.         $this->security $security;
  43.     }
  44.     /**
  45.      * @return array
  46.      */
  47.     public static function getSubscribedEvents(): array
  48.     {
  49.         return [
  50.             ConfigureMenuEvent::NAME => 'onLoad'
  51.         ];
  52.     }
  53.     /**
  54.      * @param ConfigureMenuEvent $event
  55.      * @return void
  56.      */
  57.     public function onLoad(ConfigureMenuEvent $event): void
  58.     {
  59.         if (!$this->security->isGranted('ROLE__SUPPLIERS__READ_ONLY')) {
  60.             return;
  61.         }
  62.         $menu $event->getMenu();
  63.         $currentPath $this->requestStack->getCurrentRequest()->getPathInfo();
  64.         $currentRoute $this->requestStack->getCurrentRequest()->attributes->get('_route');
  65.         $suppliersOrdersBundleChilds = [
  66.             [
  67.                 'key' => 'orders',
  68.                 'name' => $this->tr->trans('Orders', [], 'SuppliersOrdersBundle'),
  69.                 'route' => 'suppliers_orders_bundle__supplier_order_list',
  70.                 'matchPath' => 'bundles/suppliers-orders-bundle/supplier-order/'
  71.             ],
  72.             [
  73.                 'key' => 'waiting_products',
  74.                 'name' => $this->tr->trans('Waiting products', [], 'SuppliersOrdersBundle'),
  75.                 'route' => 'suppliers_orders_bundle__waiting_product_list'
  76.             ]
  77.         ];
  78.         foreach ($suppliersOrdersBundleChilds as $suppliersOrdersBundleChild) {
  79.             $current $suppliersOrdersBundleChild['route'] == $currentRoute
  80.                 || (
  81.                     !empty($suppliersOrdersBundleChild['matchPath'])
  82.                     && strpos($currentPath$suppliersOrdersBundleChild['matchPath']) !== false
  83.                 )
  84.             ;
  85.             $menu['SuppliersMenu']->addChild(
  86.                 $suppliersOrdersBundleChild['key'],
  87.                 [
  88.                     'label' => $suppliersOrdersBundleChild['name'],
  89.                     'route' => $suppliersOrdersBundleChild['route'],
  90.                     'current' => $current
  91.                 ]
  92.             );
  93.         }
  94.         if (CheckBundleInstall::exist('suppliers-bundle')) {
  95.             $matchPath 'bundles/suppliers-orders-bundle/supplier-view';
  96.             if (strpos($currentPath$matchPath) !== false) {
  97.                 $menu->getChild('SuppliersMenu')->getChild('suppliers')->setCurrent(true);
  98.             }
  99.         }
  100.     }
  101. }