1. Go to this page and download the library: Download iak/result library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
try {
$order = $placeOrder->handle($request->cart(), $request->address());
} catch (KitchenClosedException) {
return back()->withErrors(['order' => 'The kitchen is closed right now.']);
} catch (OutOfStockException) {
return back()->withErrors(['order' => 'Some items are out of stock.']);
} catch (DeliveryUnavailableException) {
return back()->withErrors(['order' => "We don't deliver to this address yet."]);
}
return redirect()->route('orders.show', $order);
enum OrderError
{
case KitchenClosed;
case OutOfStock;
case DeliveryUnavailable;
public function message(): string
{
return match ($this) {
self::KitchenClosed => 'The kitchen is closed right now.',
self::OutOfStock => 'Some items are out of stock.',
self::DeliveryUnavailable => "We don't deliver to this address yet.",
};
}
}
use Iak\Result\Result;
class PlaceOrder
{
/** @return Result<Order, OrderError> */
public function handle(Cart $cart, Address $address): Result
{
if (! $this->kitchen->isOpen()) {
return Result::failure(OrderError::KitchenClosed);
}
if (! $cart->allItemsAvailable()) {
return Result::failure(OrderError::OutOfStock);
}
if (! $this->zones->covers($address)) {
return Result::failure(OrderError::DeliveryUnavailable);
}
return Result::success(Order::create($cart, $address));
}
}