vendor/bluue/crm-bundle/src/EventSubscriber/TicketViewSubscriber.php line 73

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Bluue\CrmBundle\EventSubscriber;
  4. use App\Services\CheckBundleInstall;
  5. use Bluue\CrmBundle\Repository\OpportunityRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use ReflectionClass;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Routing\RouterInterface;
  10. use Symfony\Contracts\Translation\TranslatorInterface;
  11. class TicketViewSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var OpportunityRepository
  15.      */
  16.     private OpportunityRepository $opportunityRepo;
  17.     /**
  18.      * @var RouterInterface
  19.      */
  20.     private RouterInterface $router;
  21.     /**
  22.      * @var EntityManagerInterface
  23.      */
  24.     private EntityManagerInterface $em;
  25.     /**
  26.      * @var TranslatorInterface
  27.      */
  28.     private TranslatorInterface $tr;
  29.     /**
  30.      * @param OpportunityRepository $opportunityRepo
  31.      * @param RouterInterface $router
  32.      * @param EntityManagerInterface $em
  33.      * @param TranslatorInterface $tr
  34.      */
  35.     public function __construct(
  36.         OpportunityRepository $opportunityRepo,
  37.         RouterInterface $router,
  38.         EntityManagerInterface $em,
  39.         TranslatorInterface $tr
  40.     ) {
  41.         $this->opportunityRepo $opportunityRepo;
  42.         $this->router $router;
  43.         $this->em $em;
  44.         $this->tr $tr;
  45.     }
  46.     /**
  47.      * @return array
  48.      */
  49.     public static function getSubscribedEvents(): array
  50.     {
  51.         if (CheckBundleInstall::exist('tickets-bundle')) {
  52.             $reflectionClass = new ReflectionClass('Bluue\TicketsBundle\Event\TicketViewEvent');
  53.             return [
  54.                 $reflectionClass->getConstant('NAME') => 'paths'
  55.             ];
  56.         }
  57.         return [];
  58.     }
  59.     /**
  60.      * @param object $event
  61.      * @return void
  62.      */
  63.     public function paths(object $event): void
  64.     {
  65.         $ticket $this->em->getRepository('TicketsBundle:Ticket')->find($event->getTicketId());
  66.         $opportunity $this->opportunityRepo->createQueryBuilder('o')
  67.             ->where("JSON_UNQUOTE(JSON_EXTRACT(o.options, '$.tickets_bundle.ticketNumber')) = :ticket")
  68.             ->setParameter('ticket'$ticket->getNumber())
  69.             ->getQuery()
  70.             ->getOneOrNullResult()
  71.         ;
  72.         if ($opportunity) {
  73.             $event->addPath([
  74.                 'linkPage' => $this->router->generate('crm_bundle__opportunity_view', ['id' => $opportunity->getId()]),
  75.                 'name' => $this->tr->trans('Opportunity:', [], 'CrmBundle') . ' ' $opportunity->getName(),
  76.             ]);
  77.         }
  78.     }
  79. }