src/Integration/BookingPayment/Subscribers/ErrorBookingPaymentEventSubscriber.php line 42

Open in your IDE?
  1. <?php
  2. namespace Harmonizely\Integration\BookingPayment\Subscribers;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Harmonizely\Integration\BookingPayment\Events\ErrorBookingPaymentEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. class ErrorBookingPaymentEventSubscriber implements EventSubscriberInterface
  7. {
  8.     /**
  9.      * @var EntityManagerInterface
  10.      */
  11.     private EntityManagerInterface $entityManager;
  12.     /**
  13.      * SimplypayConfirmBookingPaymentEventSubscriber constructor.
  14.      *
  15.      * @param EntityManagerInterface $entityManager
  16.      */
  17.     public function __construct(EntityManagerInterface $entityManager)
  18.     {
  19.         $this->entityManager $entityManager;
  20.     }
  21.     /**
  22.      * Returns an array of event names this subscriber wants to listen to.
  23.      *
  24.      * @return string[]
  25.      */
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             ErrorBookingPaymentEvent::NAME => 'onErrorPayment',
  30.         ];
  31.     }
  32.     /**
  33.      * @param ErrorBookingPaymentEvent $event
  34.      */
  35.     public function onErrorPayment(ErrorBookingPaymentEvent $event): void
  36.     {
  37.         $payment $event->getBooking()->getPayment();
  38.         if ($payment) {
  39.             $event->getBooking()->cancel();
  40.             $payment->error();
  41.             $event->getBooking()->removeSingleUseLink();
  42.             $this->entityManager->persist($event->getBooking());
  43.             $this->entityManager->flush();
  44.         }
  45.     }
  46. }