Skip to content

Commit b5636e2

Browse files
committed
Handle missing rates by fetching previous day's data
Modified logic to retrieve rates by iterating over previous days if data for the requested date is unavailable. Limits the search to 7 days, ensuring fallback behavior without indefinite looping.
1 parent fb87bf7 commit b5636e2

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/Service/EuropeanCentralBank.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,16 @@ protected function getHistoricalExchangeRate(HistoricalExchangeRateQuery $exchan
7676
$formattedDate = $exchangeQuery->getDate()->format('Y-m-d');
7777
$quoteCurrency = $currencyPair->getQuoteCurrency();
7878

79+
/**
80+
* if rate do not exist for actual date, try get prev day rate, while ge it
81+
*/
7982
$prevDays = 0;
8083
while (empty($element->xpath('//xmlns:Cube[@time="'.$formattedDate.'"]'))) {
8184
$prevDays ++;
8285
if ($prevDays > 7) {
8386
throw new UnsupportedDateException($exchangeQuery->getDate(), $this);
8487
}
85-
$formattedDate = $exchangeQuery
86-
->getDate()
88+
$formattedDate = (clone $exchangeQuery->getDate())
8789
->sub(new DateInterval('P'.$prevDays.'D'))
8890
->format('Y-m-d');
8991
}

0 commit comments

Comments
 (0)