<?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\CategoriesBundle\EventSubscriber;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Translation\TranslatorInterface;
use Bluue\ProductsBundle\Event\ConfigurationProductMenuEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ConfigureProductMenuSubscriber implements EventSubscriberInterface
{
/**
* @var TranslatorInterface
*/
private TranslatorInterface $tr;
/**
* @var Security
*/
private Security $security;
/**
* @var RequestStack
*/
private RequestStack $requestStack;
/**
* @param TranslatorInterface $tr
* @param Security $security
* @param RequestStack $requestStack
*/
public function __construct(
TranslatorInterface $tr,
Security $security,
RequestStack $requestStack
) {
$this->tr = $tr;
$this->security = $security;
$this->requestStack = $requestStack;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
ConfigurationProductMenuEvent::NAME => 'onLoad'
];
}
/**
* @param ConfigurationProductMenuEvent $event
* @return void
*/
public function onLoad(ConfigurationProductMenuEvent $event): void
{
$productId = $this->requestStack->getMainRequest()->attributes->get('id');
if (!$productId || !$this->security->isGranted('ROLE__CATALOG__SMALL_ADMIN')) {
return;
}
$pages = $event->getPages();
$pages[] = [
'path' => 'categories_bundle__category_product_list',
'product_id_required' => true,
'href' => 'categories',
'name' => $this->tr->trans('Categories', [], 'CategoriesBundle')
];
$event->setPages($pages);
}
}