Wallet   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 34
ccs 17
cts 17
cp 1
rs 10
c 2
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A debit() 0 5 1
A credit() 0 5 1
A isEmpty() 0 5 2
A getBalance() 0 5 1
A hasBalance() 0 3 1
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/money-interop project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Money\ValueObject;
10
11
use Daikon\ValueObject\ValueObjectMap;
12
13
/**
14
 * @type(Daikon\Money\ValueObject\Money)
15
 */
16
class Wallet extends ValueObjectMap
17
{
18 1
    public function isEmpty(): bool
19
    {
20 1
        return $this->reduce(
21 1
            fn(bool $carry, string $currency, MoneyInterface $money): bool => $carry && $money->isZero(),
22 1
            true
23
        );
24
    }
25
26 4
    public function getBalance(string $currency): MoneyInterface
27
    {
28
        /** @var MoneyInterface $balance */
29 4
        $balance = $this->get($currency, Money::zero($currency));
30 4
        return $balance;
31
    }
32
33 1
    public function hasBalance(MoneyInterface $amount): bool
34
    {
35 1
        return $this->getBalance($amount->getCurrency())->isGreaterThanOrEqual($amount);
36
    }
37
38 2
    public function credit(MoneyInterface $amount): self
39
    {
40 2
        $currency = $amount->getCurrency();
41 2
        $balance = $this->getBalance($currency);
42 2
        return $this->with($currency, $balance->add($amount));
43
    }
44
45 1
    public function debit(MoneyInterface $amount): self
46
    {
47 1
        $currency = $amount->getCurrency();
48 1
        $balance = $this->getBalance($currency);
49 1
        return $this->with($currency, $balance->subtract($amount));
50
    }
51
}
52