File: D:/HostingSpaces/SBogers10/komma.pro/app/KommaApp/Shop/Cart/ShoppingCartItem.php
<?php
namespace App\KommaApp\Shop\Cart;
use App\KommaApp\Shop\Discounts\Actions\ModifyPriceAction;
use App\KommaApp\Shop\Discounts\Discount;
use App\KommaApp\Shop\Discounts\DiscountableInterface;
use App\KommaApp\Shop\Products\Product\Product;
use App\KommaApp\Shop\Products\ProductableInterface;
/**
* Represents an item that can be put in the shopping cart.
* This is simply a product coupled with an amount.
*
* Class ShoppingCartItem
* @package App\KommaApp\Shop\Cart
*/
class ShoppingCartItem implements ShoppingCartItemInterface, HasCouponCodesInterface
{
use HasCouponCodesTrait;
/** @var int $id */
private $id;
/** @var ProductableInterface $productable */
private $productable;
/** @var int $amount */
private $amount;
/** @var Discount[] An array of DiscountModel instances that represents discounts applied to this ShoppingCartItem **/
private $discounts;
/** @var string[] $couponCodes */
private $couponCodes;
/**
* ShoppingCartItem constructor.
*/
public function __construct()
{
$this->initializeId();
$this->discounts = [];
$this->couponCodes = [];
}
/**
* @return Product
*/
public function getProductable(): ProductableInterface
{
return $this->productable;
}
/**
* @param ProductableInterface $productable
* @return ShoppingCartItemInterface
*/
public function setProductable(ProductableInterface $productable): ShoppingCartItemInterface
{
$this->productable = $productable;
return $this;
}
/**
* @param bool $unique Not used for this implementation
* @param bool $includeDiscounts
* @return int
*/
public function getItemsCount(bool $unique = false, bool $includeDiscounts = false): int
{
if($includeDiscounts == false) {
return $this->amount;
} else {
return $this->amount + count($this->discounts);
}
}
/**
* @param int $amount
* @return ShoppingCartItemInterface
*/
public function setAmount(int $amount): ShoppingCartItemInterface
{
$this->amount = $amount;
return $this;
}
/**
* @return int
*/
public function getAmount(): int
{
return $this->amount;
}
/**
* Returns the total of all product prices. This value is in cents
* @param bool $includeDiscounts
* @return float
*/
public function getTotal(bool $includeDiscounts = true): float
{
$price = $this->amount * $this->productable->getPrice(); //TODO: GET PRICE
if($includeDiscounts && $this->discounts)
{
foreach($this->discounts as $discount)
{
$action = $discount->getDiscountAction();
switch (get_class($action))
{
case ModifyPriceAction::class;
$price = $action->do($price);
break;
}
}
}
return $price;
}
/**
* @return Discount[]
*/
public function getDiscounts(): array
{
return $this->discounts;
}
/**
* @param Discount $discount
*/
public function addDiscount(Discount $discount)
{
$this->discounts[] = $discount;
}
/**
* Get the shoppingCartItem's id
*
* @return int
*/
public function getId() : int
{
return $this->id;
}
/**
* Applies the discount to itself
*
* @param Discount $discount
* @return DiscountableInterface
*/
public function applyDiscount(Discount $discount): DiscountableInterface
{
$this->discounts[] = $discount;
return $this;
}
/**
* Removes all discounts
*/
public function clearDiscounts(): void
{
$this->discounts = [];
}
/**
* Assign the Shopping cart item an unique id
*/
private function initializeId()
{
if(session()->has('ShoppingCartIdCount')) {
$this->id = session('ShoppingCartIdCount');
$this->id++;
} else {
$this->id = 0;
}
session()->put('ShoppingCartIdCount', $this->id);
}
}