CoinCounter::addCurrency()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace LlewellynThomas\Coins\Counter;
4
5
use LlewellynThomas\Coins\Currency\CurrencyInterface;
6
7
/**
8
 * Class CoinCounter
9
 * @package LlewellynThomas\Coins\Counter
10
 */
11
class CoinCounter
12
{
13
    /**
14
     * @var array
15
     */
16
    private $currencies;
17
18
    /**
19
     * @var CurrencyInterface
20
     */
21
    private $currency;
22
23
    /**
24
     * @param CurrencyInterface $currency
25
     */
26
    public function addCurrency(CurrencyInterface $currency)
27
    {
28
        if (empty($this->currencies[$currency->getName()])) {
29
            $this->currencies[$currency->getName()] = $currency;
30
        }
31
    }
32
33
    /**
34
     * @param $currencyName
35
     * @param $amount
36
     * @return array
37
     */
38
    public function calculateCoins($currencyName, $amount)
39
    {
40
        $this->setCurrency($currencyName);
41
        
42
        return $this->currency->countCoins($amount);
43
    }
44
45
    /**
46
     * @param $currency
47
     */
48
    private function setCurrency($currency)
49
    {
50
        if (empty($this->currencies[$currency])) {
51
            throw new \RuntimeException("Currency not supported: " . $currency);
52
        }
53
54
        $this->currency = $this->currencies[$currency];
55
    }
56
}