/** * @param NewCheckout $command * @throws CheckoutAlreadyExistsException * @throws InvalidArgumentException * @throws CartNotFoundException * @throws InvalidUUIDFormatException */ public function handle(NewCheckout $command) { $cartId = new CartId($command->cartId()); if (!$this->carts->exists($cartId)) { throw new CartNotFoundException(); } $billingAddress = new Address($command->name(), $command->street(), $command->postCode(), $command->city(), $command->countryIso2Code()); if ($this->checkouts->existsForCart($cartId)) { throw new CheckoutAlreadyExistsException(); } $this->checkouts->add(new Checkout($cartId, $billingAddress)); }
/** * @param OrderId $id * @param Products $products * @param Carts $carts * @return Order * @throws EmptyCartException * @throws ProductNotFoundException */ public function placeOrder(OrderId $id, Products $products, Carts $carts) : Order { $cart = $carts->getById($this->cartId()); if ($cart->isEmpty()) { throw new EmptyCartException(); } $orderItems = []; foreach ($cart->items() as $item) { $product = $products->getBySku($item->sku()); $orderItems[] = OrderItem::createFromProduct($product, $item->quantity()); } return new Order($id, $orderItems, $this->billingAddress(), $this->shippingAddress()); }
/** * @param PlaceOrder $command * @throws OrderAlreadyExistsException * @throws \Dumplie\Customer\Domain\Exception\EmptyCartException */ public function handle(PlaceOrder $command) { $cartId = new CartId($command->cartId()); $orderId = new OrderId($command->orderId()); if ($this->orders->exists($orderId)) { throw OrderAlreadyExistsException::withId($orderId); } $checkout = $this->checkouts->getForCart($cartId); $order = $checkout->placeOrder($orderId, $this->products, $this->carts); $this->orders->add($order); $this->checkouts->removeForCart($cartId); $this->carts->remove($cartId); $this->eventLog->log(new CustomerPlacedOrder((string) $orderId)); }
/** * @param RemoveFromCart $command * * @throws \Exception */ public function handle(RemoveFromCart $command) { $cart = $this->carts->getById(new CartId($command->cartId())); $cart->remove(new SKU($command->sku())); }
/** * @param CreateCart $command * * @throws \Exception */ public function handle(CreateCart $command) { $this->carts->add(new Cart(new CartId($command->uuid()), $command->currency())); }
function it_throws_exception_when_cart_is_empty(Products $products, Carts $carts) { $cart = new Cart(CartId::generate(), 'EUR'); $carts->getById(Argument::type(CartId::class))->willReturn($cart); $this->shouldThrow(EmptyCartException::class)->during('placeOrder', [OrderId::generate(), $products, $carts]); }
/** * @param AddToCart $command * * @throws \Exception */ public function handle(AddToCart $command) { $product = $this->products->getBySku(new SKU($command->sku())); $cart = $this->carts->getById(new CartId($command->cartId())); $cart->add($product, $command->quantity()); }