<?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\LabelsBundle\EventSubscriber\ProductsBundle;
use ReflectionClass;
use App\Services\CheckBundleInstall;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Translation\TranslatorInterface;
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
{
if (CheckBundleInstall::exist('products-bundle')) {
$reflectionClass = new ReflectionClass('Bluue\ProductsBundle\Event\ConfigurationProductMenuEvent');
return [
$reflectionClass->getConstant('NAME') => 'onLoad'
];
}
return [];
}
/**
* @param object $event
* @return void
*/
public function onLoad(object $event): void
{
$productId = $this->requestStack->getMainRequest()->attributes->get('id');
if (!$productId || !$this->security->isGranted('ROLE__LABELS__SMALL_ADMIN')) {
return;
}
$pages = $event->getPages();
$pages[] = [
'path' => 'labels_bundle__product_elements',
'product_id_required' => true,
'href' => 'labels',
'name' => $this->tr->trans('Labels', [], 'LabelsBundle')
];
$event->setPages($pages);
}
}