vendor/bluue/projects-bundle/src/EventSubscriber/ProductsBundle/ConfigureProductMenuSubscriber.php line 79

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Leo 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\ProjectsBundle\EventSubscriber\ProductsBundle;
  9. use ReflectionClass;
  10. use App\Services\CheckBundleInstall;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Component\Security\Core\Security;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class ConfigureProductMenuSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var TranslatorInterface
  20.      */
  21.     private TranslatorInterface $tr;
  22.     /**
  23.      * @var RequestStack
  24.      */
  25.     protected RequestStack $requestStack;
  26.     /**
  27.      * @var Security
  28.      */
  29.     private Security $security;
  30.     /**
  31.      * @var EntityManagerInterface
  32.      */
  33.     private EntityManagerInterface $em;
  34.     /**
  35.      * @param RequestStack $requestStack
  36.      * @param TranslatorInterface $tr
  37.      * @param Security $security
  38.      * @param EntityManagerInterface $em
  39.      */
  40.     public function __construct(
  41.         RequestStack $requestStack,
  42.         TranslatorInterface $tr,
  43.         Security $security,
  44.         EntityManagerInterface $em
  45.     ) {
  46.         $this->requestStack $requestStack;
  47.         $this->tr $tr;
  48.         $this->security $security;
  49.         $this->em $em;
  50.     }
  51.     /**
  52.      * @return array
  53.      */
  54.     public static function getSubscribedEvents(): array
  55.     {
  56.         if (CheckBundleInstall::exist('products-bundle')) {
  57.             $reflectionClass = new ReflectionClass('Bluue\ProductsBundle\Event\ConfigurationProductMenuEvent');
  58.             return [
  59.                 $reflectionClass->getConstant('NAME') => 'onLoad'
  60.             ];
  61.         }
  62.         return [];
  63.     }
  64.     /**
  65.      * @param object $event
  66.      * @return void
  67.      */
  68.     public function onLoad(object $event)
  69.     {
  70.         $productId $this->requestStack->getMainRequest()->attributes->get('id');
  71.         if (!$productId || !$this->security->isGranted('ROLE__PROJECTS__SMALL_ADMIN')) {
  72.             return;
  73.         }
  74.         $product $this->em->getRepository('ProductsBundle:Product')->find($productId);
  75.         if ($product->getIsPack()) {
  76.             return;
  77.         }
  78.         $pages $event->getPages();
  79.         $pages[] = [
  80.             'path' => 'projects_bundle__product_task_models',
  81.             'product_id_required' => true,
  82.             'href' => 'task-models',
  83.             'name' => $this->tr->trans('Task models', [], 'ProjectsBundle')
  84.         ];
  85.         $event->setPages($pages);
  86.     }
  87. }