Slide 11
Slide 11 text
妥当性チェックと例外を投げることに集中する
function createOrder(int $productId, int $quantity): Order {
if ($quantity <= 0) {
throw new LogicException('Quantity must be greater than 0');
}
if (!productExists($productId)) {
throw new NotFoundException($productId);
}
$stock = getStock($productId);
if ($stock < $quantity) {
throw new InsufficientStockException($productId, $quantity, $stock);
}
// DBへの保存に発生する例外はcatchしない
return Order::create(['productId' => $productId, 'quantity' => $quantity]);
}
11/63