100 lines
3.7 KiB
PHP
100 lines
3.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Cqs\Actions\Orders;
|
|
|
|
use App\Attributes\Locked;
|
|
use App\Contracts\Action;
|
|
use App\Contracts\ActionInput;
|
|
use App\Contracts\HandlesLockTimeout;
|
|
use App\Contracts\ProvidesLockKey;
|
|
use Domain\Exceptions\OrderTunnel\BasketPriceChangedException;
|
|
use Domain\Exceptions\OrderTunnel\ExpiredPickupTimeSlotException;
|
|
use Domain\Exceptions\OrderTunnel\InactiveShopperException;
|
|
use Domain\Exceptions\OrderTunnel\OrderLimitExceededException;
|
|
use Domain\Exceptions\OrderTunnel\UnorderableProductException;
|
|
use Domain\Exceptions\OrderTunnel\WeeklyLimitExceededException;
|
|
use Domain\Ports\Repositories\BasketRepository;
|
|
use Domain\Services\OrderTunnelService;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
/**
|
|
* @implements \App\Contracts\Action<
|
|
* \App\Cqs\Actions\Orders\PlacePendingOrderInput,
|
|
* \App\Cqs\Actions\Orders\PlacePendingOrderOutput
|
|
* >
|
|
* @implements \App\Contracts\ProvidesLockKey<\App\Cqs\Actions\Orders\PlacePendingOrderInput>
|
|
* @implements \App\Contracts\HandlesLockTimeout<
|
|
* \App\Cqs\Actions\Orders\PlacePendingOrderInput,
|
|
* \App\Cqs\Actions\Orders\PlacePendingOrderOutput
|
|
* >
|
|
*/
|
|
#[Locked(ttlSeconds: 30, waitSeconds: 10)]
|
|
final class PlacePendingOrder implements Action, HandlesLockTimeout, ProvidesLockKey
|
|
{
|
|
public function __construct(
|
|
private readonly OrderTunnelService $orderTunnelService,
|
|
private readonly BasketRepository $basketRepository,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @param \App\Cqs\Actions\Orders\PlacePendingOrderInput $input
|
|
*/
|
|
#[\Override]
|
|
public function lockKey(ActionInput $input): string
|
|
{
|
|
Assert::isInstanceOf($input, PlacePendingOrderInput::class);
|
|
|
|
$shopperId = $input->shopperId ?? $this->orderTunnelService->getBuilderCompletedOrFail()->getShopperId();
|
|
|
|
return "order:finalize:shopper:{$shopperId->value}";
|
|
}
|
|
|
|
/**
|
|
* @param \App\Cqs\Actions\Orders\PlacePendingOrderInput $input
|
|
*/
|
|
#[\Override]
|
|
public function onLockTimeout(ActionInput $input): PlacePendingOrderOutput
|
|
{
|
|
Assert::isInstanceOf($input, PlacePendingOrderInput::class);
|
|
|
|
return PlacePendingOrderOutput::lockTimeout();
|
|
}
|
|
|
|
/**
|
|
* @param \App\Cqs\Actions\Orders\PlacePendingOrderInput $input
|
|
*/
|
|
#[\Override]
|
|
public function __invoke(ActionInput $input): PlacePendingOrderOutput
|
|
{
|
|
Assert::isInstanceOf($input, PlacePendingOrderInput::class);
|
|
|
|
$builder = $this->orderTunnelService->getBuilderCompletedOrFail();
|
|
|
|
try {
|
|
$order = $this->orderTunnelService->placePendingOrder($builder);
|
|
} catch (BasketPriceChangedException $e) {
|
|
$this->basketRepository->persist($e->refreshedBasket);
|
|
|
|
return PlacePendingOrderOutput::basketPriceChanged($e->getMessage());
|
|
} catch (
|
|
ExpiredPickupTimeSlotException
|
|
| InactiveShopperException
|
|
| UnorderableProductException
|
|
| OrderLimitExceededException
|
|
| WeeklyLimitExceededException $e
|
|
) {
|
|
return match (true) {
|
|
$e instanceof ExpiredPickupTimeSlotException => PlacePendingOrderOutput::expiredTimeSlot($e->getMessage()),
|
|
$e instanceof InactiveShopperException => PlacePendingOrderOutput::inactiveShopper($e->getMessage()),
|
|
$e instanceof UnorderableProductException => PlacePendingOrderOutput::unorderableProduct($e->getMessage()),
|
|
$e instanceof OrderLimitExceededException => PlacePendingOrderOutput::orderLimitExceeded($e->getMessage()),
|
|
$e instanceof WeeklyLimitExceededException => PlacePendingOrderOutput::weeklyLimitExceeded($e->getMessage()),
|
|
};
|
|
}
|
|
|
|
return PlacePendingOrderOutput::pendingCreated($order->id(), $order->reference);
|
|
}
|
|
}
|