vendor/bluue/stocks-bundle/src/EventSubscriber/ProductsBundle/ConfigureProductMenuSubscriber.php line 74

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Léo BANNHOLTZER (contact@scaledev.fr)
  4.  * @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
  5.  * @license commercial
  6.  */
  7. declare(strict_types=1);
  8. namespace Bluue\StocksBundle\EventSubscriber\ProductsBundle;
  9. use Symfony\Component\Security\Core\Security;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Bluue\ProductsBundle\Repository\ProductRepository;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. use Bluue\ProductsBundle\Event\ConfigurationProductMenuEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class ConfigureProductMenuSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var TranslatorInterface
  19.      */
  20.     private TranslatorInterface $tr;
  21.     /**
  22.      * @var RequestStack
  23.      */
  24.     protected RequestStack $requestStack;
  25.     /**
  26.      * @var Security
  27.      */
  28.     private Security $security;
  29.     /**
  30.      * @var ProductRepository
  31.      */
  32.     private ProductRepository $productRepo;
  33.     /**
  34.      * @param RequestStack $requestStack
  35.      * @param TranslatorInterface $tr
  36.      * @param Security $security
  37.      * @param ProductRepository $productRepo
  38.      */
  39.     public function __construct(
  40.         RequestStack $requestStack,
  41.         TranslatorInterface $tr,
  42.         Security $security,
  43.         ProductRepository $productRepo
  44.     ) {
  45.         $this->requestStack $requestStack;
  46.         $this->tr $tr;
  47.         $this->security $security;
  48.         $this->productRepo $productRepo;
  49.     }
  50.     /**
  51.      * @return array
  52.      */
  53.     public static function getSubscribedEvents(): array
  54.     {
  55.         return [
  56.             ConfigurationProductMenuEvent::NAME => 'onLoad'
  57.         ];
  58.     }
  59.     /**
  60.      * @param ConfigurationProductMenuEvent $event
  61.      * @return void
  62.      */
  63.     public function onLoad(ConfigurationProductMenuEvent $event): void
  64.     {
  65.         $productId $this->requestStack->getMainRequest()->attributes->get('id');
  66.         if (!$productId || !$this->security->isGranted('ROLE__STOCKS__READ_ONLY')) {
  67.             return;
  68.         }
  69.         $product $this->productRepo->find($productId);
  70.         if ($product->getIsPack()) {
  71.             return;
  72.         }
  73.         $pages $event->getPages();
  74.         $pages[] = [
  75.             'path' => 'stocks_bundle__products_bundle__product_sheet',
  76.             'product_id_required' => true,
  77.             'href' => 'stock',
  78.             'name' => $this->tr->trans('Stocks', [], 'StocksBundle')
  79.         ];
  80.         $event->setPages($pages);
  81.     }
  82. }