Fixer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 9 2
A setRates() 0 3 1
A getRates() 0 5 2
1
<?php
2
3
namespace Rdehnhardt\ExchangeRate;
4
5
use GuzzleHttp\Client;
6
use Rdehnhardt\ExchangeRate\Excaptions\NotFoundException;
7
8
class Fixer
9
{
10
    /**
11
     * @var array
12
     */
13
    private $rates;
14
15
    /**
16
     * @param string $from
17
     * @param string $to
18
     * @return mixed
19
     * @throws NotFoundException
20
     */
21
    public function get($from, $to)
22
    {
23
        $this->getRates($from);
24
25
        if (!array_key_exists(strtoupper($to), $this->rates[$from])) {
26
            throw new NotFoundException("Exchange rate '$to' not found.");
27
        }
28
29
        return $this->rates[$from][strtoupper($to)];
30
    }
31
32
    /**
33
     * @param string $from
34
     */
35
    private function getRates($from)
36
    {
37
        if (!$this->rates[$from]) {
38
            $response = (new Client())->get("http://api.fixer.io/latest?base={$from}");
39
            $this->rates[$from] = json_decode($response->getBody(), true)['rates'];
40
        }
41
    }
42
43
    /**
44
     * @param string $from
45
     * @param array $rates
46
     */
47
    public function setRates($from, $rates)
48
    {
49
        $this->rates[$from] = $rates;
50
    }
51
}
52