<?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\ManufacturingBundle\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 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__MANUFACTURING__READ_ONLY')) {
return;
}
$menu = $event->getMenu();
$menuManufacturingChilds = [
[
'key' => 'products',
'label' => $this->tr->trans('Products', [], 'ManufacturingBundle'),
'route' => 'manufacturing_bundle__products_list',
]
];
$currentPath = $this->requestStack->getCurrentRequest()->getPathInfo();
$currentRoute = $this->requestStack->getCurrentRequest()->attributes->get('_route');
$manufacturingTranslate = $this->tr->trans('Manufacturing', [], 'ManufacturingBundle');
$menu->addChild('ManufacturingMenu')
->setLabel($manufacturingTranslate)
->setExtra('icon', 'fas fa-hammer');
foreach ($menuManufacturingChilds as $menuManufacturingChild) {
$current = $menuManufacturingChild['route'] == $currentRoute
|| (
!empty($menuManufacturingChild['matchPath'])
&& strpos($currentPath, $menuManufacturingChild['matchPath']) !== false
)
;
$menu['ManufacturingMenu']->addChild(
$menuManufacturingChild['key'],
[
'route' => $menuManufacturingChild['route'],
'label' => $menuManufacturingChild['label'],
'current' => $current
]
);
}
}
}