<?php
declare(strict_types=1);
namespace Bluue\CrmBundle\EventSubscriber;
use App\Services\CheckBundleInstall;
use Bluue\CrmBundle\Repository\OpportunityRepository;
use Doctrine\ORM\EntityManagerInterface;
use ReflectionClass;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class TicketViewSubscriber implements EventSubscriberInterface
{
/**
* @var OpportunityRepository
*/
private OpportunityRepository $opportunityRepo;
/**
* @var RouterInterface
*/
private RouterInterface $router;
/**
* @var EntityManagerInterface
*/
private EntityManagerInterface $em;
/**
* @var TranslatorInterface
*/
private TranslatorInterface $tr;
/**
* @param OpportunityRepository $opportunityRepo
* @param RouterInterface $router
* @param EntityManagerInterface $em
* @param TranslatorInterface $tr
*/
public function __construct(
OpportunityRepository $opportunityRepo,
RouterInterface $router,
EntityManagerInterface $em,
TranslatorInterface $tr
) {
$this->opportunityRepo = $opportunityRepo;
$this->router = $router;
$this->em = $em;
$this->tr = $tr;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
if (CheckBundleInstall::exist('tickets-bundle')) {
$reflectionClass = new ReflectionClass('Bluue\TicketsBundle\Event\TicketViewEvent');
return [
$reflectionClass->getConstant('NAME') => 'paths'
];
}
return [];
}
/**
* @param object $event
* @return void
*/
public function paths(object $event): void
{
$ticket = $this->em->getRepository('TicketsBundle:Ticket')->find($event->getTicketId());
$opportunity = $this->opportunityRepo->createQueryBuilder('o')
->where("JSON_UNQUOTE(JSON_EXTRACT(o.options, '$.tickets_bundle.ticketNumber')) = :ticket")
->setParameter('ticket', $ticket->getNumber())
->getQuery()
->getOneOrNullResult()
;
if ($opportunity) {
$event->addPath([
'linkPage' => $this->router->generate('crm_bundle__opportunity_view', ['id' => $opportunity->getId()]),
'name' => $this->tr->trans('Opportunity:', [], 'CrmBundle') . ' ' . $opportunity->getName(),
]);
}
}
}