HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers95/rentman.io/app/Komma/Shop/Cart/ShoppingCartItem.php
<?php

namespace App\Komma\Shop\Cart;

use App\Komma\Shop\Discounts\Actions\ModifyPriceAction;
use App\Komma\Shop\Discounts\Discount;
use App\Komma\Shop\Discounts\DiscountableInterface;
use App\Komma\Shop\Products\Product\Product;
use App\Komma\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
 */
class ShoppingCartItem implements ShoppingCartItemInterface, HasCouponCodesInterface
{
    use HasCouponCodesTrait;

    /** @var int */
    private $id;

    /** @var ProductableInterface */
    private $productable;

    /** @var int */
    private $amount;

    /** @var Discount[] An array of DiscountModel instances that represents discounts applied to this ShoppingCartItem * */
    private $discounts;

    /** @var string[] */
    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);
    }
}