<?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\ShipmentsBundle\EventSubscriber;
use App\Event\ConfigureMenuEvent;
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 RequestStack
*/
private RequestStack $requestStack;
/**
* @var TranslatorInterface
*/
private TranslatorInterface $tr;
/**
* @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 array
*/
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__SHIPMENTS__READ_ONLY')) {
return;
}
$menu = $event->getMenu();
$menuShipmentsChilds = [
[
'key' => 'orders',
'label' => $this->tr->trans('Orders', [], 'ShipmentsBundle'),
'route' => 'shipments_bundle__order_list',
'matchPath' => 'bundles/shipments-bundle/order/'
],
[
'key' => 'bursts',
'label' => $this->tr->trans('Bursts', [], 'ShipmentsBundle'),
'route' => 'shipments_bundle__burst_list',
'matchPath' => 'bundles/shipments-bundle/bursts/'
],
[
'key' => 'labels',
'label' => $this->tr->trans('Labels', [], 'ShipmentsBundle'),
'route' => 'shipments_bundle__label_list',
'matchPath' => 'bundles/shipments-bundle/labels/'
]
];
$currentPath = $this->requestStack->getCurrentRequest()->getPathInfo();
$currentRoute = $this->requestStack->getCurrentRequest()->attributes->get('_route');
$parentName = $this->tr->trans('Shipments', [], 'ShipmentsBundle');
$menu->addChild('ShipmentsMenu')
->setLabel($parentName)
->setExtra('icon', 'fas fa-shipping-fast');
foreach ($menuShipmentsChilds as $menuShipmentsChild) {
$current = $menuShipmentsChild['route'] == $currentRoute
|| (
!empty($menuShipmentsChild['matchPath'])
&& strpos($currentPath, $menuShipmentsChild['matchPath']) !== false
)
;
$menu['ShipmentsMenu']->addChild(
$menuShipmentsChild['key'],
[
'route' => $menuShipmentsChild['route'],
'label' => $menuShipmentsChild['label'],
'current' => $current
]
);
}
}
}