src/Sylius/Bundle/AdminApiBundle/EventListener/CartChangeListener.php line 48

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\AdminApiBundle\EventListener;
  12. use Doctrine\Common\Persistence\ObjectManager;
  13. use Sylius\Component\Core\Model\OrderItemInterface;
  14. use Sylius\Component\Order\Model\OrderInterface;
  15. use Sylius\Component\Order\Processor\OrderProcessorInterface;
  16. use Symfony\Component\EventDispatcher\GenericEvent;
  17. use Webmozart\Assert\Assert;
  18. final class CartChangeListener
  19. {
  20.     /** @var OrderProcessorInterface */
  21.     private $orderProcessor;
  22.     /** @var ObjectManager */
  23.     private $objectManager;
  24.     public function __construct(OrderProcessorInterface $orderProcessorObjectManager $objectManager)
  25.     {
  26.         $this->orderProcessor $orderProcessor;
  27.         $this->objectManager $objectManager;
  28.     }
  29.     public function recalculateOrderOnAdd(GenericEvent $event): void
  30.     {
  31.         $item $event->getSubject();
  32.         Assert::isInstanceOf($itemOrderItemInterface::class);
  33.         $order $item->getOrder();
  34.         $this->orderProcessor->process($order);
  35.         $this->objectManager->persist($order);
  36.     }
  37.     public function recalculateOrderOnDelete(GenericEvent $event): void
  38.     {
  39.         $item $event->getSubject();
  40.         Assert::isInstanceOf($itemOrderItemInterface::class);
  41.         /** @var OrderInterface $order */
  42.         $order $item->getOrder();
  43.         $order->removeItem($item);
  44.         $this->orderProcessor->process($order);
  45.     }
  46. }