Skip to content

Commit

Permalink
add support for laravel 8
Browse files Browse the repository at this point in the history
refactor code

update service provider
  • Loading branch information
AndikanGabriel committed May 9, 2021
1 parent bb467dc commit dba172c
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 74 deletions.
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
"issues": "https://github.com/AndikanGabriel/coindesk/issues"
},
"require": {
"php": "^7.2",
"guzzlehttp/guzzle": "~6.0",
"php": "^7.4 || ^8.0",
"guzzlehttp/guzzle": "^7.0",
"illuminate/support": "^8.40"
},
"require-dev": {
"phpunit/phpunit": "^8.0"
"orchestra/testbench": "^5.0 || ^6.0",
"phpunit/phpunit": "^9.0"
},
"autoload": {
"psr-4": {
Expand Down
180 changes: 110 additions & 70 deletions src/Coindesk.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,85 @@

namespace GabrielAndy\Coindesk;

use GabrielAndy\Coindesk\Exceptions\CoindeskException;
use GabrielAndy\Coindesk\Exceptions\HttpException;
use GabrielAndy\Coindesk\Exceptions\UnsupportedCurrencyCode;
use GuzzleHttp\Client;
use GabrielAndy\Coindesk\Exceptions\ErrorsException;
use GuzzleHttp\Exception\GuzzleException;

class Coindesk implements CryptoFiatInterface
class Coindesk
{
/** @var \GuzzleHttp\Client */
/**
* The GuzzleHttpClient instance.
*
* @var \GuzzleHttp\Client
*/
protected $client;

/** @var string */
protected $apiEndpoint;
/**
* The Coindesk API endpoint.
*
* @var string
*/
protected $endpoint;

/**
* Coindesk's supported currencies.
*
* @var array
*/
protected $supportedCurrencies = [
'USD',
'GBP',
'EUR',
];

/**
* Create a new Coindesk instance.
*
* @param \GuzzleHttp\Client $client
* @return void
*/
public function __construct(Client $client)
{
$this->client = $client;
}

public function setAPIUrl(string $apiEndpoint)
/**
* Set the Coindesk's API endpoint being used.
*
* @param \GuzzleHttp\Client $client
* @return void
*/
public function setEndpoint(string $endpoint)
{
$this->apiEndpoint = $apiEndpoint;
$this->endpoint = $endpoint;

return $this;
return $this;
}

/**
* Get the rate of a currency to Bitcoin.
*
* @param string $currencyCode
* @param string $currencyCode
* @return float
*
* @throws \GabrielAndy\Coindesk\Exceptions\UnsupportedCurrencyCode
*/
public function retrieveRate($currencyCode)
{
if (! Helper::isCurrencyCode($currencyCode)) {
throw ErrorsException::customError("Argument passed is not a valid currency code, '{$currencyCode}' given.");
public function retrieveRate($currencyCode)
{
if (! in_array(
strtoupper($currencyCode), $this->supportedCurrencies
)) {
throw new UnsupportedCurrencyCode(
"The currency, '{$currencyCode}' is not supported by Coindesk."
);
}

$exchangeRates = $this->getExchangeRates();

if (! Helper::currencySupport($currencyCode, $this->exchangeRates)) {
throw ErrorsException::customError("{$currencyCode} currency code is not supported by Coindesk.");
}

return $exchangeRates[strtoupper($currencyCode)];
}
}

/**
* Get Bitcoin exchange rates in an associative array.
Expand All @@ -64,7 +99,8 @@ protected function getExchangeRates()
/**
* Set exchange rates.
*
* @param array $exchangeRatesArray
* @param array $exchangeRatesArray
* @return void
*/
protected function setExchangeRates($exchangeRatesArray)
{
Expand All @@ -78,32 +114,36 @@ protected function setExchangeRates($exchangeRatesArray)
*/
protected function retrieveExchangeRates()
{
$exchangeRatesArray = $this->parseToExchangeRatesArray($this->fetchExchangeRates());
$exchangeRatesArray = $this->parseToExchangeRatesArray(
$this->fetchExchangeRates()
);

return $exchangeRatesArray;
}

/**
* Fetch exchange rates json data from API endpoint.
*
* @return string|json
* @return string
*
* @throws \GabrielAndy\Coindesk\Exceptions\CoindeskException
*/
protected function fetchExchangeRates()
{
$response = $this->client->request('GET', $this->apiEndpoint);
try {
$response = $this->client->request('GET', $this->endpoint);

if ($response->getStatusCode() != 200) {
throw ErrorsException::customError("Not OK response received from API endpoint.");
return $response->getBody();
} catch (GuzzleException $e) {
throw new HttpException($e->getMessage());
}

return $response->getBody();
}

/**
* Parse retrieved JSON data to exchange rates associative array.
* i.e. ['BTC' => 1, 'USD' => 4000.00, ...]
*
* @param string|json $rawJsonData
* @param string $rawJsonData
* @return array
*/
protected function parseToExchangeRatesArray($rawJsonData)
Expand All @@ -118,88 +158,88 @@ protected function parseToExchangeRatesArray($rawJsonData)
}

/**
* Convert Bitcoin amount to a specific currency.
* Convert Bitcoin amount to a Coindesk's supported fiat currency.
*
* @param string $currencyCode
* @param string $currencyCode
* @param float $btcAmount
* @return float
* @return string
*
* @throws \GabrielAndy\Coindesk\Exceptions\CoindeskException
*/
public function toCurrency($currencyCode, $btcAmount)
{
public function toFiatCurrency($currencyCode, $btcAmount)
{
if (! is_numeric($btcAmount)) {
throw new CoindeskException("
Amount should be numeric, '{$btcAmount}' given."
);
}

$rate = $this->retrieveRate($currencyCode);

$value = $this->computeCurrencyValue($btcAmount, $rate);

return $this->formatToCurrency($currencyCode, $value);
}
return number_format(
$value,
config('coindesk.btc_fiat_precision'),
'.',
''
);
}

/**
* Compute currency value.
*
* @param float $btcAmount
* @param float $rate
* @param float $btcAmount
* @param float $rate
* @return float
* @throws Jimmerioles\BitcoinCurrencyConverter\Exception\InvalidArgumentException
*/
public function computeCurrencyValue($btcAmount, $rate)
{
if (! is_numeric($btcAmount)) {
throw ErrorsException::customError("Argument \$btcAmount should be numeric, '{$btcAmount}' given.");
}
$rate = is_numeric($rate) ? $rate : (float) $rate;

return $btcAmount * $rate;
}


/**
* Format value based on currency.
*
* @param string $currencyCode
* @param float $value
* @return float
*/
public function formatToCurrency($currencyCode, $value)
{
if (Helper::isCryptoCurrency($currencyCode)) {
return round($value, 8, PHP_ROUND_HALF_UP);
}
if (Helper::isFiatCurrency($currencyCode)) {
return round($value, 2, PHP_ROUND_HALF_UP);
}
throw ErrorsException::customError("Argument \$currencyCode not valid currency code, '{$currencyCode}' given.");
}


/**
* Convert currency amount to Bitcoin.
*
* @param float $amount
* @param string $currency
* @return float
* @param string $currency
* @return string
*/
public function toBtc($amount, $currencyCode)
{
if (! is_numeric($amount)) {
throw new CoindeskException("
Amount should be numeric, '{$amount}' given."
);
}

$rate = $this->retrieveRate($currencyCode);

$value = $this->computeBtcValue($amount, $rate);

return $this->formatToCurrency('BTC', $value);
return number_format(
$value,
config('coindesk.fiat_btc_precision'),
'.',
''
);
}

/**
* Compute Bitcoin value.
*
* @param float $amount
* @param float $rate
* @param float $amount
* @param float $rate
* @return float
* @throws Jimmerioles\BitcoinCurrencyConverter\Exception\InvalidArgumentException
*
* @throws \GabrielAndy\Coindesk\Exceptions\CoindeskException
*/
public function computeBtcValue($amount, $rate)
{
if (! is_numeric($amount)) {
throw ErrorsException::customError("Argument \$amount should be numeric, '{$amount}' given.");
}
$rate = is_numeric($rate) ? $rate : (float) $rate;

return $amount / $rate;
}
}
}
2 changes: 1 addition & 1 deletion src/CoindeskServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function register()

$this->app->bind('coindesk', function ($app) {
return (new Coindesk(new Client))
->setApiURL(config('coindesk.apiUrl'));
->setEndpoint(config('coindesk.endpoint'));
});
}
}

0 comments on commit dba172c

Please sign in to comment.