<?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\BusinessIndication;
use App\Entity\Reference\ReferenceTransactionStatus;
use App\Message\CreateBusinessIndicationMessage;
use App\Message\UpdateBusinessIndicationMessage;
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 TransactionBusinessIndicationSubscriber implements EventSubscriberInterface
{
private MessageBusInterface $bus;
private Security $security;
private DoctrineMarginCheckListRepository $marginRepo;
/**
* @param MessageBusInterface $bus
* @param Security $security
* @param DoctrineMarginCheckListRepository $marginRepo
*/
public function __construct(
MessageBusInterface $bus,
Security $security,
DoctrineMarginCheckListRepository $marginRepo
) {
$this->bus = $bus;
$this->security = $security;
$this->marginRepo = $marginRepo;
}
public function postUpdate($event): void
{
$indication = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (
!$indication instanceof BusinessIndication ||
Request::METHOD_PUT !== $method ||
$indication->getReferenceTransactionStatus()->getCode() ===
ReferenceTransactionStatus::REFERENCE_CODE_TRANSACTION_STATUS_REFUSED
) {
return;
}
$marginCheckList = $this->marginRepo->findOneByTransaction($indication);
if (
in_array(
$indication->getReferenceTransactionStatus()->getCode(),
ReferenceTransactionStatus::ALL_TRANSACTION_RESERVED_CODE_STATUS,
true
)
) {
if ($marginCheckList instanceof MarginCheckListEntity) {
$this->bus->dispatch(
new UpdateBusinessIndicationMessage($indication->getId(), $this->security->getUser()->getId())
);
} else {
$this->bus->dispatch(
new CreateBusinessIndicationMessage($indication->getId(), $this->security->getUser()->getId())
);
}
}
}
/**
* @return array[]
*/
public static function getSubscribedEvents(): array
{
return [
KernelEvents::VIEW => [['postUpdate', EventPriorities::POST_WRITE]]
];
}
}