Changeset View
Changeset View
Standalone View
Standalone View
src/app/Console/Commands/Data/Import/CountriesCommand.php
- This file was moved from src/app/Console/Commands/DataCountries.php.
<?php | <?php | ||||
namespace App\Console\Commands; | namespace App\Console\Commands\Data\Import; | ||||
use Illuminate\Console\Command; | use App\Console\Command; | ||||
use Carbon\Carbon; | |||||
class DataCountries extends Command | class CountriesCommand extends Command | ||||
{ | { | ||||
private $currency_fixes = [ | private $currency_fixes = [ | ||||
// Country code => currency | // Country code => currency | ||||
'LT' => 'EUR', | 'LT' => 'EUR', | ||||
]; | ]; | ||||
/** | /** | ||||
* The name and signature of the console command. | * The name and signature of the console command. | ||||
* | * | ||||
* @var string | * @var string | ||||
*/ | */ | ||||
protected $signature = 'data:countries'; | protected $signature = 'data:import:countries'; | ||||
/** | /** | ||||
* The console command description. | * The console command description. | ||||
* | * | ||||
* @var string | * @var string | ||||
*/ | */ | ||||
protected $description = 'Fetches countries map from wikipedia'; | protected $description = 'Fetches countries map from country.io'; | ||||
/** | /** | ||||
* Execute the console command. | * Execute the console command. | ||||
* | * | ||||
* @return mixed | * @return mixed | ||||
*/ | */ | ||||
public function handle() | public function handle() | ||||
{ | { | ||||
$today = Carbon::now()->toDateString(); | |||||
$countries = []; | $countries = []; | ||||
$currencies = []; | $currencies = []; | ||||
$currencies_url = 'http://country.io/currency.json'; | $currencySource = 'http://country.io/currency.json'; | ||||
$countries_url = 'http://country.io/names.json'; | $countrySource = 'http://country.io/names.json'; | ||||
$this->info("Fetching currencies from $currencies_url..."); | |||||
// fetch currency table and create an index by country page url | // | ||||
$currencies_json = file_get_contents($currencies_url); | // countries | ||||
// | |||||
$file = storage_path("countries-{$today}.json"); | |||||
if (!$currencies_json) { | $this->info("Retrieving {$countrySource}..."); | ||||
$this->error("Failed to fetch currencies"); | |||||
return; | |||||
} | |||||
$this->info("Fetching countries from $countries_url..."); | \App\Utils::downloadFile($countrySource, $file); | ||||
$countries_json = file_get_contents($countries_url); | $countryJson = file_get_contents($file); | ||||
if (!$countries_json) { | if (!$countryJson) { | ||||
$this->error("Failed to fetch countries"); | $this->error("Failed to fetch countries"); | ||||
return; | return 1; | ||||
} | } | ||||
$currencies = json_decode($currencies_json, true); | $countries = json_decode($countryJson, true); | ||||
$countries = json_decode($countries_json, true); | |||||
if (!is_array($countries) || empty($countries)) { | if (!is_array($countries) || empty($countries)) { | ||||
$this->error("Invalid countries data"); | $this->error("Invalid countries data"); | ||||
return 1; | |||||
} | |||||
// | |||||
// currencies | |||||
// | |||||
$file = storage_path("currencies-{$today}.json"); | |||||
$this->info("Retrieving {$currencySource}..."); | |||||
\App\Utils::downloadFile($currencySource, $file); | |||||
// fetch currency table and create an index by country page url | |||||
$currencyJson = file_get_contents($file); | |||||
if (!$currencyJson) { | |||||
$this->error("Failed to fetch currencies"); | |||||
return; | return; | ||||
} | } | ||||
$currencies = json_decode($currencyJson, true); | |||||
if (!is_array($currencies) || empty($currencies)) { | if (!is_array($currencies) || empty($currencies)) { | ||||
$this->error("Invalid currencies data"); | $this->error("Invalid currencies data"); | ||||
return; | return 1; | ||||
} | } | ||||
// | |||||
// export | |||||
// | |||||
$file = resource_path('countries.php'); | $file = resource_path('countries.php'); | ||||
$this->info("Generating resource file $file..."); | $this->info("Generating resource file $file..."); | ||||
asort($countries); | asort($countries); | ||||
$out = "<?php return [\n"; | $out = "<?php return [\n"; | ||||
foreach ($countries as $code => $name) { | foreach ($countries as $code => $name) { | ||||
$currency = $currencies[$code] ?? null; | $currency = $currencies[$code] ?? null; | ||||
if (!empty($this->currency_fixes[$code])) { | if (!empty($this->currency_fixes[$code])) { | ||||
$currency = $this->currency_fixes[$code]; | $currency = $this->currency_fixes[$code]; | ||||
} | } | ||||
if (!$currency) { | if (!$currency) { | ||||
$this->error("Unknown currency for {$name} ({$code}). Skipped."); | $this->warn("Unknown currency for {$name} ({$code}). Skipped."); | ||||
continue; | continue; | ||||
} | } | ||||
$out .= sprintf(" '%s' => ['%s','%s'],\n", $code, $currency, addslashes($name)); | $out .= sprintf(" '%s' => ['%s','%s'],\n", $code, $currency, addslashes($name)); | ||||
} | } | ||||
$out .= "];\n"; | $out .= "];\n"; | ||||
file_put_contents($file, $out); | file_put_contents($file, $out); | ||||
} | } | ||||
} | } |