Wallet::credit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 1
b 0
f 0
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