<?php
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Domain\MarginCheckList\Adapters\Gateway\Doctrine\MarginCheckListEntity;
use App\Domain\MarginCheckList\Adapters\Gateway\Doctrine\Repository\DoctrineMarginCheckListRepository;
use App\Entity\Reference\ReferenceTransactionStatus;
use App\Entity\Transaction;
use App\Mailer\Transaction\TransactionMailerInterface;
use App\Message\CreateTransactionMessage;
use App\Message\UpdateTransactionMessage;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Security\Core\Security;
class TransactionSubscriber implements EventSubscriberInterface
{
private MessageBusInterface $bus;
private TransactionMailerInterface $mailer;
private Security $security;
private DoctrineMarginCheckListRepository $marginRepo;
/**
* @param MessageBusInterface $bus
* @param Security $security
* @param TransactionMailerInterface $mailer
* @param DoctrineMarginCheckListRepository $marginRepo
*/
public function __construct(
MessageBusInterface $bus,
Security $security,
TransactionMailerInterface $mailer,
DoctrineMarginCheckListRepository $marginRepo
) {
$this->bus = $bus;
$this->security = $security;
$this->mailer = $mailer;
$this->marginRepo = $marginRepo;
}
/**
* @param $event
*/
public function onPost($event): void
{
$transaction = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$transaction instanceof Transaction || Request::METHOD_POST !== $method) {
return;
}
$this->mailer->transactionCreatedNotifyManager($transaction);
}
public function postUpdate($event): void
{
$transaction = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (
!$transaction instanceof Transaction ||
!in_array($method, [Request::METHOD_POST, Request::METHOD_PUT]) ||
!in_array(
$transaction->getReferenceTransactionStatus()->getCode(),
ReferenceTransactionStatus::ALL_TRANSACTION_RESERVED_CODE_STATUS,
true
)
) {
return;
}
$marginCheckList = $this->marginRepo->findOneByTransaction($transaction);
if (
$transaction->getReferenceTransactionStatus()->getCode() ===
ReferenceTransactionStatus::REFERENCE_CODE_TRANSACTION_STATUS_UNDER_COMPROMISE_VALID
) {
if ($marginCheckList instanceof MarginCheckListEntity) {
$this->bus->dispatch(
new UpdateTransactionMessage($transaction->getId(), $this->security->getUser()->getId())
);
} else {
$this->bus->dispatch(
new CreateTransactionMessage($transaction->getId(), $this->security->getUser()->getId())
);
}
} else {
if ($marginCheckList instanceof MarginCheckListEntity) {
$this->bus->dispatch(
new UpdateTransactionMessage($transaction->getId(), $this->security->getUser()->getId())
);
} else {
$this->bus->dispatch(
new CreateTransactionMessage($transaction->getId(), $this->security->getUser()->getId())
);
}
}
}
/**
* @return array[]
*/
public static function getSubscribedEvents(): array
{
return [
KernelEvents::VIEW => [['onPost', EventPriorities::POST_WRITE], ['postUpdate', EventPriorities::POST_WRITE]]
];
}
}