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');
}
}