vendor/boldr/shop-bundle/src/EventSubscriber/OrderEventSubscriber.php line 47

Open in your IDE?
  1. <?php
  2. namespace Boldr\Shop\ShopBundle\EventSubscriber;
  3. use Boldr\Shop\ShopBundle\Event\{ OrderStateUpdatedEventPaymentStatusUpdatedEvent };
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\Mailer\MailerInterface;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Boldr\Shop\ShopBundle\Entity\Payment;
  9. use Boldr\Shop\ShopBundle\EmailFactory;
  10. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  11. class OrderEventSubscriber implements EventSubscriberInterface
  12. {
  13.     private EntityManagerInterface $em;
  14.     private MailerInterface $mailer;
  15.     private RequestStack $requestStack;
  16.     private EmailFactory $emailFactory;
  17.     private EventDispatcherInterface $eventDispatcher;
  18.     public function __construct(
  19.         RequestStack $requestStack,
  20.         EntityManagerInterface $em,
  21.         MailerInterface $mailer,
  22.         EmailFactory $emailFactory,
  23.         EventDispatcherInterface $eventDispatcher
  24.     )
  25.     {
  26.         $this->emailFactory $emailFactory;
  27.         $this->em $em;
  28.         $this->mailer $mailer;
  29.         $this->requestStack $requestStack;
  30.         $this->eventDispatcher $eventDispatcher;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             OrderStateUpdatedEvent::class => [
  36.                 ['onOrderStateUpdated'10]
  37.             ],
  38.         ];
  39.     }
  40.     public function onOrderStateUpdated(OrderStateUpdatedEvent $event)
  41.     {
  42.         $order $event->getOrder();
  43.         if ($event->getNewState() === 'cart')
  44.         {
  45.             $payments $this->em->getRepository(Payment::class)->createQueryBuilder('p')
  46.                 ->leftJoin('p.invoice''i')
  47.                 ->leftJoin('i.items''ii')
  48.                 ->andWhere('ii.order = :order')
  49.                 ->andWhere('p.status = 'Payment::STATUS_PENDING)
  50.                 ->setParameter('order'$order)
  51.                 ->getQuery()
  52.                 ->getResult()
  53.             ;
  54.             foreach ($payments as $payment)
  55.             {
  56.                 try
  57.                 {
  58.                     $payment->setStatus(Payment::STATUS_CANCELLED);
  59.                     $this->em->flush();
  60.                     $event = new PaymentStatusUpdatedEvent($paymentPayment::STATUS_PENDINGPayment::STATUS_CANCELLED);
  61.                     $this->eventDispatcher->dispatch($event);
  62.                 }
  63.                 catch (\Exception $ex)
  64.                 {
  65.                     // ignore silently
  66.                 }
  67.             }
  68.         }
  69.         elseif ($event->getNewState() === 'confirmed')
  70.         {
  71.             $invoices = [];
  72.             foreach ($order->getInvoices() as $invoice)
  73.             {
  74.                 if (!$invoice->isDraft() && !$invoice->isProForma() && !$invoice->isSentToCustomer())
  75.                 {
  76.                     $invoices[] = $invoice;
  77.                     $invoice->setSentToCustomer(true);
  78.                 }
  79.             }
  80.             try
  81.             {
  82.                 $email $this->emailFactory->createConfirmationEmail($order$invoices);
  83.                 $this->mailer->send($email);
  84.             }
  85.             catch (\Exception $ex)
  86.             {
  87.                 // could not send, ignore silently
  88.             }
  89.             try
  90.             {
  91.                 $email $this->emailFactory->createShopOrderEmail($order);
  92.                 $this->mailer->send($email);
  93.             }
  94.             catch (\Exception $ex)
  95.             {
  96.                 // could not send, ignore silently
  97.             }
  98.             // Save sent to customer
  99.             $this->em->flush();
  100.         }
  101.     }
  102. }