<?php
namespace Harmonizely\Integration\BookingPayment\Subscribers;
use Doctrine\ORM\EntityManagerInterface;
use Harmonizely\Calendar\ExternalCalendarManager;
use Harmonizely\CalendarEvents;
use Harmonizely\Integration\BookingPayment\Events\ConfirmBookingPaymentEvent;
use Harmonizely\Model\EventPayment;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
class ConfirmBookingPaymentEventSubscriber implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
private EntityManagerInterface $entityManager;
/**
* @var ExternalCalendarManager
*/
private ExternalCalendarManager $externalCalendarManager;
/**
* @var EventDispatcherInterface
*/
private EventDispatcherInterface $eventDispatcher;
/**
* SimplypayConfirmBookingPaymentEventSubscriber constructor.
*
* @param EntityManagerInterface $entityManager
* @param ExternalCalendarManager $externalCalendarManager
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(
EntityManagerInterface $entityManager,
ExternalCalendarManager $externalCalendarManager,
EventDispatcherInterface $eventDispatcher
)
{
$this->entityManager = $entityManager;
$this->externalCalendarManager = $externalCalendarManager;
$this->eventDispatcher = $eventDispatcher;
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
ConfirmBookingPaymentEvent::NAME => 'onConfirmPayment',
];
}
/**
* @param ConfirmBookingPaymentEvent $event
*/
public function onConfirmPayment(ConfirmBookingPaymentEvent $event): void
{
$booking = $event->getBooking();
$payment = $booking->getPayment();
if ($payment && $payment->getStatus() !== EventPayment::STATUS_RECEIVED) {
$booking->receivePayment();
$this->entityManager->persist($booking);
$this->entityManager->flush();
if ($payment->getSystem() !== 'delay') {
$this->externalCalendarManager->create($booking);
$this->entityManager->flush();
$this->eventDispatcher->dispatch(new GenericEvent($booking), CalendarEvents::EVENT_POST_CREATE);
}
}
}
}