ItemState
1<?php ...
2
3namespace Thunk\Verbs\Examples\Cart\States;
4
5use Thunk\Verbs\State;
6
7class ItemState extends State
8{
9 public int $quantity = 0;
10
11 public array $holds = [];
12
13 public function available(): int
14 {
15 return $this->quantity - $this->activeHoldCount();
16 }
17
18 public function activeHolds(): array
19 {
20 return $this->holds = array_filter($this->holds, fn ($hold) => $hold['expires'] > now()->unix());
21 }
22
23 protected function activeHoldCount(): mixed
24 {
25 return collect($this->activeHolds())->sum('quantity');
26 }
27}