FleetCart/Modules/Cart/Http/Controllers/CartItemController.php
2023-06-11 13:14:03 +01:00

80 lines
2.0 KiB
PHP

<?php
namespace Modules\Cart\Http\Controllers;
use Modules\Cart\Facades\Cart;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Routing\Controller;
use Modules\Coupon\Checkers\MaximumSpend;
use Modules\Coupon\Checkers\MinimumSpend;
use Modules\Cart\Http\Requests\StoreCartItemRequest;
use Modules\Coupon\Exceptions\MaximumSpendException;
use Modules\Coupon\Exceptions\MinimumSpendException;
use Modules\Cart\Http\Middleware\CheckProductIsInStock;
class CartItemController extends Controller
{
private $checkers = [
MinimumSpend::class,
MaximumSpend::class,
];
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware(CheckProductIsInStock::class)->only(['store', 'update']);
}
/**
* Store a newly created resource in storage.
*
* @param \Modules\Cart\Http\Requests\StoreCartItemRequest $request
* @return \Illuminate\Http\Response
*/
public function store(StoreCartItemRequest $request)
{
Cart::store($request->product_id, $request->qty, $request->options ?? []);
return Cart::instance();
}
/**
* Update the specified resource in storage.
*
* @param mixed $cartItemId
* @return \Illuminate\Http\Response
*/
public function update($cartItemId)
{
Cart::updateQuantity($cartItemId, request('qty'));
try {
resolve(Pipeline::class)
->send(Cart::coupon())
->through($this->checkers)
->thenReturn();
} catch (MinimumSpendException | MaximumSpendException $e) {
Cart::removeCoupon();
}
return Cart::instance();
}
/**
* Remove the specified resource from storage.
*
* @param string $cartItemId
* @return \Illuminhtate\Http\Response
*/
public function destroy($cartItemId)
{
Cart::remove($cartItemId);
return Cart::instance();
}
}