vendor/bluue/recurring-invoices-bundle/src/EventSubscriber/ConfigurationBundleSubscriber.php line 69

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\RecurringInvoicesBundle\EventSubscriber;
  9. use App\Event\ConfigureMenuEvent;
  10. use App\Event\MenuConfigurationPageEvent;
  11. use Bluue\SalesBundle\Event\MenuSalesConfigurationEvent;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class ConfigurationBundleSubscriber 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.      * @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 string[]
  46.      */
  47.     public static function getSubscribedEvents(): array
  48.     {
  49.         return [
  50.             ConfigureMenuEvent::NAME => 'mainMenu',
  51.             MenuConfigurationPageEvent::NAME => 'configurationMenu',
  52.             MenuSalesConfigurationEvent::NAME => 'configurationSalesMenu'
  53.         ];
  54.     }
  55.     /**
  56.      * @param ConfigureMenuEvent $event
  57.      * @return void
  58.      */
  59.     public function mainMenu(ConfigureMenuEvent $event): void
  60.     {
  61.         if (!$this->security->isGranted('ROLE__SALES__READ_ONLY')) {
  62.             return;
  63.         }
  64.         $path $this->requestStack->getCurrentRequest()->getPathInfo();
  65.         $currentRoute $this->requestStack->getCurrentRequest()->attributes->get('_route');
  66.         $menu $event->getMenu();
  67.         $recurringInvoicesMenu = [
  68.             [
  69.                 'key' => 'recurring_invoices',
  70.                 'name' => $this->tr->trans('Recurring invoices', [], 'RecurringInvoicesBundle'),
  71.                 'route' => 'recurring_invoices_bundle__recurring_invoice_list',
  72.                 'matchPath' => 'bundles/recurring-invoices-bundle/recurring-invoice/'
  73.             ]
  74.         ];
  75.         foreach ($recurringInvoicesMenu as $recurringInvoicesMenuChild) {
  76.             $current $recurringInvoicesMenuChild['route'] == $currentRoute
  77.                 || strpos($path$recurringInvoicesMenuChild['matchPath']) !== false;
  78.             $menu['SalesMenu']->addChild(
  79.                 $recurringInvoicesMenuChild['key'],
  80.                 [
  81.                     'label' => $recurringInvoicesMenuChild['name'],
  82.                     'route' => $recurringInvoicesMenuChild['route'],
  83.                     'current' => $current
  84.                 ]
  85.             );
  86.         }
  87.         $matchPath = [
  88.             'bundles/recurring-invoices-bundle/recurring-invoice/customer'
  89.         ];
  90.         foreach ($matchPath as $match) {
  91.             if (strpos($path$match) !== false) {
  92.                 $menu->getChild('CustomersMenu')->getChild('customers')->setCurrent(true);
  93.                 $menu->getChild('SalesMenu')->getChild('recurring_invoices')->setCurrent(false);
  94.             }
  95.         }
  96.     }
  97.     /**
  98.      * @param MenuSalesConfigurationEvent $event
  99.      * @return void
  100.      */
  101.     public function configurationSalesMenu(MenuSalesConfigurationEvent $event)
  102.     {
  103.         $pages $event->getPages();
  104.         $pages[] = [
  105.             'id' => 'recurring_invoices_bundle__configuration_settings',
  106.             'name' => $this->tr->trans('Recurring invoices', [], 'RecurringInvoicesBundle')
  107.         ];
  108.         $event->setPages($pages);
  109.     }
  110.     /**
  111.      * @param MenuConfigurationPageEvent $event
  112.      * @return void
  113.      */
  114.     public function configurationMenu(MenuConfigurationPageEvent $event): void
  115.     {
  116.         $pages $event->getPages();
  117.         $salesMenu array_search('sales_bundle__configuration_settings'array_column($pages'id'));
  118.         if (is_numeric($salesMenu)) {
  119.             $pages[$salesMenu]['subpages'][] = ['id' => 'recurring_invoices_bundle__configuration_settings'];
  120.             $event->setPages($pages);
  121.         }
  122.     }
  123. }