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

Rates   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 33
ccs 12
cts 13
cp 0.9231
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 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