<?php
declare(strict_types=1);
namespace App\EventSubscriber;
use App\Entity\Reserve;
use App\Entity\Transaction;
use App\Event\ReserveCreateEvent;
use App\Service\TransactionService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ReserveEventSubscriber implements EventSubscriberInterface
{
private EntityManagerInterface $entityManager;
private TransactionService $transactionService;
public function __construct(EntityManagerInterface $entityManager, TransactionService $transactionService)
{
$this->entityManager = $entityManager;
$this->transactionService = $transactionService;
}
public static function getSubscribedEvents()
{
return [
ReserveCreateEvent::NAME => 'createEvent',
];
}
public function createEvent(ReserveCreateEvent $event): void
{
$reserve = $event->getReserve();
$transactionRepository = $this->entityManager->getRepository(Transaction::class);
$entity = $transactionRepository->find($reserve->getId());
$this->transactionService->createReserveIncomeTransaction($event->getReserve());
$this->entityManager->flush();
}
}