Verbs

ItemRemovedFromCart

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 ItemRemovedFromCart extends Event
10{
11 public CartState $cart;
12 
13 public ItemState $item;
14 
15 public int $quantity;
16 
17 public function validate()
18 {
19 $this->assert(! $this->cart->checked_out, 'Already checked out');
20 
21 $this->assert(
22 $this->cart->count($this->item->id) >= $this->quantity,
23 "There aren't {$this->quantity} items in the cart to remove."
24 );
25 }
26 
27 public function apply()
28 {
29 $this->cart->items[$this->item->id] -= $this->quantity;
30 
31 if (isset($this->item->holds[$this->cart->id])) {
32 $this->item->holds[$this->cart->id]['quantity'] -= $this->quantity;
33 }
34 }
35}