1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BenTools\Currency\Provider; |
4
|
|
|
|
5
|
|
|
use BenTools\Currency\Model\CurrencyInterface; |
6
|
|
|
use BenTools\Currency\Model\ExchangeRateFactoryInterface; |
7
|
|
|
use BenTools\Currency\Model\ExchangeRateInterface; |
8
|
|
|
use BenTools\Currency\Model\ExchangeRateNotFoundException; |
9
|
|
|
use BenTools\Currency\Model\NativeExchangeRateFactory; |
10
|
|
|
use DateTime; |
11
|
|
|
use DateTimeImmutable; |
12
|
|
|
use DateTimeInterface; |
13
|
|
|
use DateTimeZone; |
14
|
|
|
use Http\Client\HttpClient; |
15
|
|
|
use Http\Discovery\HttpClientDiscovery; |
16
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
17
|
|
|
use Http\Message\RequestFactory; |
18
|
|
|
use Psr\SimpleCache\CacheInterface; |
19
|
|
|
use SimpleXMLElement; |
20
|
|
|
|
21
|
|
|
final class EuropeanCentralBankProvider implements ExchangeRateProviderInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
const LIVE_FEED_URL = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'; |
25
|
|
|
const NINETYDAYS_FEED_URL = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml'; |
26
|
|
|
const FULL_FEED_URL = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var HttpClient|null |
30
|
|
|
*/ |
31
|
|
|
private $client; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var RequestFactory|null |
35
|
|
|
*/ |
36
|
|
|
private $requestFactory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ExchangeRateFactoryInterface|null |
40
|
|
|
*/ |
41
|
|
|
private $exchangeRateFactory; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* EuropeanCentralBankProvider constructor. |
45
|
|
|
* @param HttpClient|null $client |
46
|
|
|
* @param RequestFactory|null $requestFactory |
47
|
|
|
* @param ExchangeRateFactoryInterface|null $exchangeRateFactory |
48
|
|
|
* @param CacheInterface|null $cache |
49
|
|
|
* @throws \Http\Discovery\Exception\NotFoundException |
50
|
|
|
*/ |
51
|
|
|
public function __construct( |
52
|
|
|
HttpClient $client = null, |
53
|
|
|
RequestFactory $requestFactory = null, |
54
|
|
|
ExchangeRateFactoryInterface $exchangeRateFactory = null, |
55
|
|
|
CacheInterface $cache = null |
|
|
|
|
56
|
|
|
) { |
57
|
|
|
$this->client = $client ?? HttpClientDiscovery::find(); |
58
|
|
|
$this->requestFactory = $requestFactory ?? MessageFactoryDiscovery::find(); |
59
|
|
|
$this->exchangeRateFactory = $exchangeRateFactory ?? new NativeExchangeRateFactory(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritDoc |
64
|
|
|
*/ |
65
|
|
|
public function getExchangeRate(CurrencyInterface $sourceCurrency, CurrencyInterface $targetCurrency, DateTimeInterface $date = null): ExchangeRateInterface |
66
|
|
|
{ |
67
|
|
|
if (null === $date) { |
68
|
|
|
$date = new DateTimeImmutable('now', new DateTimeZone('Europe/Paris')); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($date instanceof DateTime) { |
72
|
|
|
$date = DateTimeImmutable::createFromMutable($date)->setTimezone(new DateTimeZone('Europe/Paris')); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$dateString = $date->format('Y-m-d'); |
|
|
|
|
76
|
|
|
|
77
|
|
|
if (!in_array('EUR', [$sourceCurrency->getCode(), $targetCurrency->getCode()])) { |
78
|
|
|
throw new ExchangeRateNotFoundException($sourceCurrency, $targetCurrency, "ECB only provide EUR-based currency conversions."); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Same currencies |
82
|
|
|
if ($sourceCurrency->getCode() === $targetCurrency->getCode()) { |
83
|
|
|
return $this->exchangeRateFactory->create($sourceCurrency, $targetCurrency, 1); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Invert currencies |
87
|
|
|
if ('EUR' === $targetCurrency->getCode()) { // ECB only provide EUR -> * |
88
|
|
|
return $this->getExchangeRate($targetCurrency, $sourceCurrency, $date)->invertCurrencies(); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$url = $this->pickUrl($date); |
|
|
|
|
92
|
|
|
$response = $this->client->sendRequest($this->requestFactory->createRequest('GET', $url)); |
93
|
|
|
$xml = new SimpleXMLElement($response->getBody()); |
94
|
|
|
|
95
|
|
|
$rates = []; |
96
|
|
|
foreach ($xml->Cube->Cube as $cube) { |
97
|
|
|
foreach ($cube->Cube as $rate) { |
98
|
|
|
$currency = (string) $rate['currency']; |
99
|
|
|
$ratio = (float) (string) $rate['rate']; |
100
|
|
|
$rates[(string) $cube['time']][$currency] = $ratio; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (isset($rates[$dateString][$targetCurrency->getCode()])) { |
105
|
|
|
return $this->exchangeRateFactory->create($sourceCurrency, $targetCurrency, $rates[$dateString][$targetCurrency->getCode()]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
throw new ExchangeRateNotFoundException($sourceCurrency, $targetCurrency); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param DateTimeInterface|null $date |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
private function pickUrl(DateTimeInterface $date): string |
117
|
|
|
{ |
118
|
|
|
if ($date instanceof DateTime) { |
119
|
|
|
$date = DateTimeImmutable::createFromMutable($date); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$today = new DateTimeImmutable('today midnight', $date->getTimezone()); |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
switch (true) { |
126
|
|
|
case $date->format('Y-m-d') === $today->format('Y-m-d'): |
127
|
|
|
return static::LIVE_FEED_URL; |
128
|
|
|
|
129
|
|
|
case $date >= new DateTimeImmutable('-90 days', $date->getTimezone()): |
130
|
|
|
return static::NINETYDAYS_FEED_URL; |
131
|
|
|
|
132
|
|
|
default: |
133
|
|
|
return static::FULL_FEED_URL; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.