ldc-cocotte/app/Http/Requests/Basket/AddBasketRequest.php

65 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Requests\Basket;
use App\Contracts\Executor;
use App\Cqs\Actions\Basket\AddToBasketInput;
use App\Cqs\Queries\Products\GetProductQueryInput;
use App\Cqs\Queries\Products\GetProductWithPricingQuery;
use Domain\Models\ValueObjects\Entities\ProductId;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
use Shared\Helpers\UuidHelper;
class AddBasketRequest extends FormRequest
{
public function __construct(
private readonly Executor $executor,
private readonly GetProductWithPricingQuery $getProductWithPricingQuery,
) {
}
public function authorize(): bool
{
$rowId = $this->input('rowId');
if (! is_string($rowId) || ! UuidHelper::isValid($rowId)) {
return false;
}
$product = $this->executor->execute(
$this->getProductWithPricingQuery,
new GetProductQueryInput($this->getProductId()),
);
return $product !== null && Gate::allows('addToBasket', $product);
}
public function toAddToBasketInput(): AddToBasketInput
{
return new AddToBasketInput(
$this->getProductId(),
$this->integer('qty')
);
}
public function getProductId(): ProductId
{
return new ProductId(
UuidHelper::fromString(
$this->string('rowId')->value()
)
);
}
/** @return array<string,mixed> */
public function rules(): array
{
return [
'rowId' => ['required', 'string', 'uuid', 'exists:products,id'],
'qty' => ['required', 'integer', 'min:1'],
];
}
}