<?php
/**
* @author Leo BANNHOLTZER (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace Bluue\ProjectsBundle\EventSubscriber\ProductsBundle;
use ReflectionClass;
use App\Services\CheckBundleInstall;
use Doctrine\ORM\EntityManagerInterface;
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 RequestStack
*/
protected RequestStack $requestStack;
/**
* @var Security
*/
private Security $security;
/**
* @var EntityManagerInterface
*/
private EntityManagerInterface $em;
/**
* @param RequestStack $requestStack
* @param TranslatorInterface $tr
* @param Security $security
* @param EntityManagerInterface $em
*/
public function __construct(
RequestStack $requestStack,
TranslatorInterface $tr,
Security $security,
EntityManagerInterface $em
) {
$this->requestStack = $requestStack;
$this->tr = $tr;
$this->security = $security;
$this->em = $em;
}
/**
* @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)
{
$productId = $this->requestStack->getMainRequest()->attributes->get('id');
if (!$productId || !$this->security->isGranted('ROLE__PROJECTS__SMALL_ADMIN')) {
return;
}
$product = $this->em->getRepository('ProductsBundle:Product')->find($productId);
if ($product->getIsPack()) {
return;
}
$pages = $event->getPages();
$pages[] = [
'path' => 'projects_bundle__product_task_models',
'product_id_required' => true,
'href' => 'task-models',
'name' => $this->tr->trans('Task models', [], 'ProjectsBundle')
];
$event->setPages($pages);
}
}