-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
279 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,15 +26,21 @@ | |
/** | ||
* ExchangeRatesApi Service. | ||
* | ||
* @see https://exchangeratesapi.io/documentation/ | ||
* | ||
* @author Arjan Westdorp <[email protected]> | ||
*/ | ||
final class ExchangeRatesApi extends HttpService | ||
{ | ||
use SupportsHistoricalQueries; | ||
|
||
const LATEST_URL = 'https://api.exchangeratesapi.io/latest?base=%s&access_key=%s'; | ||
const LATEST_URL = 'https://api.exchangeratesapi.io/latest?base=%s&access_key=%s&symbols=%s'; | ||
|
||
const HISTORICAL_URL = 'https://api.exchangeratesapi.io/%s?base=%s&access_key=%s&symbols=%s'; | ||
|
||
const FREE_LATEST_URL = 'http://api.exchangeratesapi.io/latest?access_key=%s&symbols=%s'; | ||
|
||
const HISTORICAL_URL = 'https://api.exchangeratesapi.io/%s?base=%s&access_key=%s'; | ||
const FREE_HISTORICAL_URL = 'http://api.exchangeratesapi.io/%s?access_key=%s&symbols=%s'; | ||
|
||
const ACCESS_KEY_OPTION = 'access_key'; | ||
|
||
|
@@ -46,6 +52,10 @@ public function processOptions(array &$options): void | |
if (!isset($options[self::ACCESS_KEY_OPTION])) { | ||
throw new NonBreakingInvalidArgumentException('The "access_key" option must be provided to use exchangeratesapi.io'); | ||
} | ||
|
||
if (!isset($options['enterprise'])) { | ||
$options['enterprise'] = false; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -54,12 +64,20 @@ public function processOptions(array &$options): void | |
protected function getLatestExchangeRate(ExchangeRateQuery $exchangeQuery): ExchangeRateContract | ||
{ | ||
$currencyPair = $exchangeQuery->getCurrencyPair(); | ||
|
||
$url = sprintf( | ||
self::LATEST_URL, | ||
$currencyPair->getBaseCurrency(), | ||
$this->options[self::ACCESS_KEY_OPTION] | ||
); | ||
if ($this->options['enterprise']) { | ||
$url = sprintf( | ||
self::LATEST_URL, | ||
$currencyPair->getBaseCurrency(), | ||
$this->options[self::ACCESS_KEY_OPTION], | ||
$currencyPair->getQuoteCurrency() | ||
); | ||
} else { | ||
$url = sprintf( | ||
self::FREE_LATEST_URL, | ||
$this->options[self::ACCESS_KEY_OPTION], | ||
$currencyPair->getQuoteCurrency() | ||
); | ||
} | ||
|
||
return $this->doCreateRate($url, $currencyPair); | ||
} | ||
|
@@ -70,13 +88,22 @@ protected function getLatestExchangeRate(ExchangeRateQuery $exchangeQuery): Exch | |
protected function getHistoricalExchangeRate(HistoricalExchangeRateQuery $exchangeQuery): ExchangeRateContract | ||
{ | ||
$currencyPair = $exchangeQuery->getCurrencyPair(); | ||
|
||
$url = sprintf( | ||
self::HISTORICAL_URL, | ||
$exchangeQuery->getDate()->format('Y-m-d'), | ||
$exchangeQuery->getCurrencyPair()->getBaseCurrency(), | ||
$this->options[self::ACCESS_KEY_OPTION] | ||
); | ||
if ($this->options['enterprise']) { | ||
$url = sprintf( | ||
self::HISTORICAL_URL, | ||
$exchangeQuery->getDate()->format('Y-m-d'), | ||
$exchangeQuery->getCurrencyPair()->getBaseCurrency(), | ||
$this->options[self::ACCESS_KEY_OPTION], | ||
$currencyPair->getQuoteCurrency() | ||
); | ||
} else { | ||
$url = sprintf( | ||
self::FREE_HISTORICAL_URL, | ||
$exchangeQuery->getDate()->format('Y-m-d'), | ||
$this->options[self::ACCESS_KEY_OPTION], | ||
$exchangeQuery->getCurrencyPair()->getQuoteCurrency() | ||
); | ||
} | ||
|
||
return $this->doCreateRate($url, $currencyPair); | ||
} | ||
|
@@ -86,16 +113,13 @@ protected function getHistoricalExchangeRate(HistoricalExchangeRateQuery $exchan | |
*/ | ||
public function supportQuery(ExchangeRateQuery $exchangeQuery): bool | ||
{ | ||
return true; | ||
return $this->options['enterprise'] || 'EUR' === $exchangeQuery->getCurrencyPair()->getBaseCurrency(); | ||
} | ||
|
||
/** | ||
* Creates a rate. | ||
* | ||
* @param string $url | ||
* @param CurrencyPair $currencyPair | ||
* | ||
* @return ExchangeRate | ||
* @param string $url | ||
* | ||
* @throws Exception | ||
*/ | ||
|
@@ -105,7 +129,22 @@ private function doCreateRate($url, CurrencyPair $currencyPair): ExchangeRate | |
$data = StringUtil::jsonToArray($content); | ||
|
||
if (isset($data['error'])) { | ||
throw new Exception($data['error']); | ||
if (isset($data['error']['code'])) { | ||
if (\in_array($data['error']['code'], [ | ||
'invalid_currency_codes', | ||
'invalid_base_currency', | ||
'no_rates_available', | ||
], true)) { | ||
throw new UnsupportedCurrencyPairException($currencyPair, $this); | ||
} | ||
if (isset($data['error']['message'])) { | ||
throw new Exception($data['error']['message']); | ||
} else { | ||
throw new Exception('Service return error code: '.$data['error']['code']); | ||
} | ||
} else { | ||
throw new Exception('Service return unhandled error'); | ||
} | ||
} | ||
|
||
if (isset($data['rates'][$currencyPair->getQuoteCurrency()])) { | ||
|
6 changes: 6 additions & 0 deletions
6
tests/Fixtures/Service/ExchangeRatesApi/base_currency_access_restricted.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"error":{ | ||
"code":"base_currency_access_restricted", | ||
"message":"An unexpected error ocurred. [Technical Support: [email protected]]" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,10 @@ | ||
{ | ||
"base": "USD", | ||
"date": "2000-01-03", | ||
"rates": { | ||
"AUD": 1.5209, | ||
"CAD": 1.4447, | ||
"CHF": 1.59, | ||
"CYP": 0.57156, | ||
"CZK": 35.741, | ||
"DKK": 7.374, | ||
"EEK": 15.507, | ||
"GBP": 0.61903, | ||
"HKD": 7.7923, | ||
"HUF": 252.26, | ||
"ISK": 72.379, | ||
"JPY": 101.83, | ||
"KRW": 1129.9, | ||
"LTL": 4.0093, | ||
"LVL": 0.58632, | ||
"MTL": 0.4114, | ||
"NOK": 7.9901, | ||
"NZD": 1.9159, | ||
"PLN": 4.1462, | ||
"ROL": 18110.0, | ||
"SEK": 8.4757, | ||
"SGD": 1.6619, | ||
"SIT": 197.12, | ||
"SKK": 41.94, | ||
"TRL": 541260.0, | ||
"ZAR": 6.146, | ||
"EUR": 0.99108 | ||
} | ||
"success":true, | ||
"timestamp":1618531199, | ||
"historical":true, | ||
"base":"EUR", | ||
"date":"2021-04-15", | ||
"rates":{ | ||
"USD":1.196953 | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/Fixtures/Service/ExchangeRatesApi/https_access_restricted.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"error":{ | ||
"code":"https_access_restricted", | ||
"message":"Access Restricted - Your current Subscription Plan does not support HTTPS Encryption." | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/Fixtures/Service/ExchangeRatesApi/invalid_access_key.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"error":{ | ||
"code":"invalid_access_key", | ||
"message":"You have not supplied a valid API Access Key. [Technical Support: [email protected]]" | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/Fixtures/Service/ExchangeRatesApi/invalid_base_currency.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"error":{ | ||
"code":"invalid_base_currency", | ||
"message":"An unexpected error ocurred. [Technical Support: [email protected]]" | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/Fixtures/Service/ExchangeRatesApi/invalid_currency_codes.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"error":{ | ||
"code":"invalid_currency_codes", | ||
"message":"You have provided one or more invalid Currency Codes. [Required format: currencies=EUR,USD,GBP,...]" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"error":{ | ||
"code":"invalid_date", | ||
"message":"You have entered an invalid date. [Required format: date=YYYY-MM-DD]" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,9 @@ | ||
{ | ||
"base": "EUR", | ||
"date": "2016-08-26", | ||
"rates": { | ||
"AUD": 1.4771, | ||
"BGN": 1.9558, | ||
"BRL": 3.6441, | ||
"CAD": 1.4546, | ||
"CHF": 1.0933, | ||
"CNY": 7.5318, | ||
"CZK": 27.024, | ||
"DKK": 7.4462, | ||
"GBP": 0.8545, | ||
"HKD": 8.7555, | ||
"HRK": 7.4893, | ||
"HUF": 308.5, | ||
"IDR": 14906.0, | ||
"ILS": 4.2444, | ||
"INR": 75.647, | ||
"JPY": 113.33, | ||
"KRW": 1258.02, | ||
"MXN": 20.7197, | ||
"MYR": 4.5349, | ||
"NOK": 9.2501, | ||
"NZD": 1.5418, | ||
"PHP": 52.29, | ||
"PLN": 4.3244, | ||
"RON": 4.4578, | ||
"RUB": 73.1392, | ||
"SEK": 9.4903, | ||
"SGD": 1.5269, | ||
"THB": 39.004, | ||
"TRY": 3.316, | ||
"USD": 1.129, | ||
"ZAR": 15.8622 | ||
} | ||
"success":true, | ||
"timestamp":1619171643, | ||
"base":"EUR", | ||
"date":"2021-04-23", | ||
"rates":{ | ||
"USD":1.20555 | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/Fixtures/Service/ExchangeRatesApi/no_rates_available.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"error":{ | ||
"code":"no_rates_available", | ||
"message":"Your query did not return any results. Please try again." | ||
} | ||
} |
Oops, something went wrong.