Lines 100.00% 16 / 16
Methods 100.00% 7 / 7
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 earn 100.00% 1 / 1 100.00% 1 / 1 1
 pay 100.00% 6 / 6 100.00% 1 / 1 2
 affords 100.00% 1 / 1 100.00% 1 / 1 1
 recount 100.00% 2 / 2 100.00% 1 / 1 1
 held 100.00% 1 / 1 100.00% 1 / 1 2
 hold 100.00% 4 / 4 100.00% 1 / 1 2
13final class HeroWallet
14{
15    public function __construct(
16        public private(set) Gold $gold = new Gold(0),
17        public private(set) AdventureCoins $coins = new AdventureCoins(0),
18    ) {}
19
20    public function earn(Currency $amount): void
21    {
22        $this->hold($amount, $this->held($amount) + $amount->value);
23    }
24
25    /** @throws InvalidArgumentException */
26    public function pay(Currency $amount): void
27    {
28        $held = $this->held($amount);
29        if ($amount->value > $held) {
30            throw new InvalidArgumentException(
31                "Cannot pay {$amount}{$amount->symbol()} holding only {$held}{$amount->symbol()}.",
32            );
33        }
34
35        $this->hold($amount, $held - $amount->value);
36    }
37
38    public function affords(ItemPrice $price): bool
39    {
40        return $this->held($price->amount) >= $price->amount->value;
41    }
42
43    public function recount(Gold $gold, AdventureCoins $coins): void
44    {
45        $this->gold = $gold;
46        $this->coins = $coins;
47    }
48
49    private function held(Currency $currency): int
50    {
51        return $currency instanceof Gold ? $this->gold->value : $this->coins->value;
52    }
53
54    private function hold(Currency $currency, int $total): void
55    {
56        if ($currency instanceof Gold) {
57            $this->gold = new Gold($total);
58
59            return;
60        }
61
62        $this->coins = new AdventureCoins($total);
63    }
64}