2026-06-14 21:32:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Requests\Basket;
|
|
|
|
|
|
2026-11-15 19:44:35 +01:00
|
|
|
use App\Contracts\Executor;
|
2026-11-10 14:28:00 +01:00
|
|
|
use App\Cqs\Actions\Basket\AddToBasketInput;
|
2026-11-14 16:06:21 +01:00
|
|
|
use App\Cqs\Queries\Products\GetProductQueryInput;
|
|
|
|
|
use App\Cqs\Queries\Products\GetProductWithPricingQuery;
|
2026-06-14 21:32:07 +02:00
|
|
|
use Domain\Models\ValueObjects\Entities\ProductId;
|
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
|
use Illuminate\Support\Facades\Gate;
|
2026-11-17 12:15:34 +01:00
|
|
|
use Shared\Helpers\UuidHelper;
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
class AddBasketRequest extends FormRequest
|
|
|
|
|
{
|
2026-11-15 19:44:35 +01:00
|
|
|
public function __construct(
|
|
|
|
|
private readonly Executor $executor,
|
|
|
|
|
private readonly GetProductWithPricingQuery $getProductWithPricingQuery,
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
public function authorize(): bool
|
|
|
|
|
{
|
2026-11-14 16:06:21 +01:00
|
|
|
$rowId = $this->input('rowId');
|
|
|
|
|
|
2026-11-17 12:15:34 +01:00
|
|
|
if (! is_string($rowId) || ! UuidHelper::isValid($rowId)) {
|
2026-11-14 16:06:21 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-15 19:44:35 +01:00
|
|
|
$product = $this->executor->execute(
|
|
|
|
|
$this->getProductWithPricingQuery,
|
|
|
|
|
new GetProductQueryInput($this->getProductId()),
|
2026-11-14 16:06:21 +01:00
|
|
|
);
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
return $product !== null && Gate::allows('addToBasket', $product);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-30 10:24:52 +02:00
|
|
|
public function toAddToBasketInput(): AddToBasketInput
|
2026-06-14 21:32:07 +02:00
|
|
|
{
|
2026-09-30 10:24:52 +02:00
|
|
|
return new AddToBasketInput(
|
|
|
|
|
$this->getProductId(),
|
|
|
|
|
$this->integer('qty')
|
|
|
|
|
);
|
2026-06-14 21:32:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getProductId(): ProductId
|
|
|
|
|
{
|
|
|
|
|
return new ProductId(
|
2026-11-17 12:15:34 +01:00
|
|
|
UuidHelper::fromString(
|
2026-06-14 21:32:07 +02:00
|
|
|
$this->string('rowId')->value()
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-09-30 10:24:52 +02:00
|
|
|
|
|
|
|
|
/** @return array<string,mixed> */
|
|
|
|
|
public function rules(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'rowId' => ['required', 'string', 'uuid', 'exists:products,id'],
|
|
|
|
|
'qty' => ['required', 'integer', 'min:1'],
|
|
|
|
|
];
|
|
|
|
|
}
|
2026-06-14 21:32:07 +02:00
|
|
|
}
|