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/ShoppingCartController.php
<?php

namespace App\Komma\Shop\Cart;

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

/**
 * Represents a shopping cart
 *
 * Class ShoppingCartService
 */
class ShoppingCartController extends Controller
{
    /**
     * @var ShoppingCartService
     */
    private $shoppingCartService;

    /**
     * @var DiscountServiceInterface
     */
    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,
        ]);

        return $view;
    }

    /**
     * Add a product to the cart
     *
     * @return View
     */
    public function addProductToShoppingCart(Request $request) : String
    {
        $productable = urldecode($request->itemType)::where('id', $request->productableID)->first();
        $this->shoppingCartService->addProductable($productable, $request->amount);

        return route('shoppingcart');
    }

    /**
     * Add a product to the cart
     *
     * @param Request $request
     * @return View
     */
    public function removeItemFromShoppingCart(Request $request) : String
    {
        if ($request->has('productableID')) {
            $productable = urldecode($request->itemType)::where('id', $request->productableID)->first();
            $this->shoppingCartService->deleteItem($productable);
        }

        return route('shoppingcart');
    }

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

        return route('shoppingcart');
    }
}