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/structura.komma.pro/app/KommaApp/Shop/Cart/ShoppingCartController.php
<?php
namespace App\KommaApp\Shop\Cart;


use App\Http\Controllers\Controller;
use App\KommaApp\Pages\PageService;
use App\KommaApp\Shop\Discounts\DiscountServiceInterface;
use App\KommaApp\Shop\Products\Product\Product;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;

/**
 * Represents a shopping cart
 *
 * Class ShoppingCartService
 * @package App\KommaApp\Shop\Cart
 */
class ShoppingCartController extends Controller
{
    /**
     * @var ShoppingCartService $shoppingCartService
     */
    private $shoppingCartService;

    /**
     * @var DiscountServiceInterface $discountService
     */
    private $discountService;

    public function __construct(PageService $pageService)
    {
        $this->shoppingCartService = \App::make(ShoppingCartService::class);
        $this->discountService = \App::make(DiscountServiceInterface::class);
    }

    /**
     * Get the shopping cart index
     *
     * @return View
     */
    public function index() : View
    {
//        dd($this->shoppingCartService); //Use me to view the complete shopping cart when testing. Including discounts

        $items = $this->shoppingCartService->getItems();
        $subTotal = $this->shoppingCartService->getTotal(false);
        $itemCount = $this->shoppingCartService->getItemsCount();
        $subTotalWithDiscounts = $this->shoppingCartService->getTotal(true);

        $view = \View::make('shop.pages.shoppingCart', [
            'items' => $items,
            'subTotal' => $subTotal,
            'itemCount' => $itemCount,
            'links' => $this->links,
            'subTotalWithDiscounts' => $subTotalWithDiscounts,
            'categoryTree' => $this->categoryTree,
        ]);

        return $view;
    }

    /**
     * Add a product to the cart
     *
     * @return View
     */
    public function addProductToShoppingCart() : View
    {
        //TODO: call this with productID from some product page or index
        $this->shoppingCartService->addProductable(rand(1,10));

        return $this->cartView();
    }

    /**
     * Add a product to the cart
     *
     * @param Request $request
     * @return View
     */
    public function removeItemFromShoppingCart(Request $request) : View
    {
        if($request->has('itemID')) {
            $this->shoppingCartService->deleteItem($request->itemID);
        }

        return $this->cartView();
    }

    /**
     * Add a product to the cart
     *
     * @param Request $request
     * @return View
     */
    public function setItemAmountInShoppingCart(Request $request) : View
    {
        if($request->has('itemID')) {
            $this->shoppingCartService->setItemAmount($request->itemID, $request->amount);
        }

        return $this->cartView();
    }

    private function cartView() : View
    {
        $items = $this->shoppingCartService->getItems();
        $subTotal = $this->shoppingCartService->getTotal();
        $subTotalWithDiscounts = $this->shoppingCartService->getTotal(true);

        $view = \View::make('shop.cart.content', [
            'items' => $items,
            'subTotal' => $subTotal,
            'subTotalWithDiscounts' => $subTotalWithDiscounts,
        ]);

        return $view;
    }


}