diff --git a/src/Service/ExchangeRatesApi.php b/src/Service/ExchangeRatesApi.php index 9683ebb..4b44a6b 100755 --- a/src/Service/ExchangeRatesApi.php +++ b/src/Service/ExchangeRatesApi.php @@ -31,9 +31,26 @@ final class ExchangeRatesApi extends HttpService { use SupportsHistoricalQueries; - const LATEST_URL = 'https://api.exchangeratesapi.io/latest?base=%s'; + const LATEST_URL = 'https://api.exchangeratesapi.io/latest?base=%s&access_key=%s'; + + const HISTORICAL_URL = 'https://api.exchangeratesapi.io/%s?base=%s&access_key=%s'; + + const ACCESS_KEY_OPTION = 'access_key'; + + /** + * {@inheritdoc} + */ + public function processOptions(array &$options): void + { + if (!isset($options[self::ACCESS_KEY_OPTION])) { + throw new \InvalidArgumentException('The "access_key" option must be provided to use exchangeratesapi.io'); + } + + if (!isset($options['enterprise'])) { + $options['enterprise'] = false; + } + } - const HISTORICAL_URL = 'https://api.exchangeratesapi.io/%s?base=%s'; /** * {@inheritdoc} @@ -44,7 +61,8 @@ protected function getLatestExchangeRate(ExchangeRateQuery $exchangeQuery): Exch $url = sprintf( self::LATEST_URL, - $currencyPair->getBaseCurrency() + $currencyPair->getBaseCurrency(), + $this->options[self::ACCESS_KEY_OPTION] ); return $this->doCreateRate($url, $currencyPair); @@ -60,7 +78,8 @@ protected function getHistoricalExchangeRate(HistoricalExchangeRateQuery $exchan $url = sprintf( self::HISTORICAL_URL, $exchangeQuery->getDate()->format('Y-m-d'), - $exchangeQuery->getCurrencyPair()->getBaseCurrency() + $exchangeQuery->getCurrencyPair()->getBaseCurrency(), + $this->options[self::ACCESS_KEY_OPTION] ); return $this->doCreateRate($url, $currencyPair); diff --git a/tests/Tests/Service/ExchangeRatesApiTest.php b/tests/Tests/Service/ExchangeRatesApiTest.php index 2dbcaa1..0310a77 100644 --- a/tests/Tests/Service/ExchangeRatesApiTest.php +++ b/tests/Tests/Service/ExchangeRatesApiTest.php @@ -29,7 +29,7 @@ class ExchangeRatesApiTest extends ServiceTestCase */ public function it_does_support_all_queries() { - $service = new ExchangeRatesApi($this->createMock('Http\Client\HttpClient')); + $service = new ExchangeRatesApi($this->createMock('Http\Client\HttpClient'), null, ['access_key' => 'x']); $this->assertTrue($service->supportQuery(new ExchangeRateQuery(CurrencyPair::createFromString('USD/EUR')))); } @@ -39,7 +39,7 @@ public function it_does_support_all_queries() */ public function it_supports_eur_base() { - $service = new ExchangeRatesApi($this->createMock('Http\Client\HttpClient')); + $service = new ExchangeRatesApi($this->createMock('Http\Client\HttpClient'), null, ['access_key' => 'x']); $this->assertTrue($service->supportQuery(new ExchangeRateQuery(CurrencyPair::createFromString('EUR/CAD')))); } @@ -48,7 +48,7 @@ public function it_supports_eur_base() */ public function it_does_support_other_than_eur() { - $service = new ExchangeRatesApi($this->createMock('Http\Client\HttpClient')); + $service = new ExchangeRatesApi($this->createMock('Http\Client\HttpClient'), null, ['access_key' => 'x']); $this->assertTrue($service->supportQuery(new ExchangeRateQuery(CurrencyPair::createFromString('USD/CAD')))); } @@ -60,10 +60,10 @@ public function it_throws_an_exception_with_error_response() $this->expectException(Exception::class); $this->expectExceptionMessage('Base \'FOO\' is not supported.'); - $uri = 'https://api.exchangeratesapi.io/latest?base=FOO'; + $uri = 'https://api.exchangeratesapi.io/latest?base=FOO&access_key=x'; $content = file_get_contents(__DIR__.'/../../Fixtures/Service/ExchangeRatesApi/error.json'); - $service = new ExchangeRatesApi($this->getHttpAdapterMock($uri, $content)); + $service = new ExchangeRatesApi($this->getHttpAdapterMock($uri, $content), null, ['access_key' => 'x']); $service->getExchangeRate(new ExchangeRateQuery(CurrencyPair::createFromString('FOO/EUR'))); } @@ -73,10 +73,10 @@ public function it_throws_an_exception_with_error_response() public function it_fetches_a_rate() { $pair = CurrencyPair::createFromString('EUR/CHF'); - $uri = 'https://api.exchangeratesapi.io/latest?base=EUR'; + $uri = 'https://api.exchangeratesapi.io/latest?base=EUR&access_key=x'; $content = file_get_contents(__DIR__.'/../../Fixtures/Service/ExchangeRatesApi/latest.json'); - $service = new ExchangeRatesApi($this->getHttpAdapterMock($uri, $content)); + $service = new ExchangeRatesApi($this->getHttpAdapterMock($uri, $content), null, ['access_key' => 'x']); $rate = $service->getExchangeRate(new ExchangeRateQuery($pair)); $this->assertEquals(1.0933, $rate->getValue()); @@ -91,11 +91,11 @@ public function it_fetches_a_rate() public function it_fetches_a_historical_rate() { $pair = CurrencyPair::createFromString('EUR/AUD'); - $uri = 'https://api.exchangeratesapi.io/2000-01-03?base=EUR'; + $uri = 'https://api.exchangeratesapi.io/2000-01-03?base=EUR&access_key=x'; $content = file_get_contents(__DIR__.'/../../Fixtures/Service/ExchangeRatesApi/historical.json'); $date = new \DateTime('2000-01-03'); - $service = new ExchangeRatesApi($this->getHttpAdapterMock($uri, $content)); + $service = new ExchangeRatesApi($this->getHttpAdapterMock($uri, $content), null, ['access_key' => 'x']); $rate = $service->getExchangeRate(new HistoricalExchangeRateQuery($pair, $date)); $this->assertEquals(1.5209, $rate->getValue()); @@ -109,7 +109,7 @@ public function it_fetches_a_historical_rate() */ public function it_has_a_name() { - $service = new ExchangeRatesApi($this->createMock('Http\Client\HttpClient')); + $service = new ExchangeRatesApi($this->createMock('Http\Client\HttpClient'), null, ['access_key' => 'x']); $this->assertSame('exchange_rates_api', $service->getName()); }