Passed
Push — master ( b4f1ba...dc5416 )
by Alessandro
02:37 queued 22s
created

CurrencyConverter::setApiCaller()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 3
    public function __construct(string $apiKey)
16
    {
17 3
        $this->apiKey = $apiKey;
18 3
        $this->apiCaller = new ApiCaller($apiKey);
19 3
    }
20
21 3
    public function convert(string $fromCurrency, string $toCurrency, $amount) :float
22
    {
23 3
        $this->fromCurrency = $fromCurrency;
24 3
        $this->toCurrency = $toCurrency;
25
26 3
        $rates = new Rates($this->apiCaller);
27
28 3
        $rate = $rates->getRates($fromCurrency, $toCurrency);
29
30 2
        return $this->calculateValue($rate, $amount);
31
    }
32
33 2
    private function calculateValue($rate, $amount) :float
34
    {
35 2
        $value = (double)$rate * (double)$amount;
36
37 2
        return number_format((double)$value, 2, '.', '');
38
    }
39
40 2
    public function setApiCaller(ApiCaller $apiCaller)
41
    {
42 2
        $this->apiCaller = $apiCaller;
43 2
    }
44
}
45