vendor/sylius/sylius/src/Sylius/Bundle/ShopBundle/EventListener/OrderIntegrityChecker.php line 47

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ShopBundle\EventListener;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Persistence\ObjectManager;
  14. use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
  15. use Sylius\Component\Core\Model\OrderInterface;
  16. use Sylius\Component\Core\Model\PromotionInterface;
  17. use Sylius\Component\Order\Processor\OrderProcessorInterface;
  18. use Symfony\Component\HttpFoundation\RedirectResponse;
  19. use Symfony\Component\Routing\RouterInterface;
  20. use Webmozart\Assert\Assert;
  21. final class OrderIntegrityChecker
  22. {
  23.     /** @var RouterInterface */
  24.     private $router;
  25.     /** @var OrderProcessorInterface */
  26.     private $orderProcessor;
  27.     /** @var ObjectManager */
  28.     private $manager;
  29.     public function __construct(
  30.         RouterInterface $router,
  31.         OrderProcessorInterface $orderProcessor,
  32.         ObjectManager $manager
  33.     ) {
  34.         $this->router $router;
  35.         $this->orderProcessor $orderProcessor;
  36.         $this->manager $manager;
  37.     }
  38.     public function check(ResourceControllerEvent $event): void
  39.     {
  40.         $order $event->getSubject();
  41.         /** @var OrderInterface $order */
  42.         Assert::isInstanceOf($orderOrderInterface::class);
  43.         /** @var ArrayCollection<array-key, PromotionInterface> $previousPromotions */
  44.         $previousPromotions = new ArrayCollection($order->getPromotions()->toArray());
  45.         $oldTotal $order->getTotal();
  46.         $this->orderProcessor->process($order);
  47.         /** @var PromotionInterface $previousPromotion */
  48.         foreach ($previousPromotions as $previousPromotion) {
  49.             if (!$order->getPromotions()->contains($previousPromotion)) {
  50.                 $event->stop(
  51.                     'sylius.order.promotion_integrity',
  52.                     ResourceControllerEvent::TYPE_ERROR,
  53.                     ['%promotionName%' => $previousPromotion->getName()]
  54.                 );
  55.                 $event->setResponse(new RedirectResponse($this->router->generate('sylius_shop_checkout_complete')));
  56.                 $this->manager->persist($order);
  57.                 $this->manager->flush();
  58.                 return;
  59.             }
  60.         }
  61.         if ($order->getTotal() !== $oldTotal) {
  62.             $event->stop('sylius.order.total_integrity'ResourceControllerEvent::TYPE_ERROR);
  63.             $event->setResponse(new RedirectResponse($this->router->generate('sylius_shop_checkout_complete')));
  64.             $this->manager->persist($order);
  65.             $this->manager->flush();
  66.             return;
  67.         }
  68.     }
  69. }