1313
1414namespace Exchanger \Service ;
1515
16+ use DateInterval ;
17+ use DateTime ;
18+ use DateTimeInterface ;
19+ use Exception ;
1620use Exchanger \Contract \ExchangeRateQuery ;
1721use Exchanger \Contract \HistoricalExchangeRateQuery ;
1822use 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