/** * @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()); }