Skip to content

Commit

Permalink
[Cart] Simplify cart recalculation after customer cart assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
lchrusciel committed Jul 24, 2019
1 parent f80d609 commit bf72278
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 93 deletions.
27 changes: 17 additions & 10 deletions spec/Handler/Cart/AssignCustomerToCartHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 0 additions & 17 deletions src/EventListener/CartBlamerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,32 @@

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;
use Webmozart\Assert\Assert;

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;
}

Expand Down Expand Up @@ -70,10 +58,5 @@ public function onJwtLogin(JWTCreatedEvent $interactiveLoginEvent): void
$user->getCustomer()->getEmail()
)
);

$this->orderProcessor->process($cart);

$this->cartManager->persist($cart);
$this->cartManager->flush();
}
}
49 changes: 0 additions & 49 deletions src/EventListener/UserCartRecalculationListener.php

This file was deleted.

26 changes: 18 additions & 8 deletions src/Handler/Cart/AssignCustomerToCartHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,43 @@

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;

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);
}
}
10 changes: 1 addition & 9 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,12 @@
id="sylius.listener.cart_blamer"
class="Sylius\ShopApiPlugin\EventListener\CartBlamerListener"
>
<argument type="service" id="sylius.manager.order" />
<argument type="service" id="sylius.context.cart" />
<argument type="service" id="sylius.repository.order" />
<argument type="service" id="sylius_shop_api_plugin.command_bus" />
<argument type="service" id="request_stack" />
<tag name="kernel.event_listener" event="lexik_jwt_authentication.on_jwt_created" method="onJwtLogin" />
</service>

<service id="sylius.listener.user_cart_recalculation" class="Sylius\ShopApiPlugin\EventListener\UserCartRecalculationListener">
<argument type="service" id="sylius.context.cart" />
<argument type="service" id="sylius.order_processing.order_processor" />
<argument type="service" id="sylius.manager.order" />
<tag name="kernel.event_listener" event="lexik_jwt_authentication.on_jwt_created" method="recalculateCartWhileLogin" />
</service>

<!-- Removing the create cart context from composite context (see: https://github.com/Sylius/Sylius/issues/10192) -->
<service id="sylius.context.cart.new" class="Sylius\Component\Order\Context\CartContext">
<argument type="service" id="sylius.factory.order" />
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services/handler/cart.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<service id="sylius.shop_api_plugin.handler.assign_customer_to_cart_handler"
class="Sylius\ShopApiPlugin\Handler\Cart\AssignCustomerToCartHandler">
<argument type="service" id="sylius.repository.order"/>
<argument type="service" id="sylius.order_processing.order_processor" />
<argument type="service" id="sylius.shop_api_plugin.provider.customer_provider" />
<tag name="messenger.message_handler" />
</service>
Expand Down

0 comments on commit bf72278

Please sign in to comment.