1 | <?php |
||
11 | class EcbClient implements EcbClientInterface |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * @const string |
||
16 | */ |
||
17 | const HTTP_GET = 'GET'; |
||
18 | |||
19 | /** |
||
20 | * @const string |
||
21 | */ |
||
22 | const DEFAULT_EXCHANGE_RATES_URL = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'; |
||
23 | |||
24 | /** |
||
25 | * @var Client |
||
26 | */ |
||
27 | private $client; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $exchangeRatesUrl; |
||
33 | |||
34 | /** |
||
35 | * @param string $exchangeRatesUrl |
||
36 | */ |
||
37 | public function __construct(string $exchangeRatesUrl = null) |
||
38 | { |
||
39 | if (null === $exchangeRatesUrl) { |
||
40 | $this->exchangeRatesUrl = self::DEFAULT_EXCHANGE_RATES_URL |
||
41 | } else { |
||
|
|||
42 | $this->exchangeRatesUrl = $exchangeRatesUrl; |
||
43 | } |
||
44 | $this->client = new Client(); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @throws ExchangeRatesRequestFailedException |
||
49 | * @return ExchangeRate[] |
||
50 | */ |
||
51 | public function getExchangeRates(): array |
||
52 | { |
||
53 | try { |
||
54 | $response = $this->client->request(self::HTTP_GET, $this->exchangeRatesUrl); |
||
55 | } catch (Exception $e) { |
||
56 | throw new ExchangeRatesRequestFailedException($e); |
||
57 | } |
||
58 | |||
59 | $mapper = new ExchangeRatesMapper(); |
||
60 | return $mapper->map($response); |
||
64 |