From bf722788d45e8553960e33e22459e7dcc2a3893d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Chru=C5=9Bciel?= Date: Wed, 24 Jul 2019 11:02:55 +0200 Subject: [PATCH] [Cart] Simplify cart recalculation after customer cart assignment --- .../Cart/AssignCustomerToCartHandlerSpec.php | 27 ++++++---- src/EventListener/CartBlamerListener.php | 17 ------- .../UserCartRecalculationListener.php | 49 ------------------- .../Cart/AssignCustomerToCartHandler.php | 26 +++++++--- src/Resources/config/services.xml | 10 +--- .../config/services/handler/cart.xml | 1 + 6 files changed, 37 insertions(+), 93 deletions(-) delete mode 100644 src/EventListener/UserCartRecalculationListener.php diff --git a/spec/Handler/Cart/AssignCustomerToCartHandlerSpec.php b/spec/Handler/Cart/AssignCustomerToCartHandlerSpec.php index 8ff41b3f9..1e6d03ece 100644 --- a/spec/Handler/Cart/AssignCustomerToCartHandlerSpec.php +++ b/spec/Handler/Cart/AssignCustomerToCartHandlerSpec.php @@ -8,35 +8,42 @@ use Sylius\Component\Core\Model\CustomerInterface; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Repository\OrderRepositoryInterface; +use Sylius\Component\Order\Processor\OrderProcessorInterface; use Sylius\ShopApiPlugin\Command\Cart\AssignCustomerToCart; use Sylius\ShopApiPlugin\Provider\CustomerProviderInterface; final class AssignCustomerToCartHandlerSpec extends ObjectBehavior { - function let(OrderRepositoryInterface $orderRepository, CustomerProviderInterface $customerProvider): void - { - $this->beConstructedWith($orderRepository, $customerProvider); + function let( + OrderRepositoryInterface $cartRepository, + OrderProcessorInterface $orderProcessor, + CustomerProviderInterface $customerProvider + ): void { + $this->beConstructedWith($cartRepository, $orderProcessor, $customerProvider); } function it_handles_assigning_customer_to_cart( - CustomerInterface $customer, + OrderRepositoryInterface $cartRepository, + OrderProcessorInterface $orderProcessor, CustomerProviderInterface $customerProvider, - OrderInterface $order, - OrderRepositoryInterface $orderRepository + CustomerInterface $customer, + OrderInterface $cart ): void { - $orderRepository->findOneBy(['tokenValue' => 'ORDERTOKEN'])->willReturn($order); + $cartRepository->findOneBy(['tokenValue' => 'ORDERTOKEN'])->willReturn($cart); $customerProvider->provide('example@customer.com')->willReturn($customer); - $order->setCustomer($customer)->shouldBeCalled(); + $cart->setCustomer($customer)->shouldBeCalled(); + + $orderProcessor->process($cart); $this(new AssignCustomerToCart('ORDERTOKEN', 'example@customer.com')); } function it_throws_an_exception_if_order_does_not_exist( - OrderRepositoryInterface $orderRepository + OrderRepositoryInterface $cartRepository ): void { - $orderRepository->findOneBy(['tokenValue' => 'ORDERTOKEN'])->willReturn(null); + $cartRepository->findOneBy(['tokenValue' => 'ORDERTOKEN'])->willReturn(null); $this ->shouldThrow(\InvalidArgumentException::class) diff --git a/src/EventListener/CartBlamerListener.php b/src/EventListener/CartBlamerListener.php index 3b97999a9..9520ce632 100644 --- a/src/EventListener/CartBlamerListener.php +++ b/src/EventListener/CartBlamerListener.php @@ -4,11 +4,9 @@ namespace Sylius\ShopApiPlugin\EventListener; -use Doctrine\Common\Persistence\ObjectManager; use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent; use Sylius\Component\Core\Model\ShopUserInterface; use Sylius\Component\Core\Repository\OrderRepositoryInterface; -use Sylius\Component\Order\Processor\OrderProcessorInterface; use Sylius\ShopApiPlugin\Command\Cart\AssignCustomerToCart; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\Messenger\MessageBusInterface; @@ -16,32 +14,22 @@ final class CartBlamerListener { - /** @var ObjectManager */ - private $cartManager; - /** @var OrderRepositoryInterface */ private $cartRepository; /** @var MessageBusInterface */ private $bus; - /** @var OrderProcessorInterface */ - private $orderProcessor; - /** @var RequestStack */ private $requestStack; public function __construct( - ObjectManager $cartManager, OrderRepositoryInterface $cartRepository, MessageBusInterface $bus, - OrderProcessorInterface $orderProcessor, RequestStack $requestStack ) { - $this->cartManager = $cartManager; $this->cartRepository = $cartRepository; $this->bus = $bus; - $this->orderProcessor = $orderProcessor; $this->requestStack = $requestStack; } @@ -70,10 +58,5 @@ public function onJwtLogin(JWTCreatedEvent $interactiveLoginEvent): void $user->getCustomer()->getEmail() ) ); - - $this->orderProcessor->process($cart); - - $this->cartManager->persist($cart); - $this->cartManager->flush(); } } diff --git a/src/EventListener/UserCartRecalculationListener.php b/src/EventListener/UserCartRecalculationListener.php deleted file mode 100644 index acf8dead9..000000000 --- a/src/EventListener/UserCartRecalculationListener.php +++ /dev/null @@ -1,49 +0,0 @@ -cartContext = $cartContext; - $this->orderProcessor = $orderProcessor; - $this->cartManager = $cartManager; - } - - public function recalculateCartWhileLogin(): void - { - try { - $cart = $this->cartContext->getCart(); - } catch (CartNotFoundException $exception) { - return; - } - - Assert::isInstanceOf($cart, OrderInterface::class); - - $this->orderProcessor->process($cart); - - $this->cartManager->flush(); - } -} diff --git a/src/Handler/Cart/AssignCustomerToCartHandler.php b/src/Handler/Cart/AssignCustomerToCartHandler.php index d5ce5f6f5..0a8ab0170 100644 --- a/src/Handler/Cart/AssignCustomerToCartHandler.php +++ b/src/Handler/Cart/AssignCustomerToCartHandler.php @@ -6,6 +6,7 @@ use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Repository\OrderRepositoryInterface; +use Sylius\Component\Order\Processor\OrderProcessorInterface; use Sylius\ShopApiPlugin\Command\Cart\AssignCustomerToCart; use Sylius\ShopApiPlugin\Provider\CustomerProviderInterface; use Webmozart\Assert\Assert; @@ -13,26 +14,35 @@ final class AssignCustomerToCartHandler { /** @var OrderRepositoryInterface */ - private $orderRepository; + private $cartRepository; + + /** @var OrderProcessorInterface */ + private $orderProcessor; /** @var CustomerProviderInterface */ private $customerProvider; - public function __construct(OrderRepositoryInterface $orderRepository, CustomerProviderInterface $customerProvider) - { - $this->orderRepository = $orderRepository; + public function __construct( + OrderRepositoryInterface $cartRepository, + OrderProcessorInterface $orderProcessor, + CustomerProviderInterface $customerProvider + ) { + $this->cartRepository = $cartRepository; $this->customerProvider = $customerProvider; + $this->orderProcessor = $orderProcessor; } public function __invoke(AssignCustomerToCart $assignOrderToCustomer): void { - /** @var OrderInterface $order */ - $order = $this->orderRepository->findOneBy(['tokenValue' => $assignOrderToCustomer->orderToken()]); + /** @var OrderInterface $cart */ + $cart = $this->cartRepository->findOneBy(['tokenValue' => $assignOrderToCustomer->orderToken()]); - Assert::notNull($order, sprintf('Order with %s token has not been found.', $assignOrderToCustomer->orderToken())); + Assert::notNull($cart, sprintf('Order with %s token has not been found.', $assignOrderToCustomer->orderToken())); $customer = $this->customerProvider->provide($assignOrderToCustomer->email()); - $order->setCustomer($customer); + $cart->setCustomer($customer); + + $this->orderProcessor->process($cart); } } diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index d1bfa6d5e..4cad85691 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -77,20 +77,12 @@ id="sylius.listener.cart_blamer" class="Sylius\ShopApiPlugin\EventListener\CartBlamerListener" > - - + - - - - - - - diff --git a/src/Resources/config/services/handler/cart.xml b/src/Resources/config/services/handler/cart.xml index 51296e239..6b7392530 100644 --- a/src/Resources/config/services/handler/cart.xml +++ b/src/Resources/config/services/handler/cart.xml @@ -101,6 +101,7 @@ +