70 lines
2 KiB
PHP
70 lines
2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Requests\Fo\Orders;
|
|
|
|
use App\Actions\Orders\InitializeOrderTunnelInput;
|
|
use Domain\Exceptions\OrderTunnel\OrderLimitExceededException;
|
|
use Domain\Services\BasketService;
|
|
use Domain\Services\OrderTunnelService;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Infrastructure\Adapters\Services\AppCustomerAuthService;
|
|
|
|
class ValidateSummaryRequest extends FormRequest
|
|
{
|
|
public function __construct(
|
|
private readonly AppCustomerAuthService $authService,
|
|
private readonly BasketService $basketService,
|
|
private readonly OrderTunnelService $orderTunnelService
|
|
) {
|
|
}
|
|
|
|
public function toInitializeOrderTunnelInput(): InitializeOrderTunnelInput
|
|
{
|
|
$customer = $this->authService->getCustomerAsMandatory();
|
|
$basket = $this->basketService->getBasket();
|
|
|
|
return new InitializeOrderTunnelInput(
|
|
basket: $basket,
|
|
shopperId: $customer->getId(),
|
|
);
|
|
}
|
|
|
|
public function authorize(): bool
|
|
{
|
|
return $this->authService->check();
|
|
}
|
|
|
|
#[\Override]
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$this->merge([
|
|
'basketTotal' => $this->basketService
|
|
->getBasket()
|
|
->totalTaxIncluded()
|
|
->getAmount(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'basketTotal' => [
|
|
function (string $_, mixed $__, \Closure $fail) {
|
|
try {
|
|
$basket = $this->basketService->getBasket();
|
|
$this->orderTunnelService->assertBasketAmountWithinLimit($basket);
|
|
} catch (OrderLimitExceededException) {
|
|
$fail(trans('You have reached the maximum order limit for this basket.'));
|
|
}
|
|
},
|
|
],
|
|
];
|
|
}
|
|
}
|