This commit introduces new actions for adding, removing, and emptying items in the basket, along with their corresponding input classes. It also updates the BasketController to utilize these new actions, enhancing the basket management functionality.
44 lines
1.1 KiB
PHP
44 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Actions\Basket;
|
|
|
|
use App\Contracts\Action;
|
|
use App\Contracts\ActionInput;
|
|
use Domain\Services\BasketService;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
/**
|
|
* @implements \App\Contracts\Action<\App\Actions\Basket\RemoveQuantityFromBasketInput|\App\Actions\Basket\RemoveProductFromBasketInput, void>
|
|
*/
|
|
final class RemoveFromBasket implements Action
|
|
{
|
|
public function __construct(
|
|
private readonly BasketService $basketService,
|
|
) {
|
|
}
|
|
|
|
#[\Override]
|
|
public function __invoke(ActionInput $input): void
|
|
{
|
|
Assert::isInstanceOfAny(
|
|
$input,
|
|
[
|
|
RemoveQuantityFromBasketInput::class,
|
|
RemoveProductFromBasketInput::class,
|
|
]
|
|
);
|
|
|
|
if ($input instanceof RemoveQuantityFromBasketInput) {
|
|
$this->basketService->remove(
|
|
$input->productId,
|
|
$input->qty
|
|
);
|
|
} elseif ($input instanceof RemoveProductFromBasketInput) {
|
|
$this->basketService->removeLine(
|
|
$input->productId
|
|
);
|
|
}
|
|
}
|
|
}
|