Money   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 65
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A __toString() 0 8 1
A setAmount() 0 7 1
A getAmount() 0 4 1
A getFormattedAmount() 0 14 2
A getCurrency() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Assimtech\Fiat;
6
7
use Locale;
8
use NumberFormatter;
9
10
class Money
11
{
12
    private $amount;
13
    private $currency;
14
15
    /**
16
     * @param float|integer $amount
17
     * @param Currency $currency iso4217 currency
18
     */
19 10
    public function __construct(
20
        $amount,
21
        Currency $currency
22
    ) {
23 10
        $this->currency = $currency;
24 10
        $this->setAmount($amount);
25 10
    }
26
27 1
    public function __toString(): string
28
    {
29 1
        return sprintf(
30 1
            '%s %s',
31 1
            $this->getFormattedAmount(),
32 1
            $this->currency
33
        );
34
    }
35
36
    /**
37
     * @param float|integer|string $amount
38
     */
39 10
    public function setAmount(
40
        $amount
41
    ): self {
42 10
        $this->amount = round($amount, $this->currency->getFractionDigits());
43
44 10
        return $this;
45
    }
46
47 2
    public function getAmount(): float
48
    {
49 2
        return $this->amount;
50
    }
51
52
    /**
53
     * @param string|null $locale if null, defaults to Locale::getDefault
54
     */
55 2
    public function getFormattedAmount(
56
        string $locale = null
57
    ): string {
58 2
        if ($locale === null) {
59 1
            $locale = Locale::getDefault();
60
        }
61
62 2
        $numberFormatter = new NumberFormatter($locale, NumberFormatter::DECIMAL);
63 2
        $fractionDigits = $this->currency->getFractionDigits();
64 2
        $numberFormatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $fractionDigits);
65 2
        $numberFormatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $fractionDigits);
66
67 2
        return $numberFormatter->format($this->amount);
68
    }
69
70 2
    public function getCurrency(): Currency
71
    {
72 2
        return $this->currency;
73
    }
74
}
75