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/SBogers10/shop.komma.nl/app/Cart/ShoppingCartItem.php
<?php
namespace App\Cart;

use App\Discounts\DiscountableInterface;
use App\Discounts\DiscountableTrait;
use App\Products\ProductableInterface;
use App\Vat\HasFinancialPropertiesInterface;
use App\Vat\Models\FinancialProperties;
use App\Vat\FinancialProperties as FinancialPropertiesTait;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;

/**
 * Represents an item that can be put in the shopping cart.
 * This is simply a product coupled with an amount.
 *
 * Class ShoppingCartItem
 * @package App\Cart
 */
class ShoppingCartItem implements HasCouponCodesInterface, Arrayable, HasFinancialPropertiesInterface, DiscountableInterface
{
    use HasCouponCodesTrait;
    use FinancialPropertiesTait;
    use DiscountableTrait;

    private ?int $id;
    private ?ProductableInterface $productable;
    private ?int $quantity;
    private ?int $vatTotal;

    /**
     * ShoppingCartItem constructor.
     */
    public function __construct()
    {
        $this->discountPriceMutations = collect();
        $this->discounts = collect();
        $this->initializeId();
        $this->couponCodes = [];
    }

    /**
     * @return ProductableInterface
     */
    public function getProductable(): ProductableInterface
    {
        return $this->productable;
    }

    /**
     * @param ProductableInterface $productable
     * @return ShoppingCartItem
     */
    public function setProductable(ProductableInterface $productable): ShoppingCartItem
    {
        $this->productable = $productable;
        return $this;
    }

    /**
     * @param bool $includeDiscounts
     * @return int
     */
    public function getItemsCount($includeDiscounts = true): int
    {
        $quantity = $this->quantity;
        if($includeDiscounts) $quantity += $this->discountQuantityModification;
        return $quantity;
    }


    /**
     * @param int $quantity
     * @return ShoppingCartItem
     */
    public function setQuantity(int $quantity): ShoppingCartItem
    {
        $this->quantity = $quantity;
        return $this;
    }

    /**
     * @return int
     */
    public function getQuantity(): int
    {
        return $this->quantity;
    }

    /**
     * @param float $price_inc
     */
    public function setPriceInc(float $price_inc): void
    {
        $this->price_inc = $price_inc;
    }

    /**
     * @return float
     */
    public function getPriceInc(): float
    {
        return $this->price_inc ?? 0;
    }

    /**
     * @param float $price_ex
     */
    public function setPriceEx(float $price_ex): void
    {
        $this->price_ex = $price_ex;
    }

    /**
     * @return float
     */
    public function getPriceEx(): float
    {
        return $this->price_ex ?? 0;
    }

    /**
     * @inheritDoc
     */
    public function getPrice(): float
    {
        $priceModification = $this->discountPriceMutations->reduce(function($total, FinancialProperties $priceModification) {
            return $total + $priceModification->getPrice();
        });
        return ($this->getProductable()->getPrice() * $this->quantity) + $priceModification;
    }

    /**
     * @return float
     */
    public function getVatAmount(): float
    {
        return $this->vat_amount ?? 0;
    }

    /**
     * @param float $vat_amount
     */
    public function setVatAmount(float $vat_amount): void
    {
        $this->vat_amount = $vat_amount;
    }

    /**
     * Get the shoppingCartItem's id
     *
     * @return int
     */
    public function getId() : int
    {
        return $this->id;
    }

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

    public function toArray()
    {
        return [
            'id' => $this->getId(),
            'quantity' => $this->getQuantity(),
            'productable' => $this->getProductable()->toArray(),
        ];
    }

    public function __get($key)
    {
        if(!property_exists($this, $key))
        return $this->productable->$key;
    }
}