CurrencyFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 12
dl 0
loc 51
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 3 1
A get() 0 3 1
A __construct() 0 5 1
A create() 0 9 2
A getAvailableCurrencies() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Whallysson\Money\Currency;
6
7
use Whallysson\Money\Currency\Coins\BRL;
8
use Whallysson\Money\Currency\Coins\EUR;
9
use Whallysson\Money\Currency\Coins\USD;
10
11
/**
12
 * Class CurrencyFactory
13
 *
14
 * @author Whallysson Avelino <[email protected]>
15
 * @package Whallysson\Money\Currency
16
 */
17
class CurrencyFactory
18
{
19
    /** @var CurrencyInterface[] */
20
    private array $currencies = [];
21
22
    public function __construct()
23
    {
24
        $this->register(new BRL());
25
        $this->register(new USD());
26
        $this->register(new EUR());
27
    }
28
29
    /**
30
     * @param CurrencyInterface $currency
31
     * @return void
32
     */
33
    public function register(CurrencyInterface $currency): void
34
    {
35
        $this->currencies[$currency->getCode()] = $currency;
36
    }
37
38
    /**
39
     * @param string $currency
40
     * @return CurrencyInterface
41
     */
42
    public static function get(string $currency): CurrencyInterface
43
    {
44
        return (new self())->create($currency);
45
    }
46
47
    /**
48
     * @param string $code
49
     * @return CurrencyInterface
50
     */
51
    public function create(string $code): CurrencyInterface
52
    {
53
        $code = strtoupper($code);
54
55
        if (!isset($this->currencies[$code])) {
56
            throw new \InvalidArgumentException('Unsupported currency: ' . $code);
57
        }
58
59
        return $this->currencies[$code];
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    public function getAvailableCurrencies(): array
66
    {
67
        return array_keys($this->currencies);
68
    }
69
}
70