vendor/bluue/gocardless-bundle/src/EventSubscriber/ConfigureMenuSubscriber.php line 65

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\GocardlessBundle\EventSubscriber;
  9. use App\Event\ConfigureMenuEvent;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\Security\Core\Security;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class ConfigureMenuSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var TranslatorInterface
  18.      */
  19.     private TranslatorInterface $tr;
  20.     /**
  21.      * @var RequestStack
  22.      */
  23.     protected RequestStack $requestStack;
  24.     /**
  25.      * @var Security
  26.      */
  27.     private Security $security;
  28.     /**
  29.      * @param RequestStack $requestStack
  30.      * @param TranslatorInterface $tr
  31.      * @param Security $security
  32.      */
  33.     public function __construct(
  34.         RequestStack $requestStack,
  35.         TranslatorInterface $tr,
  36.         Security $security
  37.     ) {
  38.         $this->requestStack $requestStack;
  39.         $this->tr $tr;
  40.         $this->security $security;
  41.     }
  42.     /**
  43.      * @return string[]
  44.      */
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             ConfigureMenuEvent::NAME => 'onLoad'
  49.         ];
  50.     }
  51.     /**
  52.      * @param ConfigureMenuEvent $event
  53.      * @return void
  54.      */
  55.     public function onLoad(ConfigureMenuEvent $event): void
  56.     {
  57.         if (!$this->security->isGranted('ROLE__GOCARDLESS__READ_ONLY')) {
  58.             return;
  59.         }
  60.         $currentPath $this->requestStack->getCurrentRequest()->getPathInfo();
  61.         $currentRoute $this->requestStack->getCurrentRequest()->attributes->get('_route');
  62.         $menu $event->getMenu();
  63.         if (!$menu->getChild('AccountingMenu')) {
  64.             $menu->addChild('AccountingMenu')
  65.                 ->setLabel($this->tr->trans('Accounting', [], 'GocardlessBundle'))
  66.                 ->setExtra('icon''fas fa-calculator');
  67.         }
  68.         $accountingMenu = [
  69.             [
  70.                 'name' => $this->tr->trans('GoCardless', [], 'GocardlessBundle'),
  71.                 'route' => 'gocardless_bundle__direct_debit_list',
  72.                 'extra_routes' => [
  73.                     'gocardless_bundle__payout_list'
  74.                 ]
  75.             ]
  76.         ];
  77.         foreach ($accountingMenu as $accountingMenuChild) {
  78.             $current $accountingMenuChild['route'] == $currentRoute
  79.                 || in_array($currentRoute$accountingMenuChild['extra_routes']);
  80.             $menu['AccountingMenu']->addChild(
  81.                 $accountingMenuChild['name'],
  82.                 [
  83.                     'route' => $accountingMenuChild['route'],
  84.                     'current' => $current
  85.                 ]
  86.             );
  87.         }
  88.         $matchPath = [
  89.             'bundles/gocardless-bundle/customer'
  90.         ];
  91.         foreach ($matchPath as $match) {
  92.             if (strpos($currentPath$match) !== false) {
  93.                 $menu->getChild('CustomersMenu')->getChild('customers')->setCurrent(true);
  94.             }
  95.         }
  96.     }
  97. }