Lines
100.00%
36 / 36
Methods
100.00%
8 / 8
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| add | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 3 | ||
| 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 | ||
| resize | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| deposit | 100.00% | 16 / 16 | 100.00% | 1 / 1 | 3 | ||
| withdraw | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 2 | ||
| 15 | final class HeroBank | |
| 16 | { | |
| 17 | public readonly Stock $items; | |
| 18 | ||
| 19 | public function __construct( | |
| 20 | public private(set) Quantity $slots, | |
| 21 | private readonly HeroInventory $inventory, | |
| 22 | ) { | |
| 23 | $this->items = new Stock(); | |
| 24 | } | |
| 25 | ||
| 26 | /** | |
| 27 | * @return $this | |
| 28 | * | |
| 29 | * @throws InvalidArgumentException | |
| 30 | */ | |
| 31 | public function add(Item $item, Quantity $quantity = new Quantity(1)): static | |
| 32 | { | |
| 33 | if ($item->tags->has(ItemTag::Temporary)) { | |
| 34 | throw new InvalidArgumentException("{$item->name} is temporary, so it cannot be banked."); | |
| 35 | } | |
| 36 | ||
| 37 | if ($item->tags->has(ItemTag::CannotBeBanked)) { | |
| 38 | throw new InvalidArgumentException("{$item->name} cannot be banked."); | |
| 39 | } | |
| 40 | ||
| 41 | $this->items->add($item, $quantity); | |
| 42 | ||
| 43 | return $this; | |
| 44 | } | |
| 45 | ||
| 46 | /** @return Option\Option<Item> */ | |
| 47 | public function find(ItemId $id): Option\Option | |
| 48 | { | |
| 49 | return $this->items->find($id); | |
| 50 | } | |
| 51 | ||
| 52 | public function quantity(ItemId $id): int | |
| 53 | { | |
| 54 | return $this->items->quantity($id); | |
| 55 | } | |
| 56 | ||
| 57 | /** @return non-negative-int */ | |
| 58 | public function free(): int | |
| 59 | { | |
| 60 | return max(0, $this->slots->value - count($this->items)); | |
| 61 | } | |
| 62 | ||
| 63 | public function resize(Quantity $slots): void | |
| 64 | { | |
| 65 | $this->slots = $slots; | |
| 66 | } | |
| 67 | ||
| 68 | /** | |
| 69 | * @return $this | |
| 70 | * | |
| 71 | * @throws InvalidArgumentException | |
| 72 | */ | |
| 73 | public function deposit(ItemId $id): static | |
| 74 | { | |
| 75 | $item = $this->inventory | |
| 76 | ->permanent | |
| 77 | ->find($id) | |
| 78 | ->unwrapOrElse( | |
| 79 | static fn(): never => throw new InvalidArgumentException( | |
| 80 | "Item {$id} is not kept, so it cannot be banked.", | |
| 81 | ), | |
| 82 | ); | |
| 83 | ||
| 84 | if ($this->inventory->equipment->equips($id)) { | |
| 85 | throw new InvalidArgumentException("{$item->name} is equipped, so it cannot be banked."); | |
| 86 | } | |
| 87 | ||
| 88 | if ($this->free() === 0) { | |
| 89 | throw new InvalidArgumentException("The bank has no free slot for {$item->name}."); | |
| 90 | } | |
| 91 | ||
| 92 | $quantity = new Quantity($this->inventory->quantity($id)); | |
| 93 | $this->add($item, $quantity); | |
| 94 | $this->inventory->take($id, $quantity); | |
| 95 | ||
| 96 | return $this; | |
| 97 | } | |
| 98 | ||
| 99 | /** | |
| 100 | * @return $this | |
| 101 | * | |
| 102 | * @throws InvalidArgumentException | |
| 103 | */ | |
| 104 | public function withdraw(ItemId $id): static | |
| 105 | { | |
| 106 | $item = $this->find($id)->unwrapOrElse( | |
| 107 | static fn(): never => throw new InvalidArgumentException("Item {$id} is not in the bank."), | |
| 108 | ); | |
| 109 | ||
| 110 | if ($this->inventory->full()) { | |
| 111 | throw new InvalidArgumentException("The bag has no free slot for {$item->name}."); | |
| 112 | } | |
| 113 | ||
| 114 | $quantity = new Quantity($this->quantity($id)); | |
| 115 | $this->inventory->add($item, $quantity); | |
| 116 | $this->items->take($id, $quantity); | |
| 117 | ||
| 118 | return $this; | |
| 119 | } | |
| 120 | } |