1<?php ...
2
3namespace Thunk\Verbs\Examples\Cart\Events;
4
5use Thunk\Verbs\Event;
6use Thunk\Verbs\Examples\Cart\States\CartState;
7use Thunk\Verbs\Examples\Cart\States\ItemState;
8
9class CheckedOut extends Event
10{
11 public CartState $cart;
12
13 public function validate()
14 {
15 $this->assert(! $this->cart->checked_out, 'Already checked out');
16
17 foreach ($this->cart->items as $item_id => $quantity) {
18 $item = ItemState::load($item_id);
19 $held = $item->activeHolds()[$this->cart->id]['quantity'] ?? 0;
20 $this->assert($held + $item->available() >= $quantity, 'Some items in your cart are out of stock');
21 }
22 }
23
24 public function apply()
25 {
26 foreach ($this->cart->items as $item_id => $quantity) {
27 $item = ItemState::load($item_id);
28 $item->quantity -= $quantity;
29 unset($item->holds[$this->cart->id]);
30 }
31
32 $this->cart->checked_out = true;
33 }
34}