Fixer::get()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 1
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
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