<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Boldr\Shop\ShopBundle\Event\OrderStateUpdatedEvent;
use Doctrine\ORM\EntityManagerInterface;
use App\Payment\InvoicePaymentOption;
class OrderEventSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly EntityManagerInterface $entityManager
) {}
public static function getSubscribedEvents(): array
{
return [
OrderStateUpdatedEvent::class => ['onOrderStateUpdated'],
];
}
public function onOrderStateUpdated(OrderStateUpdatedEvent $event): void
{
$state = $event->getNewState();
$order = $event->getOrder();
if ($state === 'cancelled' && $order->getSelectedPaymentOption() instanceof InvoicePaymentOption)
{
// delete the invoice items
foreach ($order->getInvoiceItems() as $invoiceItem)
{
$this->entityManager->remove($invoiceItem);
}
$order->setTimeCustomerConfirmed(null);
$this->entityManager->flush();
}
}
}