src/Services/Transaction/Workflow/EventSubscriber/TransactionSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Services\Transaction\Workflow\EventSubscriber;
  3. use App\Entity\Transaction;
  4. use Doctrine\ORM\EntityManager;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Psr\Log\LoggerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Workflow\Event\Event;
  9. use Exception;
  10. class TransactionSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var EntityManager
  14.      */
  15.     protected $em;
  16.     /**
  17.      * @var LoggerInterface
  18.      */
  19.     private $logger;
  20.     public function __construct(
  21.         EntityManagerInterface $entityManager,
  22.         LoggerInterface $logger
  23.     ) {
  24.         $this->em $entityManager;
  25.         $this->logger $logger;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             // announce is the event that is fired AFTER all subscribers we use in the system
  31.             'workflow.completed' => 'notifyTransactionStatusChange',
  32.         ];
  33.     }
  34.     public function notifyTransactionStatusChange(Event $event)
  35.     {
  36.         $trxEntity $event->getSubject()->getEntity();
  37.         if (!$trxEntity instanceof Transaction) {
  38.             return;
  39.         }
  40.         try {
  41.             $this->em->flush($trxEntity);
  42.         } catch (Exception $e) {
  43.             $this->logger->error($e->getMessage());
  44.         }
  45.     }
  46. }