<?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\ProductsBundle\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 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__CATALOG__READ_ONLY')) {
return;
}
$menu = $event->getMenu();
$menuCatalogChilds = [
[
'key' => 'brands',
'label' => $this->tr->trans('Brands', [], 'ProductsBundle'),
'route' => 'products_bundle__brand_list',
],
[
'key' => 'attribute_groups',
'label' => $this->tr->trans('Attribute groups', [], 'ProductsBundle'),
'route' => 'products_bundle__attribute_group_list',
'matchPath' => 'bundles/products-bundle/attribute'
],
[
'key' => 'feature',
'label' => $this->tr->trans('Features', [], 'ProductsBundle'),
'route' => 'products_bundle__feature_list',
'matchPath' => 'bundles/products-bundle/feature'
],
[
'key' => 'product',
'label' => $this->tr->trans('Products', [], 'ProductsBundle'),
'route' => 'products_bundle__product_list',
'matchPath' => 'bundles/products-bundle/product'
]
];
$currentPath = $this->requestStack->getCurrentRequest()->getPathInfo();
$currentRoute = $this->requestStack->getCurrentRequest()->attributes->get('_route');
$catalogTranslate = $this->tr->trans('Catalog', [], 'ProductsBundle');
$menu->addChild('CatalogMenu')
->setLabel($catalogTranslate)
->setExtra('icon', 'fas fa-store');
foreach ($menuCatalogChilds as $menuCatalogChild) {
$current = $menuCatalogChild['route'] == $currentRoute
|| (
!empty($menuCatalogChild['matchPath'])
&& strpos($currentPath, $menuCatalogChild['matchPath']) !== false
)
;
$menu['CatalogMenu']->addChild(
$menuCatalogChild['key'],
[
'route' => $menuCatalogChild['route'],
'label' => $menuCatalogChild['label'],
'current' => $current
]
);
}
}
}