game-village-politics-backend/app/Actions/CancelOrder.php

24 lines
756 B
PHP

<?php
namespace App\Actions;
use App\Engine\Simulation;
use App\Game\GameRuleViolation;
use App\Models\User;
use App\Models\Village;
use Carbon\CarbonImmutable;
/** Cancel an order (and later ones on the same instance) with a full refund. */
final class CancelOrder extends VillageAction
{
public function __invoke(User $user, int $villageId, int $orderId): Village
{
return $this->mutate($user, $villageId, function (Simulation $sim, Village $village, CarbonImmutable $now) use ($orderId) {
$order = $village->orders->firstWhere('id', $orderId)
?? throw new GameRuleViolation('not_found', 'Order not found.');
$sim->cancel($order);
$sim->startWaitingOrders($now);
});
}
}