<?php
/**
* @author Léo 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\SuppliersBundle\EventSubscriber\ProductsBundle;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\RequestStack;
use Bluue\ProductsBundle\Repository\ProductRepository;
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 RequestStack
*/
protected RequestStack $requestStack;
/**
* @var Security
*/
private Security $security;
/**
* @var ProductRepository
*/
private ProductRepository $productRepo;
/**
* @param RequestStack $requestStack
* @param TranslatorInterface $tr
* @param Security $security
* @param ProductRepository $productRepo
*/
public function __construct(
RequestStack $requestStack,
TranslatorInterface $tr,
Security $security,
ProductRepository $productRepo
) {
$this->requestStack = $requestStack;
$this->tr = $tr;
$this->security = $security;
$this->productRepo = $productRepo;
}
/**
* @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__SUPPLIERS__READ_ONLY')) {
return;
}
$product = $this->productRepo->find($productId);
if ($product->getIsPack()) {
return;
}
$pages = $event->getPages();
$pages[] = [
'path' => 'suppliers_bundle__products_bundle__product_sheet',
'product_id_required' => true,
'href' => 'supplier',
'name' => $this->tr->trans('Suppliers', [], 'SuppliersBundle')
];
$event->setPages($pages);
}
}