src/Integration/BookingPayment/Subscribers/CancelBookingPaymentEventSubscriber.php line 57

Open in your IDE?
  1. <?php
  2. namespace Harmonizely\Integration\BookingPayment\Subscribers;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Harmonizely\CalendarEvents;
  5. use Harmonizely\Integration\BookingPayment\Events\CancelBookingPaymentEvent;
  6. use Harmonizely\Model\Cancellation;
  7. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\EventDispatcher\GenericEvent;
  10. use Harmonizely\Model\EventPayment;
  11. class CancelBookingPaymentEventSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var EntityManagerInterface
  15.      */
  16.     private EntityManagerInterface $entityManager;
  17.     /**
  18.      * @var EventDispatcherInterface
  19.      */
  20.     private EventDispatcherInterface $eventDispatcher;
  21.     /**
  22.      * SimplypayConfirmBookingPaymentEventSubscriber constructor.
  23.      *
  24.      * @param EntityManagerInterface $entityManager
  25.      * @param EventDispatcherInterface $eventDispatcher
  26.      */
  27.     public function __construct(
  28.         EntityManagerInterface $entityManager,
  29.         EventDispatcherInterface $eventDispatcher
  30.     )
  31.     {
  32.         $this->entityManager $entityManager;
  33.         $this->eventDispatcher $eventDispatcher;
  34.     }
  35.     /**
  36.      * Returns an array of event names this subscriber wants to listen to.
  37.      *
  38.      * @return string[]
  39.      */
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         return [
  43.             CancelBookingPaymentEvent::NAME => 'onCancelPayment',
  44.         ];
  45.     }
  46.     /**
  47.      * @param CancelBookingPaymentEvent $event
  48.      */
  49.     public function onCancelPayment(CancelBookingPaymentEvent $event): void
  50.     {
  51.         $booking $event->getBooking();
  52.         $payment $booking->getPayment();
  53.         if ($payment) {
  54.             $oldPaymentStatus $payment->getStatus();
  55.             if ($oldPaymentStatus === EventPayment::STATUS_RECEIVED) {
  56.                 $this->eventDispatcher->dispatch(new GenericEvent($booking), CalendarEvents::EVENT_PRE_CANCEL);
  57.                 $booking->removeSingleUseLink();
  58.             }
  59.             $cancellation = new Cancellation();
  60.             $cancellation->setReason('Cancelled from SimplyPay');
  61.             $cancellation->setCanceler($booking->getUser()->getFullName());
  62.             $booking->setCancellation($cancellation);
  63.             $booking->cancel();
  64.             $payment->cancel();
  65.             $this->entityManager->persist($booking);
  66.             $this->entityManager->flush();
  67.             if ($oldPaymentStatus === EventPayment::STATUS_RECEIVED) {
  68.                 $this->eventDispatcher->dispatch(new GenericEvent($booking), CalendarEvents::EVENT_POST_CANCEL);
  69.             }
  70.         }
  71.     }
  72. }