<?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\GocardlessBundle\EventSubscriber;
use App\Event\ConfigureMenuEvent;
use App\Services\CheckBundleInstall;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ConfigureMenuSubscriber 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 => 'onLoad'
];
}
/**
* @param ConfigureMenuEvent $event
* @return void
*/
public function onLoad(ConfigureMenuEvent $event): void
{
if (!$this->security->isGranted('ROLE__GOCARDLESS__READ_ONLY')) {
return;
}
$currentPath = $this->requestStack->getCurrentRequest()->getPathInfo();
$currentRoute = $this->requestStack->getCurrentRequest()->attributes->get('_route');
$menu = $event->getMenu();
if (!$menu->getChild('AccountingMenu')) {
$menu->addChild('AccountingMenu')
->setLabel($this->tr->trans('Accounting', [], 'GocardlessBundle'))
->setExtra('icon', 'fas fa-calculator');
}
$accountingMenu = [
[
'name' => $this->tr->trans('GoCardless', [], 'GocardlessBundle'),
'route' => 'gocardless_bundle__direct_debit_list',
'extra_routes' => [
'gocardless_bundle__payout_list'
]
]
];
foreach ($accountingMenu as $accountingMenuChild) {
$current = $accountingMenuChild['route'] == $currentRoute
|| in_array($currentRoute, $accountingMenuChild['extra_routes']);
$menu['AccountingMenu']->addChild(
$accountingMenuChild['name'],
[
'route' => $accountingMenuChild['route'],
'current' => $current
]
);
}
$matchPath = [
'bundles/gocardless-bundle/customer'
];
foreach ($matchPath as $match) {
if (strpos($currentPath, $match) !== false) {
$menu->getChild('CustomersMenu')->getChild('customers')->setCurrent(true);
}
}
}
}