Lines 100.00% 23 / 23
Methods 100.00% 11 / 11
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 5 / 5 100.00% 1 / 1 1
 add 100.00% 5 / 5 100.00% 1 / 1 4
 receive 100.00% 4 / 4 100.00% 1 / 1 2
 take 100.00% 2 / 2 100.00% 1 / 1 1
 find 100.00% 1 / 1 100.00% 1 / 1 1
 quantity 100.00% 1 / 1 100.00% 1 / 1 1
 free 100.00% 1 / 1 100.00% 1 / 1 1
 full 100.00% 1 / 1 100.00% 1 / 1 1
 resize 100.00% 1 / 1 100.00% 1 / 1 1
 fits 100.00% 1 / 1 100.00% 1 / 1 2
 holding 100.00% 1 / 1 100.00% 1 / 1 2
16final class HeroInventory
17{
18    public readonly Stock $permanent;
19
20    public readonly Stock $temporary;
21
22    public readonly HeroEquipment $equipment;
23
24    public readonly HeroCostume $costume;
25
26    public readonly HeroBank $bank;
27
28    public function __construct(
29        public private(set) Quantity $slots,
30        Quantity $bankSlots,
31        public readonly HeroWallet $wallet = new HeroWallet(),
32    ) {
33        $this->permanent = new Stock();
34        $this->temporary = new Stock();
35        $this->equipment = new HeroEquipment($this->permanent);
36        $this->costume = new HeroCostume($this->permanent);
37        $this->bank = new HeroBank($bankSlots, $this);
38    }
39
40    /**
41     * @return $this
42     *
43     * @throws InvalidArgumentException
44     */
45    public function add(Item $item, Quantity $quantity = new Quantity(1)): static
46    {
47        $temporary = $item->tags->has(ItemTag::Temporary);
48        if (($temporary ? $this->permanent : $this->temporary)->find($item->id)->isSome()) {
49            throw new InvalidArgumentException("Item {$item->id} is both kept and temporary.");
50        }
51
52        ($temporary ? $this->temporary : $this->permanent)->add($item, $quantity);
53
54        return $this;
55    }
56
57    /**
58     * @return $this
59     *
60     * @throws InvalidArgumentException
61     */
62    public function receive(Item $item, Quantity $quantity = new Quantity(1)): static
63    {
64        if ($this->find($item->id)->isNone()) {
65            return $this->add($item, $quantity);
66        }
67
68        $this->holding($item->id)->receive($item, $quantity);
69
70        return $this;
71    }
72
73    /**
74     * @return $this
75     *
76     * @throws InvalidArgumentException
77     */
78    public function take(ItemId $id, Quantity $quantity): static
79    {
80        $this->holding($id)->take($id, $quantity);
81
82        return $this;
83    }
84
85    /** @return Option\Option<Item> */
86    public function find(ItemId $id): Option\Option
87    {
88        return $this->permanent->find($id)->orElse(fn(): Option\Option => $this->temporary->find($id));
89    }
90
91    public function quantity(ItemId $id): int
92    {
93        return $this->holding($id)->quantity($id);
94    }
95
96    /** @return non-negative-int */
97    public function free(): int
98    {
99        return max(0, $this->slots->value - count($this->permanent));
100    }
101
102    public function full(): bool
103    {
104        return count($this->permanent) >= $this->slots->value;
105    }
106
107    public function resize(Quantity $slots): void
108    {
109        $this->slots = $slots;
110    }
111
112    public function fits(ItemId $id): bool
113    {
114        return $this->permanent->find($id)->isSome() || !$this->full();
115    }
116
117    private function holding(ItemId $id): Stock
118    {
119        return $this->permanent->find($id)->isSome() ? $this->permanent : $this->temporary;
120    }
121}