Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions src/Service/EuropeanCentralBank.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

namespace Exchanger\Service;

use DateInterval;
use DateTime;
use DateTimeInterface;
use Exception;
use Exchanger\Contract\ExchangeRateQuery;
use Exchanger\Contract\HistoricalExchangeRateQuery;
use Exchanger\Exception\UnsupportedCurrencyPairException;
Expand All @@ -29,11 +33,11 @@ final class EuropeanCentralBank extends HttpService
{
use SupportsHistoricalQueries;

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

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

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

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

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

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

$elements = $element->xpath('//xmlns:Cube[@time="'.$formattedDate.'"]/xmlns:Cube[@currency="'.$quoteCurrency.'"]/@rate');

if (empty($elements)) {
if (empty($element->xpath('//xmlns:Cube[@time="'.$formattedDate.'"]'))) {
/**
* if rate do not exist for actual date, try get prev day rate, while ge it
*/
$prevDays = 0;
while (empty($element->xpath('//xmlns:Cube[@time="'.$formattedDate.'"]'))) {
$prevDays ++;
if ($prevDays > 7) {
throw new UnsupportedDateException($exchangeQuery->getDate(), $this);
}
$formattedDate = (clone $exchangeQuery->getDate())
->sub(new DateInterval('P'.$prevDays.'D'))
->format('Y-m-d');
}
$elements = $element->xpath('//xmlns:Cube[@time="'.$formattedDate.'"]/xmlns:Cube[@currency="'.$quoteCurrency.'"]/@rate');

if (empty($elements)) {
throw new UnsupportedCurrencyPairException($currencyPair, $this);
}

Expand All @@ -102,15 +115,15 @@ public function getName(): string
}

/**
* @param \DateTimeInterface $date
* @param DateTimeInterface $date
*
* @return string
*
* @throws \Exception
* @throws Exception
*/
private function getHistoricalUrl(\DateTimeInterface $date): string
private function getHistoricalUrl(DateTimeInterface $date): string
{
$dateDiffInDays = $date->diff(new \DateTime('now'))->days;
$dateDiffInDays = $date->diff(new DateTime('now'))->days;
if ($dateDiffInDays > 90) {
return self::HISTORICAL_URL_OLDER_THAN_90_DAYS;
}
Expand Down