<?php
namespace App\EventSubscriber;
use App\Entity\Event;
use App\Event\QuotationCommentEvent;
use App\Service\RabbitMQ\RabbitMQDispatcher;
use App\Service\Subscription\SubscriptionService;
use App\Service\UserLog;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Security;
class QuotationCommentEventSubscriber implements EventSubscriberInterface
{
private $security;
private $em;
private $subscriptionService;
private $userLog;
public function __construct(
Security $security,
EntityManagerInterface $em,
SubscriptionService $subscriptionService,
UserLog $userLog
)
{
$this->userLog = $userLog;
$this->security = $security;
$this->subscriptionService = $subscriptionService;
$this->em = $em;
}
public static function getSubscribedEvents()
{
return [
QuotationCommentEvent::NEW => [
['addQuotationLastUpdateTime'],
['quotationCommentNewNotify'],
['logAction']
]
];
}
public function addQuotationLastUpdateTime(QuotationCommentEvent $event)
{
$event->getQuotation()->setLastUpdate(new \DateTime());
$this->em->persist($event->getQuotation());
$this->em->flush();
}
public function quotationCommentNewNotify(QuotationCommentEvent $event)
{
$this->subscriptionService->proceedEvent(
Event::QUOTATION_NEW_COMMENT,
$event->getQuotation(),
[
'data' => [
'uuid' => $event->getQuotation()->getUuid(),
'user_id' => $this->security->getUser()? $this->security->getUser()->getId():null,
'comment' => $event->getComment(),
'commented_at' => time(),
],
'action_name' => 'quotationCommentNewNotify',
]
);
}
public function logAction(QuotationCommentEvent $event)
{
$this->userLog->log(
'quotation',
'new.comment',
$event->getQuotation()->getId(),
'New quotation comment ' . $event->getQuotation()->getUuid()
);
}
}