What Is Domain-Driven Design or DDD? Domain-Driven Design is a software development approach that tries to bring the business language and the source code as close as possible.
Now we can use the state classes like this: class OrderController extends Controller { public function update(UpdateOrderRequest $request, Order $order) { abort_if(!$order->status->canBeChanged(), 400); }}
This is a concrete Transition: class DraftToPendingTransition implements Transition { public function execute(Order $order): Order { if ($order->state::class !== DraftOrderStatus::class) { throw new Exception('Transition not allowed'); } $order->state_class = PendingOrderStatus::class; $order->save(); return $order; }}
The last piece of the puzzle is the Controller: class ChangeOrderStatusController extends Controller { public function __construct(private ChangeOrderStatusAction $changeOrderStatus) { } public function index(Order $order, string $state) { $nextOrderStatus = OrderStatuses::from($state)->createOrderStatus($order); return [ 'data' => new OrderResource($this->changeOrderStatus->execute($order, $nextOrderStatus))]; }}