<?php
namespace Harmonizely\Integration\BookingPayment\Subscribers;
use Doctrine\ORM\EntityManagerInterface;
use Harmonizely\Integration\BookingPayment\Events\ErrorBookingPaymentEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ErrorBookingPaymentEventSubscriber implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
private EntityManagerInterface $entityManager;
/**
* SimplypayConfirmBookingPaymentEventSubscriber constructor.
*
* @param EntityManagerInterface $entityManager
*/
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
ErrorBookingPaymentEvent::NAME => 'onErrorPayment',
];
}
/**
* @param ErrorBookingPaymentEvent $event
*/
public function onErrorPayment(ErrorBookingPaymentEvent $event): void
{
$payment = $event->getBooking()->getPayment();
if ($payment) {
$event->getBooking()->cancel();
$payment->error();
$event->getBooking()->removeSingleUseLink();
$this->entityManager->persist($event->getBooking());
$this->entityManager->flush();
}
}
}