Skip to content

Commit

Permalink
Release v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
znck committed Mar 15, 2016
1 parent 8b10c12 commit 1bec2d0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
build
.php_cs.cache
# Created by .ignore support plugin (hsz.mobi)
### Linux template
*~
Expand Down
21 changes: 14 additions & 7 deletions src/UpdateCountriesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Symfony\Component\Console\Output\OutputInterface;

class UpdateCountriesCommand extends Command
{
Expand Down Expand Up @@ -66,7 +67,11 @@ public function __construct(Filesystem $files, Application $app)
$this->loader = new FileLoader($files, dirname(__DIR__).'/data');

$config = $app->make('config');
$this->countries = $config->get('countries');
$this->countries = $config->get('countries.countries');

if (!$this->files->isDirectory(dirname(storage_path(self::INSTALL_HISTORY)))) {
$this->files->makeDirectory(dirname(storage_path(self::INSTALL_HISTORY)), 0755, true);
}

if ($this->files->exists(storage_path(self::INSTALL_HISTORY))) {
$this->hash = $this->files->get(storage_path(self::INSTALL_HISTORY));
Expand All @@ -80,8 +85,6 @@ public function __construct(Filesystem $files, Application $app)
*/
public function handle()
{
$countries = $this->files->files($this->path);

$countries = [];

$data = $this->loader->load('en');
Expand All @@ -93,19 +96,20 @@ public function handle()
}

$countries = Collection::make($countries);

$hash = md5($countries->toJson());

if ($hash === $this->hash) {
$this->line("No new country.");
return false;
}

$countryCodes = $countries->pluck('code');

$countryCodes = $countries->pluck('code')->unique();

$existingCountryIDs = Collection::make(
DB::table($this->countries)->whereIn('code', $countryCodes)->pluck('id', 'code')
);

$countries->map(
function ($item) use ($existingCountryIDs) {
if ($existingCountryIDs->has($item['code'])) {
Expand All @@ -128,14 +132,17 @@ function () use ($countries, $hash) {
$update = Collection::make($countries->get('update'));

foreach ($create->chunk(static::QUERY_LIMIT) as $entries) {
DB::table($this->countries)->insert($entries);
DB::table($this->countries)->insert($entries->toArray());
}

foreach ($update->chunk(static::QUERY_LIMIT) as $entries) {
DB::table($this->countries)->update($entries);
DB::table($this->countries)->update($entries->toArray());
}
$this->line("{$create->count()} countries created. {$update->count()} countries updated.");
$this->files->put(storage_path(static::INSTALL_HISTORY), $hash);
}
);

return true;
}
}

0 comments on commit 1bec2d0

Please sign in to comment.