EuropeanCentralBank   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 67
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 41 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\Model\ExchangeRate;
12
use Brownie\ExchangeRate\Service;
13
use Brownie\ExchangeRate\SourceInterface;
14
use Brownie\HttpClient\Request;
15
use Brownie\HttpClient\Exception\ClientException;
16
use Brownie\HttpClient\Exception\ValidateException;
17
18
/**
19
 * Gets the exchange rate.
20
 */
21
class EuropeanCentralBank extends Service implements SourceInterface
22
{
23
24
    /**
25
     * Base currency.
26
     *
27
     * @var string
28
     */
29
    protected $baseCode = 'EUR';
30
31
    /**
32
     * The endpoint access to the list of exchange rate.
33
     *
34
     * @var string
35
     */
36
    private $endpoint = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
37
38
    /**
39
     * Returns the current exchange rate.
40
     *
41
     * @return ExchangeRate[]
42
     *
43
     * @throws ClientException
44
     * @throws ValidateException
45
     * @throws InvalidExchangeRateException
46
     */
47 3
    public function getExchangeRates()
48
    {
49
        $response = $this
50 3
            ->getHttpClient()
51 3
            ->request(
52
                $this
53 3
                    ->getHttpClient()
54 3
                    ->createRequest()
55 3
                    ->setMethod(Request::HTTP_METHOD_GET)
56 3
                    ->setUrl($this->endpoint)
57
            );
58
59 3
        if ((200 != $response->getHttpCode()) || (empty($response->getBody()))) {
60 1
            throw new InvalidExchangeRateException('Invalid response from currency exchange server.');
61
        }
62
63 2
        libxml_use_internal_errors(true);
64 2
        $xml = simplexml_load_string($response->getBody());
65
66 2
        if (false === $xml) {
67 1
            throw new InvalidExchangeRateException('Error parsing the response from the currency exchange server.');
68
        }
69
70 1
        $exchangeRates = [];
71 1
        foreach ($xml->Cube->Cube->Cube as $e)
72
        {
73 1
            $currencyCode = (string)$e->attributes()->currency;
74 1
            $exchangeRates[$currencyCode] = new ExchangeRate(
75 1
                $this->getBaseCode(),
76 1
                $currencyCode,
77 1
                (float)$e->attributes()->rate
78
            );
79
        }
80
81 1
        $exchangeRates[$this->getBaseCode()] = new ExchangeRate(
82 1
            $this->getBaseCode(),
83 1
            $this->getBaseCode(),
84 1
            1.0
85
        );
86
87 1
        return $exchangeRates;
88
    }
89
}
90