Lines 100.00% 41 / 41
Functions and Methods 100.00% 10 / 10
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
Stock 100.00% 41 / 41 100.00% 10 / 10 17 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 add 100.00% 5 / 5 100.00% 1 / 1 2
 receive 100.00% 4 / 4 100.00% 1 / 1 2
 take 100.00% 10 / 10 100.00% 1 / 1 2
 hold 100.00% 7 / 7 100.00% 1 / 1 1
 find 100.00% 1 / 1 100.00% 1 / 1 1
 quantity 100.00% 6 / 6 100.00% 1 / 1 2
 getIterator 100.00% 1 / 1 100.00% 1 / 1 1
 count 100.00% 1 / 1 100.00% 1 / 1 1
 store 100.00% 5 / 5 100.00% 1 / 1 4
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Core\Item;
6
7use Countable;
8use InvalidArgumentException;
9use Iterator;
10use IteratorAggregate;
11use Lorekeeper\Core\Item\Traits\Stackable;
12use Lorekeeper\Core\Support\Quantity;
13use Psl\Option;
14
15/**
16 * @implements IteratorAggregate<array-key, Item>
17 */
18final class Stock implements IteratorAggregate, Countable
19{
20    /** @var Inventory<Item> */
21    private readonly Inventory $items;
22
23    /** @var array<positive-int, Quantity> */
24    private array $quantities = [];
25
26    public function __construct()
27    {
28        $this->items = new Inventory([]);
29    }
30
31    /**
32     * @return $this
33     *
34     * @throws InvalidArgumentException
35     */
36    public function add(Item $item, Quantity $quantity = new Quantity(1)): static
37    {
38        if ($this->find($item->id)->isSome()) {
39            throw new InvalidArgumentException("{$item->name} is already held.");
40        }
41
42        $this->store($item, $quantity->value);
43        $this->items->add($item);
44
45        return $this;
46    }
47
48    /**
49     * @return $this
50     *
51     * @throws InvalidArgumentException
52     */
53    public function receive(Item $item, Quantity $quantity = new Quantity(1)): static
54    {
55        if ($this->find($item->id)->isNone()) {
56            return $this->add($item, $quantity);
57        }
58
59        $this->store($item, $this->quantity($item->id) + $quantity->value);
60
61        return $this;
62    }
63
64    /**
65     * @return $this
66     *
67     * @throws InvalidArgumentException
68     */
69    public function take(ItemId $id, Quantity $quantity): static
70    {
71        $item = $this->find($id)->unwrapOrElse(
72            static fn(): never => throw new InvalidArgumentException("Item {$id} is not held, so none can be taken."),
73        );
74
75        $left = $this->quantity($id) - $quantity->value;
76        if ($left <= 0) {
77            $this->items->remove($id);
78            unset($this->quantities[$id->value]);
79
80            return $this;
81        }
82
83        $this->store($item, $left);
84
85        return $this;
86    }
87
88    /**
89     * @return $this
90     *
91     * @throws InvalidArgumentException
92     */
93    public function hold(ItemId $id, Quantity $quantity): static
94    {
95        $item = $this->find($id)->unwrapOrElse(
96            static fn(): never => throw new InvalidArgumentException(
97                "Item {$id} is not held, so its amount cannot be set.",
98            ),
99        );
100
101        $this->store($item, $quantity->value);
102
103        return $this;
104    }
105
106    /** @return Option\Option<Item> */
107    public function find(ItemId $id): Option\Option
108    {
109        return $this->items->find($id);
110    }
111
112    public function quantity(ItemId $id): int
113    {
114        return $this
115            ->find($id)
116            ->map(fn(Item $item): int => $item instanceof Stackable
117                ? $this->quantities[$item->id->value]->value ?? 1
118                : 1)
119            ->unwrapOr(0);
120    }
121
122    /** @return Iterator<array-key, Item> */
123    public function getIterator(): Iterator
124    {
125        return $this->items->getIterator();
126    }
127
128    public function count(): int
129    {
130        return count($this->items);
131    }
132
133    /** @throws InvalidArgumentException */
134    private function store(Item $item, int $total): void
135    {
136        $most = $item instanceof Stackable ? $item->stack->value : 1;
137        if ($total > $most) {
138            throw new InvalidArgumentException("{$item->name} stacks up to {$most}, not {$total}.");
139        }
140
141        if ($item instanceof Stackable) {
142            $this->quantities[$item->id->value] = new Quantity($total);
143        }
144    }
145}