<?php
namespace App\Services\Transaction\Workflow\EventSubscriber;
use App\Entity\Transaction;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\Event;
use Exception;
class TransactionSubscriber implements EventSubscriberInterface
{
/**
* @var EntityManager
*/
protected $em;
/**
* @var LoggerInterface
*/
private $logger;
public function __construct(
EntityManagerInterface $entityManager,
LoggerInterface $logger
) {
$this->em = $entityManager;
$this->logger = $logger;
}
public static function getSubscribedEvents(): array
{
return [
// announce is the event that is fired AFTER all subscribers we use in the system
'workflow.completed' => 'notifyTransactionStatusChange',
];
}
public function notifyTransactionStatusChange(Event $event)
{
$trxEntity = $event->getSubject()->getEntity();
if (!$trxEntity instanceof Transaction) {
return;
}
try {
$this->em->flush($trxEntity);
} catch (Exception $e) {
$this->logger->error($e->getMessage());
}
}
}