/** * @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 RemoveFromCart $command * * @throws \Exception */ public function handle(RemoveFromCart $command) { $cart = $this->carts->getById(new CartId($command->cartId())); $cart->remove(new SKU($command->sku())); }
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()); }