FleetCart/Modules/Cart/Http/Controllers/CartItemController.php

139 lines
4.0 KiB
PHP
Raw Normal View History

2023-06-11 12:14:03 +00:00
<?php
namespace Modules\Cart\Http\Controllers;
2023-12-03 14:07:47 +00:00
use Exception;
2023-06-11 12:14:03 +00:00
use Modules\Cart\Facades\Cart;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Routing\Controller;
2023-12-03 14:07:47 +00:00
use Modules\Coupon\Entities\Coupon;
use Modules\Coupon\Checkers\ValidCoupon;
2023-06-11 12:14:03 +00:00
use Modules\Coupon\Checkers\MaximumSpend;
use Modules\Coupon\Checkers\MinimumSpend;
2023-12-03 14:07:47 +00:00
use Modules\Coupon\Checkers\CouponExists;
use Modules\Coupon\Checkers\AlreadyApplied;
use Modules\Coupon\Checkers\ExcludedProducts;
use Modules\Coupon\Checkers\ApplicableProducts;
use Modules\Coupon\Checkers\ExcludedCategories;
use Modules\Coupon\Checkers\UsageLimitPerCoupon;
use Modules\Cart\Http\Middleware\CheckItemStock;
use Modules\Coupon\Checkers\ApplicableCategories;
use Modules\Coupon\Checkers\UsageLimitPerCustomer;
2023-06-11 12:14:03 +00:00
use Modules\Cart\Http\Requests\StoreCartItemRequest;
2023-12-03 14:07:47 +00:00
use Modules\Cart\Http\Requests\UpdateCartItemRequest;
2023-06-11 12:14:03 +00:00
class CartItemController extends Controller
{
2023-12-03 14:07:47 +00:00
private array $checkers = [
CouponExists::class,
AlreadyApplied::class,
ValidCoupon::class,
2023-06-11 12:14:03 +00:00
MinimumSpend::class,
MaximumSpend::class,
2023-12-03 14:07:47 +00:00
ApplicableProducts::class,
ExcludedProducts::class,
ApplicableCategories::class,
ExcludedCategories::class,
UsageLimitPerCoupon::class,
UsageLimitPerCustomer::class,
2023-06-11 12:14:03 +00:00
];
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
2023-12-03 14:07:47 +00:00
$this->middleware(CheckItemStock::class)->only(['store', 'update']);
2023-06-11 12:14:03 +00:00
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
/**
* Store a newly created resource in storage.
*
2023-12-03 14:07:47 +00:00
* @param StoreCartItemRequest $request
*
* @return \Modules\Cart\Cart
2023-06-11 12:14:03 +00:00
*/
public function store(StoreCartItemRequest $request)
{
2023-12-03 14:07:47 +00:00
Cart::store(
$request->product_id,
$request->variant_id,
$request->qty,
$request->options ?? []
);
2023-06-11 12:14:03 +00:00
return Cart::instance();
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
/**
* Update the specified resource in storage.
*
2023-12-03 14:07:47 +00:00
* @param UpdateCartItemRequest $request
* @param string $cartItemId
*
* @return \Modules\Cart\Cart
2023-06-11 12:14:03 +00:00
*/
2023-12-03 14:07:47 +00:00
public function update(UpdateCartItemRequest $request, string $cartItemId)
2023-06-11 12:14:03 +00:00
{
Cart::updateQuantity($cartItemId, request('qty'));
2023-12-03 14:07:47 +00:00
$cartWithCoupon = null;
$couponCode = request()->query('coupon_code');
if ($couponCode) {
$coupon = Coupon::findByCode($couponCode);
try {
resolve(Pipeline::class)
->send($coupon)
->through($this->checkers)
->then(function ($coupon) use (&$cartWithCoupon) {
Cart::applyCoupon($coupon);
$cartWithCoupon = json_encode(Cart::instance());
Cart::removeCoupon();
});
} catch (Exception) {
//Just suppressing the exception
}
2023-06-11 12:14:03 +00:00
}
2023-12-03 14:07:47 +00:00
return $cartWithCoupon ?? Cart::instance();
2023-06-11 12:14:03 +00:00
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
/**
* Remove the specified resource from storage.
*
* @param string $cartItemId
2023-12-03 14:07:47 +00:00
*
* @return \Modules\Cart\Cart
2023-06-11 12:14:03 +00:00
*/
2023-12-03 14:07:47 +00:00
public function destroy(string $cartItemId)
2023-06-11 12:14:03 +00:00
{
Cart::remove($cartItemId);
2023-12-03 14:07:47 +00:00
$cartWithCoupon = null;
$couponCode = request()->query('coupon_code');
if ($couponCode) {
$coupon = Coupon::findByCode($couponCode);
try {
resolve(Pipeline::class)
->send($coupon)
->through($this->checkers)
->then(function ($coupon) use (&$cartWithCoupon) {
Cart::applyCoupon($coupon);
$cartWithCoupon = json_encode(Cart::instance());
Cart::removeCoupon();
});
} catch (Exception) {
//Just suppressing the exception
}
}
return $cartWithCoupon ?? Cart::instance();
2023-06-11 12:14:03 +00:00
}
}