src/EventSubscriber/TransactionSubscriber.php line 61

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Domain\MarginCheckList\Adapters\Gateway\Doctrine\MarginCheckListEntity;
  5. use App\Domain\MarginCheckList\Adapters\Gateway\Doctrine\Repository\DoctrineMarginCheckListRepository;
  6. use App\Entity\Reference\ReferenceTransactionStatus;
  7. use App\Entity\Transaction;
  8. use App\Mailer\Transaction\TransactionMailerInterface;
  9. use App\Message\CreateTransactionMessage;
  10. use App\Message\UpdateTransactionMessage;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\Messenger\MessageBusInterface;
  15. use Symfony\Component\Security\Core\Security;
  16. class TransactionSubscriber implements EventSubscriberInterface
  17. {
  18.     private MessageBusInterface $bus;
  19.     private TransactionMailerInterface $mailer;
  20.     private Security $security;
  21.     private DoctrineMarginCheckListRepository $marginRepo;
  22.     /**
  23.      * @param MessageBusInterface               $bus
  24.      * @param Security                          $security
  25.      * @param TransactionMailerInterface        $mailer
  26.      * @param DoctrineMarginCheckListRepository $marginRepo
  27.      */
  28.     public function __construct(
  29.         MessageBusInterface $bus,
  30.         Security $security,
  31.         TransactionMailerInterface $mailer,
  32.         DoctrineMarginCheckListRepository $marginRepo
  33.     ) {
  34.         $this->bus $bus;
  35.         $this->security $security;
  36.         $this->mailer $mailer;
  37.         $this->marginRepo $marginRepo;
  38.     }
  39.     /**
  40.      * @param $event
  41.      */
  42.     public function onPost($event): void
  43.     {
  44.         $transaction $event->getControllerResult();
  45.         $method $event->getRequest()->getMethod();
  46.         if (!$transaction instanceof Transaction || Request::METHOD_POST !== $method) {
  47.             return;
  48.         }
  49.         $this->mailer->transactionCreatedNotifyManager($transaction);
  50.     }
  51.     public function postUpdate($event): void
  52.     {
  53.         $transaction $event->getControllerResult();
  54.         $method $event->getRequest()->getMethod();
  55.         if (
  56.             !$transaction instanceof Transaction ||
  57.             !in_array($method, [Request::METHOD_POSTRequest::METHOD_PUT]) ||
  58.             !in_array(
  59.                 $transaction->getReferenceTransactionStatus()->getCode(),
  60.                 ReferenceTransactionStatus::ALL_TRANSACTION_RESERVED_CODE_STATUS,
  61.                 true
  62.             )
  63.         ) {
  64.             return;
  65.         }
  66.         $marginCheckList $this->marginRepo->findOneByTransaction($transaction);
  67.         if (
  68.             $transaction->getReferenceTransactionStatus()->getCode() ===
  69.             ReferenceTransactionStatus::REFERENCE_CODE_TRANSACTION_STATUS_UNDER_COMPROMISE_VALID
  70.         ) {
  71.             if ($marginCheckList instanceof MarginCheckListEntity) {
  72.                 $this->bus->dispatch(
  73.                     new UpdateTransactionMessage($transaction->getId(), $this->security->getUser()->getId())
  74.                 );
  75.             } else {
  76.                 $this->bus->dispatch(
  77.                     new CreateTransactionMessage($transaction->getId(), $this->security->getUser()->getId())
  78.                 );
  79.             }
  80.         } else {
  81.             if ($marginCheckList instanceof MarginCheckListEntity) {
  82.                 $this->bus->dispatch(
  83.                     new UpdateTransactionMessage($transaction->getId(), $this->security->getUser()->getId())
  84.                 );
  85.             } else {
  86.                 $this->bus->dispatch(
  87.                     new CreateTransactionMessage($transaction->getId(), $this->security->getUser()->getId())
  88.                 );
  89.             }
  90.         }
  91.     }
  92.     /**
  93.      * @return array[]
  94.      */
  95.     public static function getSubscribedEvents(): array
  96.     {
  97.         return [
  98.             KernelEvents::VIEW => [['onPost'EventPriorities::POST_WRITE], ['postUpdate'EventPriorities::POST_WRITE]]
  99.         ];
  100.     }
  101. }