Switches to File facade, resolves objects for both criteria and values

- File facade allows access to directories outside of filesystem disks
This commit is contained in:
distinctm
2019-01-23 13:25:45 -05:00
parent 5ec1146583
commit 82185c0e80

View File

@@ -2,7 +2,7 @@
namespace distinctm\LaravelDataSync; namespace distinctm\LaravelDataSync;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
class Updater class Updater
@@ -15,7 +15,10 @@ class Updater
*/ */
public function __construct($path = null) public function __construct($path = null)
{ {
$this->files = Storage::files($this->getDirectory($path));
$this->files = $this->getFiles(
$this->getDirectory($path)
);
} }
/** /**
@@ -44,17 +47,19 @@ class Updater
$records = $this->getRecords($file); $records = $this->getRecords($file);
$records->each(function($record) use ($model) { $records->each(function($record) use ($model) {
$record = $this->resolveObjects($record);
$criteria = $this->getCriteria($record); $criteria = $this->getCriteria($record);
$values = $this->getValues($record); $values = $this->getValues($record);
return $model::updateOrCreate($criteria, $values); $model::updateOrCreate($criteria, $values);
}); });
return $records; return $records;
} }
/** /**
* Get sync files directory path * Get directory path for sync files
* *
* @param object $record * @param object $record
* @return array * @return array
@@ -70,15 +75,28 @@ class Updater
return $directory; return $directory;
} }
/**
* Get list of files in directory
*
* @param string $directory
* @return void
*/
protected function getFiles(string $directory)
{
return collect(File::files($directory))->map(function($path) {
return $path->getPathname();
})->toArray();
}
/** /**
* Filter record criteria * Filter record criteria
* *
* @param object $record * @param \Illuminate\Support\Collection $record
* @return array * @return array
*/ */
protected function getCriteria(object $record) protected function getCriteria(\Illuminate\Support\Collection $record)
{ {
$criteria = collect($record)->filter(function($value, $key) { $criteria = $record->filter(function($value, $key) {
return $this->isCriteria($key); return $this->isCriteria($key);
}); });
@@ -94,12 +112,12 @@ class Updater
/** /**
* Filter record values * Filter record values
* *
* @param array $record * @param \Illuminate\Support\Collection $record
* @return array * @return array
*/ */
protected function getValues($record) protected function getValues(\Illuminate\Support\Collection $record)
{ {
$values = collect($record)->reject(function($value, $key) { return $record->reject(function($value, $key) {
if($this->isCriteria($key)) { if($this->isCriteria($key)) {
return true; return true;
} }
@@ -109,14 +127,6 @@ class Updater
} }
return false; return false;
});
return $values->mapWithKeys(function($value, $key) {
if(is_object($value)) {
return $this->resolveId($key, $value);
}
return [$key => $value];
})->toArray(); })->toArray();
} }
@@ -132,14 +142,20 @@ class Updater
} }
/** /**
* Parses file to JSON and returns collection * Parses JSON from file and returns collection
* *
* @param string $file * @param string $file
* @return \Illuminate\Support\Collection * @return \Illuminate\Support\Collection
*/ */
protected function getRecords(string $file) protected function getRecords(string $file)
{ {
return collect(json_decode(Storage::disk('sync')->get($file))); $records = collect(json_decode(File::get($file)));
if($records->isEmpty()) {
throw new \Exception("No records or invalid JSON for {$file} model");
}
return $records;
} }
/** /**
@@ -163,7 +179,7 @@ class Updater
protected function resolveId(string $key, object $values) protected function resolveId(string $key, object $values)
{ {
$model = $this->getModel($key); $model = $this->getModel($key);
$values = collect($values)->mapWithKeys(function($value, $column) { $values = collect($values)->mapWithKeys(function($value, $column) {
if(is_object($value)) { if(is_object($value)) {
return $this->resolveId($column, $value); return $this->resolveId($column, $value);
@@ -175,4 +191,21 @@ class Updater
return [$key . '_id' => $model::where($values)->first()->id]; return [$key . '_id' => $model::where($values)->first()->id];
} }
/**
* Detect nested objects and resolve them
*
* @param object $records
* @return \Illuminate\Support\Collection
*/
protected function resolveObjects(object $record)
{
return collect($record)->mapWithKeys(function($value, $key) {
if(is_object($value)) {
return $this->resolveId($key, $value);
}
return [$key => $value];
});
}
} }