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

Rates   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 53.85%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 33
ccs 7
cts 13
cp 0.5385
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getRates() 0 22 3
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 4
    public function __construct(ApiCaller $apiCaller)
17
    {
18 4
        $this->apiCaller = $apiCaller;
19 4
    }
20
21 2
    public function getRates(string $fromCurrency, string $toCurrency) :float
22
    {
23 2
        $this->apiCaller->convert(
24
            $fromCurrency,
25
            $toCurrency
26
        );
27
28 1
        if ($this->apiCaller->isLastCallEmpty()) {
29 1
            return 0;
30
        }
31
32
        $result = $this->apiCaller->getLastResponse();
33
34
        $conversion = json_decode($result, true);
35
        $key = $fromCurrency . '_' . $toCurrency;
36
37
        if (!isset($conversion[$key])) {
38
            return 0;
39
        }
40
41
        return $conversion[$fromCurrency . '_' . $toCurrency];
42
    }
43
44
}
45