src/EventSubscriber/TransactionBusinessIndicationSubscriber.php line 41

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\BusinessIndication;
  7. use App\Entity\Reference\ReferenceTransactionStatus;
  8. use App\Message\CreateBusinessIndicationMessage;
  9. use App\Message\UpdateBusinessIndicationMessage;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. use Symfony\Component\Messenger\MessageBusInterface;
  14. use Symfony\Component\Security\Core\Security;
  15. class TransactionBusinessIndicationSubscriber implements EventSubscriberInterface
  16. {
  17.     private MessageBusInterface $bus;
  18.     private Security $security;
  19.     private DoctrineMarginCheckListRepository $marginRepo;
  20.     /**
  21.      * @param MessageBusInterface               $bus
  22.      * @param Security                          $security
  23.      * @param DoctrineMarginCheckListRepository $marginRepo
  24.      */
  25.     public function __construct(
  26.         MessageBusInterface $bus,
  27.         Security $security,
  28.         DoctrineMarginCheckListRepository $marginRepo
  29.     ) {
  30.         $this->bus $bus;
  31.         $this->security $security;
  32.         $this->marginRepo $marginRepo;
  33.     }
  34.     public function postUpdate($event): void
  35.     {
  36.         $indication $event->getControllerResult();
  37.         $method $event->getRequest()->getMethod();
  38.         if (
  39.             !$indication instanceof BusinessIndication ||
  40.             Request::METHOD_PUT !== $method ||
  41.             $indication->getReferenceTransactionStatus()->getCode() ===
  42.             ReferenceTransactionStatus::REFERENCE_CODE_TRANSACTION_STATUS_REFUSED
  43.         ) {
  44.             return;
  45.         }
  46.         $marginCheckList $this->marginRepo->findOneByTransaction($indication);
  47.         if (
  48.             in_array(
  49.                 $indication->getReferenceTransactionStatus()->getCode(),
  50.                 ReferenceTransactionStatus::ALL_TRANSACTION_RESERVED_CODE_STATUS,
  51.                 true
  52.             )
  53.         ) {
  54.             if ($marginCheckList instanceof MarginCheckListEntity) {
  55.                 $this->bus->dispatch(
  56.                     new UpdateBusinessIndicationMessage($indication->getId(), $this->security->getUser()->getId())
  57.                 );
  58.             } else {
  59.                 $this->bus->dispatch(
  60.                     new CreateBusinessIndicationMessage($indication->getId(), $this->security->getUser()->getId())
  61.                 );
  62.             }
  63.         }
  64.     }
  65.     /**
  66.      * @return array[]
  67.      */
  68.     public static function getSubscribedEvents(): array
  69.     {
  70.         return [
  71.             KernelEvents::VIEW => [['postUpdate'EventPriorities::POST_WRITE]]
  72.         ];
  73.     }
  74. }