<?php
namespace Boldr\Shop\MolliePaymentMethodBundle\EventSubscriber;
use Boldr\Shop\ShopBundle\Event\{ OrderStateUpdatedEvent };
use Boldr\Shop\ShopBundle\Entity\Payment;
use Boldr\Shop\MolliePaymentMethodBundle\Entity\MolliePayment;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\ORM\EntityManagerInterface;
class OrderEventSubscriber implements EventSubscriberInterface
{
private EntityManagerInterface $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public static function getSubscribedEvents(): array
{
return [
OrderStateUpdatedEvent::class => [
['onOrderStateUpdated', 10]
]
];
}
public function onOrderStateUpdated(OrderStateUpdatedEvent $event)
{
if ($event->getOldState() === 'payment' && $event->getNewState() === 'cart')
{
foreach ($event->getOrder()->getInvoices() as $invoice)
{
foreach ($invoice->getPayments() as $payment)
{
if ($payment instanceof MolliePayment && $payment->getStatus() === Payment::STATUS_FAILED)
{
$payment->setShowFailedScreen(false);
$this->em->flush();
}
}
}
}
}
}