diff --git a/config/currency.php b/config/currency.php index 0f580af..9e2b3e3 100644 --- a/config/currency.php +++ b/config/currency.php @@ -17,15 +17,15 @@ /* |-------------------------------------------------------------------------- - | API Key for OpenExchangeRates.org + | API Keys for different services |-------------------------------------------------------------------------- | - | Only required if you with to use the Open Exchange Rates api. You can - | always just use Yahoo, the current default. - | */ - 'api_key' => '', + 'api_key' => [ + 'openexchangerates' => '', // OpenExchangeRates.org + 'fixer' => '', // fixer.io + ], /* |-------------------------------------------------------------------------- diff --git a/database/migrations/2013_11_26_161501_create_currency_table.php b/database/migrations/2013_11_26_161501_create_currency_table.php index 7161565..5bfd27a 100644 --- a/database/migrations/2013_11_26_161501_create_currency_table.php +++ b/database/migrations/2013_11_26_161501_create_currency_table.php @@ -10,12 +10,14 @@ class CreateCurrencyTable extends Migration * @var string */ protected $table_name; + protected $table_connection; /** * Create a new migration instance. */ public function __construct() { + $this->table_connection = config('currency.drivers.database.connection'); $this->table_name = config('currency.drivers.database.table'); } @@ -26,7 +28,7 @@ public function __construct() */ public function up() { - Schema::create($this->table_name, function ($table) { + Schema::connection($this->table_connection)->create($this->table_name, function ($table) { $table->increments('id')->unsigned(); $table->string('name'); $table->string('code', 10)->index(); diff --git a/src/Console/Update.php b/src/Console/Update.php index 0ccef1c..ba1db42 100644 --- a/src/Console/Update.php +++ b/src/Console/Update.php @@ -15,7 +15,8 @@ class Update extends Command */ protected $signature = 'currency:update {--o|openexchangerates : Get rates from OpenExchangeRates.org} - {--g|google : Get rates from Google Finance}'; + {--g|google : Get rates from Google Finance} + {--f|fixer : Get rates from fixer.io}'; /** * The console command description. @@ -67,7 +68,7 @@ public function handle() } if ($this->input->getOption('openexchangerates')) { - if (!$api = $this->currency->config('api_key')) { + if (!$api = $this->currency->config('api_key.openexchangerates')) { $this->error('An API key is needed from OpenExchangeRates.org to continue.'); return; @@ -76,6 +77,17 @@ public function handle() // Get rates from OpenExchangeRates return $this->updateFromOpenExchangeRates($defaultCurrency, $api); } + + if ($this->input->getOption('fixer')) { + if (!$api = $this->currency->config('api_key.fixer')) { + $this->error('An API key is needed from fixer.io to continue.'); + + return; + } + + // Get rates + $this->updateFromFixerIO($defaultCurrency, $api); + } } /** @@ -133,18 +145,47 @@ private function updateFromGoogle($defaultCurrency) if (Str::contains($response, 'bld>')) { $data = explode('bld>', $response); $rate = explode($code, $data[1])[0]; - + $this->currency->getDriver()->update($code, [ 'exchange_rate' => $rate, ]); - } - else { + } else { $this->warn('Can\'t update rate for ' . $code); continue; } } } + private function updateFromFixerIO($defaultCurrency, $api) + { + $this->info('Updating currency exchange rates from fixer.io...'); + + // Make request + $content = json_decode($this->request("http://data.fixer.io/api/latest?access_key={$api}&format=1&base={$defaultCurrency}")); + + // Error getting content? + if (isset($content->error)) { + $this->error($content->error->code . ' ' . $content->error->type); + + return; + } + + // Parse timestamp for DB + $timestamp = (new DateTime())->setTimestamp($content->timestamp); + + // Update each rate + foreach ($content->rates as $code => $value) { + $this->currency->getDriver()->update($code, [ + 'exchange_rate' => $value, + 'updated_at' => $timestamp, + ]); + } + + $this->currency->clearCache(); + + $this->info('Update!'); + } + /** * Make the request to the sever. *