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

CurrencyConverter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 40
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A convert() 0 11 1
A calculateValue() 0 6 1
A setApiCaller() 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 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