diff --git a/composer.json b/composer.json index 94c839e..d35178a 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,9 @@ "email": "jani@marketdistinctly.com" } ], - "require": {}, + "require": { + "ext-json": "*" + }, "require-dev": { "orchestra/testbench": "^3.7" }, diff --git a/src/Exceptions/FileDirectoryNotFoundException.php b/src/Exceptions/FileDirectoryNotFoundException.php new file mode 100644 index 0000000..ba47abd --- /dev/null +++ b/src/Exceptions/FileDirectoryNotFoundException.php @@ -0,0 +1,10 @@ +message = "No records or invalid JSON for {$message} model."; + } +} \ No newline at end of file diff --git a/src/Updater.php b/src/Updater.php index 8436524..e83ad4e 100644 --- a/src/Updater.php +++ b/src/Updater.php @@ -2,16 +2,21 @@ namespace distinctm\LaravelDataSync; +use distinctm\LaravelDataSync\Exceptions\FileDirectoryNotFoundException; +use distinctm\LaravelDataSync\Exceptions\NoCriteriaException; +use distinctm\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\File; -use Illuminate\Support\Facades\Schema; class Updater { - /** * Get files in sync directory * * @param string|null $path + * @param string|null $model + * + * @throws \distinctm\LaravelDataSync\Exceptions\FileDirectoryNotFoundException */ public function __construct($path = null, $model = null) { @@ -22,11 +27,11 @@ public function __construct($path = null, $model = null) /** * Execute syncModel for each file * - * @return void + * @return mixed */ public function run() { - $records = collect($this->files)->map(function($file) { + $records = collect($this->files)->map(function ($file) { return $this->syncModel($file); }); @@ -37,14 +42,16 @@ public function run() * Parse each record for criteria/values and update/create model * * @param string $file + * * @return \Illuminate\Support\Collection + * @throws \distinctm\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException */ protected function syncModel(string $file) { $model = $this->getModel($file); $records = $this->getRecords($file); - $records->each(function($record) use ($model) { + $records->each(function ($record) use ($model) { $criteria = $this->resolveObjects( $this->getCriteria($record) ); @@ -62,15 +69,17 @@ protected function syncModel(string $file) /** * Get directory path for sync files * - * @param object $record + * @param $path + * * @return array + * @throws \distinctm\LaravelDataSync\Exceptions\FileDirectoryNotFoundException */ 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"); + if (!file_exists($directory)) { + throw new FileDirectoryNotFoundException; } return $directory; @@ -80,16 +89,17 @@ protected function getDirectory($path) * Get list of files in directory * * @param string $directory - * @param string\null $model - * @return array + * @param string|null $model + * + * @return array|string */ protected function getFiles(string $directory, $model) { - if($model) { + if ($model) { return $directory . '/' . $model . '.json'; } - return collect(File::files($directory))->map(function($path) { + return collect(File::files($directory))->map(function ($path) { return $path->getPathname(); })->toArray(); } @@ -98,19 +108,21 @@ protected function getFiles(string $directory, $model) * Filter record criteria * * @param object $record + * * @return array + * @throws \distinctm\LaravelDataSync\Exceptions\NoCriteriaException */ protected function getCriteria(object $record) { - $criteria = collect($record)->filter(function($value, $key) { + $criteria = collect($record)->filter(function ($value, $key) { return $this->isCriteria($key); }); - if($criteria->count() == 0) { - throw new \Exception("No criteria/attributes detected"); + if ($criteria->count() == 0) { + throw new NoCriteriaException; } - return $criteria->mapWithKeys(function($value, $key) { + return $criteria->mapWithKeys(function ($value, $key) { return [substr($key, 1) => $value]; }); } @@ -119,16 +131,17 @@ protected function getCriteria(object $record) * Filter record values * * @param object $record + * * @return array */ protected function getValues(object $record) { - return collect($record)->reject(function($value, $key) { - if($this->isCriteria($key)) { + return collect($record)->reject(function ($value, $key) { + if ($this->isCriteria($key)) { return true; } - if(empty($value)) { + if (empty($value)) { return true; } @@ -139,7 +152,8 @@ protected function getValues(object $record) /** * Returns model name for file * - * @param string $file + * @param string $name + * * @return string */ protected function getModel(string $name) @@ -151,14 +165,16 @@ protected function getModel(string $name) * Parses JSON from file and returns collection * * @param string $file + * * @return \Illuminate\Support\Collection + * @throws \distinctm\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException */ protected function getRecords(string $file) { $records = collect(json_decode(File::get($file))); - if($records->isEmpty()) { - throw new \Exception("No records or invalid JSON for {$file} model"); + if ($records->isEmpty()) { + throw new NoRecordsInvalidJSONException($file); } return $records; @@ -168,6 +184,7 @@ protected function getRecords(string $file) * Check if column is criteria for a condition match * * @param string $key + * * @return boolean */ protected function isCriteria($key) @@ -180,15 +197,16 @@ protected function isCriteria($key) * * @param string $key * @param object $values + * * @return array */ protected function resolveId(string $key, object $values) { $model = $this->getModel($key); - - $values = collect($values)->mapWithKeys(function($value, $column) { - if(is_object($value)) { + $values = collect($values)->mapWithKeys(function ($value, $column) { + + if (is_object($value)) { return $this->resolveId($column, $value); } @@ -201,13 +219,14 @@ protected function resolveId(string $key, object $values) /** * Detect nested objects and resolve them * - * @param \Illuminate\Support\Collection $records + * @param \Illuminate\Support\Collection $record + * * @return array */ - protected function resolveObjects(\Illuminate\Support\Collection $record) + protected function resolveObjects(Collection $record) { - return $record->mapWithKeys(function($value, $key) { - if(is_object($value)) { + return $record->mapWithKeys(function ($value, $key) { + if (is_object($value)) { return $this->resolveId($key, $value); }