getById() public method

public getById ( CartId $cartId ) : Cart
$cartId CartId
return Cart
Ejemplo n.º 1
0
 /**
  * @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());
 }
Ejemplo n.º 2
0
 /**
  * @param RemoveFromCart $command
  *
  * @throws \Exception
  */
 public function handle(RemoveFromCart $command)
 {
     $cart = $this->carts->getById(new CartId($command->cartId()));
     $cart->remove(new SKU($command->sku()));
 }
Ejemplo n.º 3
0
 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]);
 }
Ejemplo n.º 4
0
 /**
  * @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());
 }