Allows for user provided path option

This commit is contained in:
Jani Gyllenberg
2019-01-22 22:28:27 -05:00
parent b92b149846
commit 8dbdeb3e66
2 changed files with 32 additions and 7 deletions

View File

@@ -7,14 +7,16 @@ use Illuminate\Console\Command;
class Sync extends Command class Sync extends Command
{ {
protected $signature = 'data:sync'; protected $signature = 'data:sync {--path=}';
protected $description = 'Update Models with respective sync data files'; protected $description = 'Update Models with respective sync data files';
public function handle() public function handle()
{ {
$path = $this->option('path');
$this->info('Updating Models with sync data files'); $this->info('Updating Models with sync data files');
(new Updater)->run(); (new Updater($path))->run();
$this->comment('Data sync completed'); $this->comment('Data sync completed');
} }
} }

View File

@@ -10,10 +10,12 @@ class Updater
/** /**
* Get files in sync directory * Get files in sync directory
*
* @param string|null $path
*/ */
public function __construct() public function __construct($path = null)
{ {
$this->files = Storage::disk('sync')->files(); $this->files = Storage::files($this->getDirectory($path));
} }
/** /**
@@ -23,9 +25,11 @@ class Updater
*/ */
public function run() public function run()
{ {
collect($this->files)->each(function($file) { $records = collect($this->files)->map(function($file) {
$this->syncModel($file); return $this->syncModel($file);
}); });
return $records;
} }
/** /**
@@ -39,12 +43,31 @@ class Updater
$model = $this->getModel($file); $model = $this->getModel($file);
$records = $this->getRecords($file); $records = $this->getRecords($file);
return $records->map(function($record) use ($model) { $records->each(function($record) use ($model) {
$criteria = $this->getCriteria($record); $criteria = $this->getCriteria($record);
$values = $this->getValues($record); $values = $this->getValues($record);
return $model::updateOrCreate($criteria, $values); return $model::updateOrCreate($criteria, $values);
}); });
return $records;
}
/**
* Get sync files directory path
*
* @param object $record
* @return array
*/
protected function getDirectory($path)
{
$directory = $path ?? config('data-sync.path', base_path('sync'));
if(!file_exists($directory)) {
throw new \Exception("Specified sync file directory does not exist");
}
return $directory;
} }
/** /**