src/EventSubscriber/OrderEventSubscriber.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Boldr\Shop\ShopBundle\Event\OrderStateUpdatedEvent;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use App\Payment\InvoicePaymentOption;
  7. class OrderEventSubscriber implements EventSubscriberInterface
  8. {
  9.     public function __construct(
  10.         private readonly EntityManagerInterface $entityManager
  11.     ) {}
  12.     public static function getSubscribedEvents(): array
  13.     {
  14.         return [
  15.             OrderStateUpdatedEvent::class => ['onOrderStateUpdated'],
  16.         ];
  17.     }
  18.     public function onOrderStateUpdated(OrderStateUpdatedEvent $event): void
  19.     {
  20.         $state $event->getNewState();
  21.         $order $event->getOrder();
  22.         if ($state === 'cancelled' && $order->getSelectedPaymentOption() instanceof InvoicePaymentOption)
  23.         {
  24.             // delete the invoice items
  25.             foreach ($order->getInvoiceItems() as $invoiceItem)
  26.             {
  27.                 $this->entityManager->remove($invoiceItem);
  28.             }
  29.             $order->setTimeCustomerConfirmed(null);
  30.             $this->entityManager->flush();
  31.         }
  32.     }
  33. }