Passed
Push — master ( 48c775...23192d )
by Alessandro
01:22
created

CurrencyConverter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 78.26%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 56
ccs 18
cts 23
cp 0.7826
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A convert() 0 9 1
A calculateValue() 0 6 1
A setApiCaller() 0 4 1
A setRates() 0 4 1
A getRates() 0 4 1
A getApiCaller() 0 4 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
    private $rates;
16
17 4
    public function __construct(string $apiKey)
18
    {
19 4
        $this->apiKey = $apiKey;
20 4
        $this->apiCaller = new ApiCaller($apiKey);
21 4
        $this->rates = new Rates($this->apiCaller);
22 4
    }
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 1
    public function getRates(string $fromCurrency, string $toCurrency) :float
35
    {
36 1
        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 3
    public function setRates(Rates $rates)
52
    {
53 3
        $this->rates = $rates;
54 3
    }
55
56
    public function getApiCaller() :ApiCaller
57
    {
58
        return $this->apiCaller;
59
    }
60
}
61