vendor/bluue/ovh-sms-bundle/src/EventSubscriber/ConfigurationBundleSubscriber.php line 112

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\OvhSmsBundle\EventSubscriber;
  9. use App\Event\BundleRolesEvent;
  10. use App\Event\ConfigureMenuEvent;
  11. use App\Event\MenuConfigurationPageEvent;
  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.             BundleRolesEvent::NAME => 'roles'
  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__OVH_SMS__READ_ONLY')) {
  62.             return;
  63.         }
  64.         $currentRoute $this->requestStack->getCurrentRequest()->attributes->get('_route');
  65.         $menu $event->getMenu();
  66.         if (!$menu->getChild('MarketingMenu')) {
  67.             $menu->addChild('MarketingMenu')
  68.                 ->setLabel($this->tr->trans('Marketing', [], 'OvhSmsBundle'))
  69.                 ->setExtra('icon''fas fa-bullhorn');
  70.         }
  71.         $ovhSmsMenu = [
  72.             [
  73.                 'name' => $this->tr->trans('SMS sending', [], 'OvhSmsBundle'),
  74.                 'route' => 'ovh_sms_bundle__sms_list',
  75.                 'extra_routes' => [
  76.                     'ovh_sms_bundle__mailing_list_list'
  77.                 ]
  78.             ]
  79.         ];
  80.         foreach ($ovhSmsMenu as $ovhSmsMenuChild) {
  81.             $current $ovhSmsMenuChild['route'] == $currentRoute
  82.                 || in_array($currentRoute$ovhSmsMenuChild['extra_routes']);
  83.             $menu['MarketingMenu']->addChild(
  84.                 $ovhSmsMenuChild['name'],
  85.                 [
  86.                     'route' => $ovhSmsMenuChild['route'],
  87.                     'current' => $current
  88.                 ]
  89.             );
  90.         }
  91.     }
  92.     /**
  93.      * @param MenuConfigurationPageEvent $event
  94.      * @return void
  95.      */
  96.     public function configurationMenu(MenuConfigurationPageEvent $event): void
  97.     {
  98.         if ($this->security->isGranted('ROLE__OVH_SMS__ADMIN')) {
  99.             $pages $event->getPages();
  100.             $pages[] = [
  101.                 'id' => 'ovh_sms_bundle__configuration_settings',
  102.                 'name' => $this->tr->trans('SMS sending Module by OVH', [], 'OvhSmsBundle'),
  103.                 'config' => [],
  104.                 'subpages' => [
  105.                     ['id' => 'ovh_sms_bundle__configuration_settings']
  106.                 ]
  107.             ];
  108.             $event->setPages($pages);
  109.         }
  110.     }
  111.     /**
  112.      * @param BundleRolesEvent $event
  113.      * @return void
  114.      */
  115.     public function roles(BundleRolesEvent $event): void
  116.     {
  117.         $bundles $event->getBundles();
  118.         $bundles[] = [
  119.             'select_name' => 'ovh_sms',
  120.             'name' => $this->tr->trans('SMS sending by OVH', [], 'OvhSmsBundle')
  121.         ];
  122.         $event->setBundles($bundles);
  123.     }
  124. }