src/Integration/BookingPayment/Subscribers/ConfirmBookingPaymentEventSubscriber.php line 65

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