Skip to content

Commit fb87bf7

Browse files
committed
EuropeanCentralBank search previous days rate, if no exist requested rate
Signed-off-by: uldisn <[email protected]>
1 parent 09183c7 commit fb87bf7

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

src/Service/EuropeanCentralBank.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
namespace Exchanger\Service;
1515

16+
use DateInterval;
17+
use DateTime;
18+
use DateTimeInterface;
19+
use Exception;
1620
use Exchanger\Contract\ExchangeRateQuery;
1721
use Exchanger\Contract\HistoricalExchangeRateQuery;
1822
use Exchanger\Exception\UnsupportedCurrencyPairException;
@@ -29,11 +33,11 @@ final class EuropeanCentralBank extends HttpService
2933
{
3034
use SupportsHistoricalQueries;
3135

32-
const DAILY_URL = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
36+
private const DAILY_URL = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
3337

34-
const HISTORICAL_URL_LIMITED_TO_90_DAYS_BACK = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml';
38+
private const HISTORICAL_URL_LIMITED_TO_90_DAYS_BACK = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml';
3539

36-
const HISTORICAL_URL_OLDER_THAN_90_DAYS = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml';
40+
private const HISTORICAL_URL_OLDER_THAN_90_DAYS = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml';
3741

3842
/**
3943
* {@inheritdoc}
@@ -48,7 +52,7 @@ protected function getLatestExchangeRate(ExchangeRateQuery $exchangeQuery): Exch
4852

4953
$quoteCurrency = $currencyPair->getQuoteCurrency();
5054
$elements = $element->xpath('//xmlns:Cube[@currency="'.$quoteCurrency.'"]/@rate');
51-
$date = new \DateTime((string) $element->xpath('//xmlns:Cube[@time]/@time')[0]);
55+
$date = new DateTime((string) $element->xpath('//xmlns:Cube[@time]/@time')[0]);
5256

5357
if (empty($elements) || !$date) {
5458
throw new UnsupportedCurrencyPairException($currencyPair, $this);
@@ -72,13 +76,20 @@ protected function getHistoricalExchangeRate(HistoricalExchangeRateQuery $exchan
7276
$formattedDate = $exchangeQuery->getDate()->format('Y-m-d');
7377
$quoteCurrency = $currencyPair->getQuoteCurrency();
7478

75-
$elements = $element->xpath('//xmlns:Cube[@time="'.$formattedDate.'"]/xmlns:Cube[@currency="'.$quoteCurrency.'"]/@rate');
76-
77-
if (empty($elements)) {
78-
if (empty($element->xpath('//xmlns:Cube[@time="'.$formattedDate.'"]'))) {
79+
$prevDays = 0;
80+
while (empty($element->xpath('//xmlns:Cube[@time="'.$formattedDate.'"]'))) {
81+
$prevDays ++;
82+
if ($prevDays > 7) {
7983
throw new UnsupportedDateException($exchangeQuery->getDate(), $this);
8084
}
85+
$formattedDate = $exchangeQuery
86+
->getDate()
87+
->sub(new DateInterval('P'.$prevDays.'D'))
88+
->format('Y-m-d');
89+
}
90+
$elements = $element->xpath('//xmlns:Cube[@time="'.$formattedDate.'"]/xmlns:Cube[@currency="'.$quoteCurrency.'"]/@rate');
8191

92+
if (empty($elements)) {
8293
throw new UnsupportedCurrencyPairException($currencyPair, $this);
8394
}
8495

@@ -102,15 +113,15 @@ public function getName(): string
102113
}
103114

104115
/**
105-
* @param \DateTimeInterface $date
116+
* @param DateTimeInterface $date
106117
*
107118
* @return string
108119
*
109-
* @throws \Exception
120+
* @throws Exception
110121
*/
111-
private function getHistoricalUrl(\DateTimeInterface $date): string
122+
private function getHistoricalUrl(DateTimeInterface $date): string
112123
{
113-
$dateDiffInDays = $date->diff(new \DateTime('now'))->days;
124+
$dateDiffInDays = $date->diff(new DateTime('now'))->days;
114125
if ($dateDiffInDays > 90) {
115126
return self::HISTORICAL_URL_OLDER_THAN_90_DAYS;
116127
}

0 commit comments

Comments
 (0)