Completed
Push — master ( df9406...2eabb1 )
by Alessandro
04:53
created

CurrencyConverter::getRates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace CurrencyConverter;
4
5
class CurrencyConverter
6
{
7
    private $fromCurrency;
8
9
    private $toCurrency;
10
11
    private $apiKey;
12
13
    private $apiCaller;
14
15
    private $rates;
16
17 3
    public function __construct(string $apiKey)
18
    {
19 3
        $this->apiKey = $apiKey;
20 3
        $this->apiCaller = new ApiCaller($apiKey);
21 3
        $this->rates = new Rates($this->apiCaller);
22 3
    }
23
24 3
    public function convert(string $fromCurrency, string $toCurrency, $amount) :float
25
    {
26 3
        $this->fromCurrency = $fromCurrency;
27 3
        $this->toCurrency = $toCurrency;
28
29 3
        $rate = $this->rates->getRates($fromCurrency, $toCurrency);
30
31 2
        return $this->calculateValue($rate, $amount);
32
    }
33
34
    public function getRates(string $fromCurrency, string $toCurrency) :float
35
    {
36
        return $this->rates->getRates($fromCurrency, $toCurrency);
37
    }
38
39 2
    private function calculateValue($rate, $amount) :float
40
    {
41 2
        $value = (double)$rate * (double)$amount;
42
43 2
        return number_format((double)$value, 2, '.', '');
44
    }
45
46
    public function setApiCaller(ApiCaller $apiCaller)
47
    {
48
        $this->apiCaller = $apiCaller;
49
    }
50
51 2
    public function setRates(Rates $rates)
52
    {
53 2
        $this->rates = $rates;
54 2
    }
55
}
56