Passed
Push — master ( 1bd18b...966309 )
by Alec
03:04
created

Currency   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 10
dl 0
loc 52
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getCode() 0 3 1
A equals() 0 3 1
A jsonSerialize() 0 3 1
A __toString() 0 3 1
1
<?php
2
/**
3
 * User: alec
4
 * Date: 05.11.18
5
 * Time: 20:27
6
 */
7
8
namespace AlecRabbit\Money;
9
10
11
use AlecRabbit\Money\Contracts\CurrencyInterface;
12
13
class Currency implements CurrencyInterface, \JsonSerializable
14
{
15
    private const _CODE_LENGTH = 9;
16
17
    /** @var string */
18
    private $code;
19
20
    /**
21
     * @param string $code
22
     */
23 132
    public function __construct(string $code)
24
    {
25 132
        if (\strlen($code) > static::_CODE_LENGTH) {
26 1
            throw new \InvalidArgumentException('Currency code must be not longer then ' . static::_CODE_LENGTH);
27
        }
28 131
        $this->code = strtoupper($code);
29 131
    }
30
31
    /**
32
     * Checks whether this currency is the same as an other.
33
     *
34
     * @param Currency $currency
35
     *
36
     * @return bool
37
     */
38 51
    public function equals(Currency $currency): bool
39
    {
40 51
        return $this->code === $currency->code;
41
    }
42
43
    /**
44
     * @return string
45
     */
46 22
    public function __toString()
47
    {
48 22
        return $this->getCode();
49
    }
50
51
    /**
52
     * Returns the currency code.
53
     */
54 23
    public function getCode(): string
55
    {
56 23
        return $this->code;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 2
    public function jsonSerialize(): string
63
    {
64 2
        return $this->code;
65
    }
66
}