Skip to content

Commit

Permalink
Merge pull request #132 from Leonigas/exchange_rates_api
Browse files Browse the repository at this point in the history
Add free API ExchangeRatesApi.io
  • Loading branch information
Torann authored Sep 21, 2020
2 parents 27b2b00 + cc39f9e commit 162143a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/vendor
composer.phar
composer.lock
.DS_Store
.DS_Store
.idea
40 changes: 40 additions & 0 deletions src/Console/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Update extends Command
* @var string
*/
protected $signature = 'currency:update
{--e|exchangeratesapi : Get rates from ExchangeRatesApi.io}
{--o|openexchangerates : Get rates from OpenExchangeRates.org}
{--g|google : Get rates from Google Finance}';

Expand Down Expand Up @@ -45,6 +46,7 @@ public function __construct()
* Execute the console command for Laravel 5.4 and below
*
* @return void
* @throws \Exception
*/
public function fire()
{
Expand All @@ -55,12 +57,18 @@ public function fire()
* Execute the console command.
*
* @return void
* @throws \Exception
*/
public function handle()
{
// Get Settings
$defaultCurrency = $this->currency->config('default');

if ($this->input->getOption('exchangeratesapi')) {
// Get rates from exchangeratesapi
return $this->updateFromExchangeRatesApi($defaultCurrency);
}

if ($this->input->getOption('google')) {
// Get rates from google
return $this->updateFromGoogle($defaultCurrency);
Expand All @@ -78,11 +86,43 @@ public function handle()
}
}

/**
* Fetch rates from the API
*
* @param $defaultCurrency
*/
private function updateFromExchangeRatesApi($defaultCurrency)
{
$this->info('Updating currency exchange rates from ExchangeRatesApi.io...');

// Make request
$content = json_decode($this->request("https://api.exchangeratesapi.io/latest?base={$defaultCurrency}"));

// Error getting content?
if (isset($content->error)) {
$this->error($content->description);

return;
}

// Update each rate
foreach ($content->rates as $code => $value) {
$this->currency->getDriver()->update($code, [
'exchange_rate' => $value,
]);
}

$this->currency->clearCache();

$this->info('Update!');
}

/**
* Fetch rates from the API
*
* @param $defaultCurrency
* @param $api
* @throws \Exception
*/
private function updateFromOpenExchangeRates($defaultCurrency, $api)
{
Expand Down

0 comments on commit 162143a

Please sign in to comment.