<?php
/**
* @author Thomas HERISSON (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace Bluue\OvhSmsBundle\EventSubscriber;
use App\Event\BundleRolesEvent;
use App\Event\ConfigureMenuEvent;
use App\Event\MenuConfigurationPageEvent;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ConfigurationBundleSubscriber implements EventSubscriberInterface
{
/**
* @var TranslatorInterface
*/
private TranslatorInterface $tr;
/**
* @var RequestStack
*/
protected RequestStack $requestStack;
/**
* @var Security
*/
private Security $security;
/**
* @param RequestStack $requestStack
* @param TranslatorInterface $tr
* @param Security $security
*/
public function __construct(
RequestStack $requestStack,
TranslatorInterface $tr,
Security $security
) {
$this->requestStack = $requestStack;
$this->tr = $tr;
$this->security = $security;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
ConfigureMenuEvent::NAME => 'mainMenu',
MenuConfigurationPageEvent::NAME => 'configurationMenu',
BundleRolesEvent::NAME => 'roles'
];
}
/**
* @param ConfigureMenuEvent $event
* @return void
*/
public function mainMenu(ConfigureMenuEvent $event): void
{
if (!$this->security->isGranted('ROLE__OVH_SMS__READ_ONLY')) {
return;
}
$currentRoute = $this->requestStack->getCurrentRequest()->attributes->get('_route');
$menu = $event->getMenu();
if (!$menu->getChild('MarketingMenu')) {
$menu->addChild('MarketingMenu')
->setLabel($this->tr->trans('Marketing', [], 'OvhSmsBundle'))
->setExtra('icon', 'fas fa-bullhorn');
}
$ovhSmsMenu = [
[
'name' => $this->tr->trans('SMS sending', [], 'OvhSmsBundle'),
'route' => 'ovh_sms_bundle__sms_list',
'extra_routes' => [
'ovh_sms_bundle__mailing_list_list'
]
]
];
foreach ($ovhSmsMenu as $ovhSmsMenuChild) {
$current = $ovhSmsMenuChild['route'] == $currentRoute
|| in_array($currentRoute, $ovhSmsMenuChild['extra_routes']);
$menu['MarketingMenu']->addChild(
$ovhSmsMenuChild['name'],
[
'route' => $ovhSmsMenuChild['route'],
'current' => $current
]
);
}
}
/**
* @param MenuConfigurationPageEvent $event
* @return void
*/
public function configurationMenu(MenuConfigurationPageEvent $event): void
{
if ($this->security->isGranted('ROLE__OVH_SMS__ADMIN')) {
$pages = $event->getPages();
$pages[] = [
'id' => 'ovh_sms_bundle__configuration_settings',
'name' => $this->tr->trans('SMS sending Module by OVH', [], 'OvhSmsBundle'),
'config' => [],
'subpages' => [
['id' => 'ovh_sms_bundle__configuration_settings']
]
];
$event->setPages($pages);
}
}
/**
* @param BundleRolesEvent $event
* @return void
*/
public function roles(BundleRolesEvent $event): void
{
$bundles = $event->getBundles();
$bundles[] = [
'select_name' => 'ovh_sms',
'name' => $this->tr->trans('SMS sending by OVH', [], 'OvhSmsBundle')
];
$event->setBundles($bundles);
}
}