FleetCart/Modules/Coupon/Http/Controllers/CartCouponController.php

77 lines
2.0 KiB
PHP
Raw Normal View History

2023-06-11 12:14:03 +00:00
<?php
namespace Modules\Coupon\Http\Controllers;
2023-12-03 14:07:47 +00:00
use Illuminate\Http\Request;
2023-06-11 12:14:03 +00:00
use Modules\Cart\Facades\Cart;
use Illuminate\Pipeline\Pipeline;
use Modules\Coupon\Entities\Coupon;
use Modules\Coupon\Checkers\ValidCoupon;
use Modules\Coupon\Checkers\CouponExists;
use Modules\Coupon\Checkers\MaximumSpend;
use Modules\Coupon\Checkers\MinimumSpend;
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\Coupon\Checkers\ApplicableCategories;
use Modules\Coupon\Checkers\UsageLimitPerCustomer;
class CartCouponController
{
2023-12-03 14:07:47 +00:00
private array $checkers = [
2023-06-11 12:14:03 +00:00
CouponExists::class,
AlreadyApplied::class,
ValidCoupon::class,
MinimumSpend::class,
MaximumSpend::class,
ApplicableProducts::class,
ExcludedProducts::class,
ApplicableCategories::class,
ExcludedCategories::class,
UsageLimitPerCoupon::class,
UsageLimitPerCustomer::class,
];
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 Request $request
*
* @return false|string
2023-06-11 12:14:03 +00:00
*/
public function store()
{
2023-12-03 14:07:47 +00:00
$cartWithCoupon = null;
2023-06-11 12:14:03 +00:00
$coupon = Coupon::findByCode(request('coupon'));
resolve(Pipeline::class)
->send($coupon)
->through($this->checkers)
2023-12-03 14:07:47 +00:00
->then(function ($coupon) use (&$cartWithCoupon) {
2023-06-11 12:14:03 +00:00
Cart::applyCoupon($coupon);
2023-12-03 14:07:47 +00:00
$cartWithCoupon = json_encode(Cart::instance());
Cart::removeCoupon();
2023-06-11 12:14:03 +00:00
});
2023-12-03 14:07:47 +00:00
return $cartWithCoupon;
2023-06-11 12:14:03 +00:00
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
/**
* Destroy resources by given ids.
*
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(): \Modules\Cart\Cart
2023-06-11 12:14:03 +00:00
{
Cart::removeCoupon();
return Cart::instance();
}
}