Skip to content

Commit

Permalink
Added in currencyLayer provider.
Browse files Browse the repository at this point in the history
  • Loading branch information
mitquinn committed Apr 6, 2018
1 parent 4e72769 commit 3a209b5
Show file tree
Hide file tree
Showing 8 changed files with 2,872 additions and 13 deletions.
2,628 changes: 2,628 additions & 0 deletions composer.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public function getCurrencyByCountryCode(string $countryCode) : ?string
* @return null|string
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function getCurrencySymbolByAlpha3(string $alpha3, string $locale = null) : ?string
public function getCurrencySymbolByAlpha3(string $alpha3, ?string $locale = null) : ?string
{
if (is_null($locale)) {
$locale = $this->getLocale();
Expand All @@ -237,11 +237,11 @@ public function getCurrencySymbolByAlpha3(string $alpha3, string $locale = null)

/**
* @param string $countryCode
* @param null $locale
* @param string|null $locale
* @return null|string
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function getCurrencySymbolByCountryCode(string $countryCode, string $locale = null) : ?string
public function getCurrencySymbolByCountryCode(string $countryCode, ?string $locale = null) : ?string
{
$alpha3 = $this->getAlpha3ByCountryCode($countryCode);
return $this->getCurrencySymbolByAlpha3($alpha3, $locale);
Expand Down
168 changes: 168 additions & 0 deletions src/Providers/CurrencyLayer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php

namespace Currency\Captain\Providers;

use Currency\Captain\Traits\HasHttpClient;
use Currency\Captain\Traits\HasLogger;
use GuzzleHttp\Psr7\Request;
use Http\Client\HttpClient;
use Monolog\Logger;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;


ini_set('xdebug.var_display_max_depth', 5);
ini_set('xdebug.var_display_max_children', 256);
ini_set('xdebug.var_display_max_data', 1024);

/**
* Class CurrencyLayer
* @package Currency\Captain\Providers
*/
class CurrencyLayer implements ProviderInterface
{
/** HasLogger */
use HasLogger;

/** HasHttpClient */
use HasHttpClient;

/** @var string $key CurrencyLayer API key. */
protected $key;

/**
* CurrencyLayer constructor.
* @param LoggerInterface|null $logger
* @param HttpClient|null $client
* @param string $key
*/
public function __construct(?LoggerInterface $logger = null, ?HttpClient $client = null, string $key)
{
if(is_null($logger)) {
$logger = new Logger('Currency_Captain_CurrencyLayer');
}
$this->setLogger($logger);

if(is_null($client)) {
$client = $this->getClient();
}
$this->setClient($client);

//Set the API Key.
$this->setKey($key);
}

/**
* CurrencyLayer does not support changing the base currency. So a little math is needed.
* @param string $from
* @param string $to
* @return float|null
* @throws \Http\Client\Exception
*/
public function getConversionRate(string $from, string $to) : ?float
{
$rate = null;
$key = $this->getKey();
$endPoint = "http://apilayer.net/api/live?access_key=$key&format=1";
$request = new Request('GET', $endPoint);
try {
$response = $this->getClient()->sendRequest($request);
if ($response->getStatusCode() != 200) {
throw new Exception("CurrencyLayer - Returned non-200 response.");
}
$rate = $this->parseConversionRateResponse($response, $from, $to);
} catch (\Exception $exception) {
$this->getLogger()->error($exception->getMessage());
}
return $rate;
}

/**
* The base currency must be USD.
* @return array
* @throws \Http\Client\Exception
*/
public function getCurrencyList() : array
{

$key = $this->getKey();
$endPoint = "http://apilayer.net/api/live?access_key=$key&format=1";
$request = new Request('GET', $endPoint);
try {
$response = $this->getClient()->sendRequest($request);
if ($response->getStatusCode() != 200) {
throw new \Exception("CurrencyLayer - Returned non-200 response.");
}
return $this->parseCurrencyListResponse($response);
} catch (\Exception $exception) {
$this->getLogger()->error($exception->getMessage());
}
}

/**
* @param string $key
* @return CurrencyLayer
*/
public function setKey(string $key): CurrencyLayer
{
$this->key = $key;
return $this;
}

/**
* @return string
*/
protected function getKey()
{
return $this->key;
}


/**
* @param ResponseInterface $response
* @param string $from
* @param string $to
* @return float
* @throws \Exception
*/
private function parseConversionRateResponse(ResponseInterface $response, string $from, string $to) : float
{
$contents = $response->getBody()->getContents();
$decoded = json_decode($contents);
$from = 'USD'.$from;
$to = 'USD'.$to;


if(!isset($decoded->quotes)) {
throw new \Exception('CurrencyLayer - Conversion rates not found.');
}

$rates = $decoded->quotes;
if(!isset($rates->$from) or !isset($rates->$to)) {
throw new \Exception("CurrencyLayer - Conversion rate not found for $from -> $to");
}

return $rates->$to / $rates->$from;

}


/**
* @param ResponseInterface $response
* @return array
* @throws \Exception
*/
private function parseCurrencyListResponse(ResponseInterface $response) : array
{
$contents = $response->getBody()->getContents();
$decoded = json_decode($contents, true);
if(isset($decoded['quotes'])) {
$currencyList = array_keys($decoded['quotes']);
foreach($currencyList as &$value) {
$value = substr($value, 3, 3);
}
return $currencyList;
}
throw new \Exception('CurrencyLayer - Rates not found.');
}
}
15 changes: 9 additions & 6 deletions src/Providers/Fixerio.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Currency\Captain\Providers;

use Http\Discovery\HttpClientDiscovery;
use Monolog\Logger;
use Http\Client\HttpClient;
use GuzzleHttp\Psr7\Request;
Expand All @@ -15,6 +14,8 @@
/**
* Class Fixerio
* @package Currency\Captain\Providers
* @see https://fixer.io/
* @todo Fixer.io has upgraded there API. Must upgrade provider to new API endpoints.
*/
class Fixerio implements ProviderInterface
{
Expand All @@ -29,15 +30,15 @@ class Fixerio implements ProviderInterface
* @param LoggerInterface|null $logger
* @param HttpClient|null $client
*/
public function __construct(LoggerInterface $logger = null, HttpClient $client = null)
public function __construct(?LoggerInterface $logger = null, ?HttpClient $client = null)
{
if (is_null($logger)) {
$logger = new Logger('Currency_Captain_Fixerio');
}
$this->setLogger($logger);

if (is_null($client)) {
$client = HttpClientDiscovery::find();
$client = $this->getClient();
}
$this->setClient($client);
}
Expand All @@ -47,6 +48,7 @@ public function __construct(LoggerInterface $logger = null, HttpClient $client =
* @param string $from Base currency for conversion.
* @param string $to Target currency for conversion.
* @return float|null Conversion rate for the base to target currency.
* @throws \Http\Client\Exception
*/
public function getConversionRate(string $from, string $to) : ?float
{
Expand All @@ -69,11 +71,12 @@ public function getConversionRate(string $from, string $to) : ?float
* Returns a pseudo currency list.
* Fixerio does not seem to have an endpoint for a full currency list.
* @return array
* @throws \Http\Client\Exception
*/
public function getCurrencyList() : array
{
$currencyList = array();
$endpoint = "https://api.fixer.io/latest?base=EUR";
$endpoint = "https://api.fixer.io/latest?base=USD";
$request = new Request('GET', $endpoint);
try {
$response = $this->getClient()->sendRequest($request);
Expand Down Expand Up @@ -105,7 +108,7 @@ private function parseConversionRateResponse(ResponseInterface $response, string
}

/**
* Parses the Fixerio list building a list of currency ccy. Also manually adds EUR.
* Parses the Fixerio list building a list of currency ccy. Also manually adds EUR.
* @param ResponseInterface $response
* @return array
* @throws \Exception
Expand All @@ -116,7 +119,7 @@ private function parseCurrencyListResponse(ResponseInterface $response) : array
$decoded = json_decode($contents);
if (isset($decoded->rates)) {
$currencyList = array_keys((array)$decoded->rates);
$currencyList[] = 'EUR';
$currencyList[] = 'USD';
return $currencyList;
}
throw new \Exception('Fixerio - Rates not found.');
Expand Down
1 change: 1 addition & 0 deletions src/Providers/ProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface ProviderInterface
public function getConversionRate(string $from, string $to) : ?float;

/**
* The base currency must be USD.
* @return array
*/
public function getCurrencyList() : array;
Expand Down
10 changes: 6 additions & 4 deletions tests/ConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

/**
* Class ConverterTest
* @package Tests\Currency\Captain
* @group Converter
*/
class ConverterTest extends TestCase
{
Expand Down Expand Up @@ -178,7 +180,7 @@ public function testGetAlpha3ByCountryCode2()
* @throws \Psr\SimpleCache\InvalidArgumentException
* @dataProvider countryCurrencyProvider
*/
public function testGetCurrencyByCountryCode(string $countryCode, $currencyName)
public function testGetCurrencyByCountryCode(string $countryCode, ?string $currencyName)
{
$result = $this->getConverter()->getCurrencyByCountryCode($countryCode);
static::assertEquals($currencyName, $result);
Expand Down Expand Up @@ -208,7 +210,7 @@ public function countryCurrencyProvider()
* Tests for getCurrencySymbol.
* @dataProvider currencySymbolProvider
*/
public function testGetCurrencySymbol(string $alpha3, $locale, string $symbol)
public function testGetCurrencySymbol(string $alpha3, ?string $locale, string $symbol)
{
$result = $this->getConverter()->getCurrencySymbolByAlpha3($alpha3, $locale);
static::assertEquals($symbol, $result);
Expand Down Expand Up @@ -239,7 +241,7 @@ public function currencySymbolProvider()
* @throws \Psr\SimpleCache\InvalidArgumentException
* @dataProvider countrySymbolProvider
*/
public function testGetCurrencySymbolByCountryCode(string $countryCode, $locale, string $symbol)
public function testGetCurrencySymbolByCountryCode(string $countryCode, ?string $locale, string $symbol)
{
$result = $this->getConverter()->getCurrencySymbolByCountryCode($countryCode, $locale);
static::assertEquals($symbol, $result);
Expand Down Expand Up @@ -267,7 +269,7 @@ public function countrySymbolProvider()
* @param string $symbol
* @dataProvider currencySymbolProvider
*/
public function testGetCurrencySymbolByAlpha3(string $alpha3, $locale, string $symbol)
public function testGetCurrencySymbolByAlpha3(string $alpha3, ?string $locale, string $symbol)
{
$result = $this->getConverter()->getCurrencySymbolByAlpha3($alpha3, $locale);
static::assertEquals($symbol, $result);
Expand Down
56 changes: 56 additions & 0 deletions tests/Providers/CurrencyLayerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Tests\Currency\Captain\Providers;

use Currency\Captain\Providers\CurrencyLayer;
use PHPUnit\Framework\TestCase;

/**
* Class CurrencyLayerTest
* @package Tests\Currency\Captain\Providers
* @group CurrencyLayer
*/
class CurrencyLayerTest extends TestCase
{
protected $currencyLayer;

public function setUp()
{
$currencyLayer = new CurrencyLayer(null, null, '3c2dfdae43b5c8f83b4ef78fa5997480');
$this->setCurrencyLayer($currencyLayer);
}


public function testGetConversionRate()
{
$rate = $this->getCurrencyLayer()->getConversionRate('EUR', 'TRY');
static::assertTrue(is_float($rate));

}

public function testGetCurrencyList()
{
$currencyList = $this->getCurrencyLayer()->getCurrencyList();
static::assertContains('USD', $currencyList);
static::assertContains('TRY', $currencyList);
}


/**
* @param CurrencyLayer $currencyLayer
* @return CurrencyLayerTest
*/
private function setCurrencyLayer(CurrencyLayer $currencyLayer)
{
$this->currencyLayer = $currencyLayer;
return $this;
}

/**
* @return CurrencyLayer
*/
private function getCurrencyLayer()
{
return $this->currencyLayer;
}
}
1 change: 1 addition & 0 deletions tests/Providers/FixerioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/**
* Class FixerioTest
* @package Tests\Currency\Captain\Providers
* @group Fixerio
*/
class FixerioTest extends TestCase
{
Expand Down

0 comments on commit 3a209b5

Please sign in to comment.