Skip to content

Commit cc39f9e

Browse files
committed
Add free API ExchangeRatesApi.io
1 parent 27b2b00 commit cc39f9e

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/vendor
22
composer.phar
33
composer.lock
4-
.DS_Store
4+
.DS_Store
5+
.idea

src/Console/Update.php

+40
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Update extends Command
1414
* @var string
1515
*/
1616
protected $signature = 'currency:update
17+
{--e|exchangeratesapi : Get rates from ExchangeRatesApi.io}
1718
{--o|openexchangerates : Get rates from OpenExchangeRates.org}
1819
{--g|google : Get rates from Google Finance}';
1920

@@ -45,6 +46,7 @@ public function __construct()
4546
* Execute the console command for Laravel 5.4 and below
4647
*
4748
* @return void
49+
* @throws \Exception
4850
*/
4951
public function fire()
5052
{
@@ -55,12 +57,18 @@ public function fire()
5557
* Execute the console command.
5658
*
5759
* @return void
60+
* @throws \Exception
5861
*/
5962
public function handle()
6063
{
6164
// Get Settings
6265
$defaultCurrency = $this->currency->config('default');
6366

67+
if ($this->input->getOption('exchangeratesapi')) {
68+
// Get rates from exchangeratesapi
69+
return $this->updateFromExchangeRatesApi($defaultCurrency);
70+
}
71+
6472
if ($this->input->getOption('google')) {
6573
// Get rates from google
6674
return $this->updateFromGoogle($defaultCurrency);
@@ -78,11 +86,43 @@ public function handle()
7886
}
7987
}
8088

89+
/**
90+
* Fetch rates from the API
91+
*
92+
* @param $defaultCurrency
93+
*/
94+
private function updateFromExchangeRatesApi($defaultCurrency)
95+
{
96+
$this->info('Updating currency exchange rates from ExchangeRatesApi.io...');
97+
98+
// Make request
99+
$content = json_decode($this->request("https://api.exchangeratesapi.io/latest?base={$defaultCurrency}"));
100+
101+
// Error getting content?
102+
if (isset($content->error)) {
103+
$this->error($content->description);
104+
105+
return;
106+
}
107+
108+
// Update each rate
109+
foreach ($content->rates as $code => $value) {
110+
$this->currency->getDriver()->update($code, [
111+
'exchange_rate' => $value,
112+
]);
113+
}
114+
115+
$this->currency->clearCache();
116+
117+
$this->info('Update!');
118+
}
119+
81120
/**
82121
* Fetch rates from the API
83122
*
84123
* @param $defaultCurrency
85124
* @param $api
125+
* @throws \Exception
86126
*/
87127
private function updateFromOpenExchangeRates($defaultCurrency, $api)
88128
{

0 commit comments

Comments
 (0)