src/Sylius/Bundle/ShopBundle/EventListener/OrderTotalIntegrityChecker.php line 45

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\Persistence\ObjectManager;
  13. use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
  14. use Sylius\Component\Core\Model\OrderInterface;
  15. use Sylius\Component\Order\Processor\OrderProcessorInterface;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\Routing\RouterInterface;
  18. use Webmozart\Assert\Assert;
  19. final class OrderTotalIntegrityChecker
  20. {
  21.     /** @var OrderProcessorInterface */
  22.     private $orderProcessors;
  23.     /** @var RouterInterface */
  24.     private $router;
  25.     /** @var ObjectManager */
  26.     private $manager;
  27.     public function __construct(
  28.         OrderProcessorInterface $orderProcessors,
  29.         RouterInterface $router,
  30.         ObjectManager $manager
  31.     ) {
  32.         $this->orderProcessors $orderProcessors;
  33.         $this->router $router;
  34.         $this->manager $manager;
  35.     }
  36.     public function check(ResourceControllerEvent $event): void
  37.     {
  38.         /** @var OrderInterface $order */
  39.         $order $event->getSubject();
  40.         Assert::isInstanceOf($orderOrderInterface::class);
  41.         $oldTotal $order->getTotal();
  42.         $this->orderProcessors->process($order);
  43.         if ($order->getTotal() !== $oldTotal) {
  44.             $event->stop('sylius.order.total_integrity'ResourceControllerEvent::TYPE_ERROR);
  45.             $event->setResponse(new RedirectResponse($this->router->generate('sylius_shop_checkout_complete')));
  46.             $this->manager->persist($order);
  47.             $this->manager->flush();
  48.         }
  49.     }
  50. }