Completed
Push — master ( 2eabb1...48c775 )
by Alessandro
01:50
created

Rates::getRates()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 9
cts 10
cp 0.9
rs 9.568
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3.009
1
<?php
2
3
/**
4
 * Created by PhpStorm.
5
 * User: alessandrominoccheri
6
 * Date: 05/09/18
7
 * Time: 12:59
8
 */
9
10
namespace CurrencyConverter;
11
12
class Rates
13
{
14
    private $apiCaller;
15
16 5
    public function __construct(ApiCaller $apiCaller)
17
    {
18 5
        $this->apiCaller = $apiCaller;
19 5
    }
20
21 3
    public function getRates(string $fromCurrency, string $toCurrency) :float
22
    {
23 3
        $this->apiCaller->convert(
24
            $fromCurrency,
25
            $toCurrency
26
        );
27
28 2
        if ($this->apiCaller->isLastCallEmpty()) {
29 1
            return 0;
30
        }
31
32 1
        $result = $this->apiCaller->getLastResponse();
33
34 1
        $conversion = json_decode($result, true);
35 1
        $key = $fromCurrency . '_' . $toCurrency;
36
37 1
        if (!isset($conversion[$key])) {
38
            return 0;
39
        }
40
41 1
        return $conversion[$fromCurrency . '_' . $toCurrency];
42
    }
43
44
}
45