Exemplo n.º 1
0
 /**
  * @param Product $product
  * @param int     $quantity
  *
  * @throws \Dumplie\SharedKernel\Domain\Exception\InvalidCurrencyException
  * @throws \Dumplie\Customer\Domain\Exception\ProductNotAvailableException
  */
 public function add(Product $product, int $quantity)
 {
     if (!$product->isAvailable()) {
         throw new ProductNotAvailableException($product);
     }
     if (!$product->price()->hasCurrency($this->currency)) {
         throw new InvalidCurrencyException($this->currency, $product->price()->currency());
     }
     if (array_key_exists((string) $product->sku(), $this->items)) {
         $quantity += $this->items[(string) $product->sku()]->quantity();
     }
     $this->items[(string) $product->sku()] = new CartItem($product->sku(), $quantity);
 }
Exemplo n.º 2
0
 /**
  * @param Product $product
  * @param int $quantity
  * @return OrderItem
  */
 public static function createFromProduct(Product $product, int $quantity) : OrderItem
 {
     return new self($product->sku(), $product->price(), $quantity);
 }
 /**
  * ProductNotAvailableException constructor.
  *
  * @param Product $product
  */
 public function __construct(Product $product)
 {
     parent::__construct(sprintf('Product with SKU "%s" is not available.', $product->sku()));
 }