Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EuropeanCentralBank search previous days rate, if no exist requested … #162

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
35 changes: 23 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,20 @@ 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.'"]'))) {
$prevDays = 0;
while (empty($element->xpath('//xmlns:Cube[@time="'.$formattedDate.'"]'))) {
$prevDays ++;
if ($prevDays > 7) {
throw new UnsupportedDateException($exchangeQuery->getDate(), $this);
}
$formattedDate = $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 +113,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