NationalBankUkraine   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 66
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A getExchangeRates() 0 40 5
1
<?php
2
/**
3
 * @category    Brownie/ExchangeRate
4
 * @author      Brownie <[email protected]>
5
 * @license     https://opensource.org/licenses/MIT
6
 */
7
8
namespace Brownie\ExchangeRate\Source;
9
10
use Brownie\ExchangeRate\Exception\InvalidExchangeRateException;
11
use Brownie\ExchangeRate\Service;
12
use Brownie\ExchangeRate\SourceInterface;
13
use Brownie\HttpClient\Exception\ClientException;
14
use Brownie\HttpClient\Exception\ValidateException;
15
use Brownie\ExchangeRate\Model\ExchangeRate;
16
use Brownie\HttpClient\Request;
17
18
class NationalBankUkraine extends Service implements SourceInterface
19
{
20
21
    /**
22
     * Base currency.
23
     *
24
     * @var string
25
     */
26
    protected $baseCode = 'UAH';
27
28
    /**
29
     * The endpoint access to the list of exchange rate.
30
     *
31
     * @var string
32
     */
33
    private $endpoint = 'https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange';
34
35
    /**
36
     * Returns the current exchange rate.
37
     *
38
     * @return ExchangeRate[]
39
     *
40
     * @throws ClientException
41
     * @throws ValidateException
42
     * @throws InvalidExchangeRateException
43
     */
44 3
    public function getExchangeRates()
45
    {
46
        $response = $this
47 3
            ->getHttpClient()
48 3
            ->request(
49
                $this
50 3
                    ->getHttpClient()
51 3
                    ->createRequest()
52 3
                    ->setMethod(Request::HTTP_METHOD_GET)
53 3
                    ->setUrl(sprintf($this->endpoint, date('d.m.Y')))
54
            );
55
56 3
        if ((200 != $response->getHttpCode()) || (empty($response->getBody()))) {
57 1
            throw new InvalidExchangeRateException('Invalid response from currency exchange server.');
58
        }
59
60 2
        libxml_use_internal_errors(true);
61 2
        $xml = simplexml_load_string($response->getBody());
62
63 2
        if (false === $xml) {
64 1
            throw new InvalidExchangeRateException('Error parsing the response from the currency exchange server.');
65
        }
66
67 1
        $exchangeRates = [];
68 1
        foreach ($xml->currency as $e) {
69 1
            $currencyCode = (string)$e->cc;
70 1
            $exchangeRates[$currencyCode] = new ExchangeRate(
71 1
                $this->getBaseCode(),
72 1
                $currencyCode,
73 1
                1 / (float)$e->rate
74
            );
75
        }
76
77 1
        $exchangeRates[$this->getBaseCode()] = new ExchangeRate(
78 1
            $this->getBaseCode(),
79 1
            $this->getBaseCode(),
80 1
            1.0
81
        );
82
83 1
        return $exchangeRates;
84
    }
85
}
86