Merge pull request #10 from vicgonvt/master

Refactor to custom exceptions & minor code style fixes
This commit is contained in:
Jani Gyllenberg
2019-01-28 20:58:43 -05:00
committed by GitHub
5 changed files with 87 additions and 30 deletions

View File

@@ -8,7 +8,9 @@
"email": "jani@marketdistinctly.com" "email": "jani@marketdistinctly.com"
} }
], ],
"require": {}, "require": {
"ext-json": "*"
},
"require-dev": { "require-dev": {
"orchestra/testbench": "^3.7" "orchestra/testbench": "^3.7"
}, },

View File

@@ -0,0 +1,10 @@
<?php
namespace distinctm\LaravelDataSync\Exceptions;
use Exception;
class FileDirectoryNotFoundException extends Exception
{
protected $message = 'Specified sync file directory does not exist';
}

View File

@@ -0,0 +1,10 @@
<?php
namespace distinctm\LaravelDataSync\Exceptions;
use Exception;
class NoCriteriaException extends Exception
{
protected $message = 'No criteria/attributes detected';
}

View File

@@ -0,0 +1,16 @@
<?php
namespace distinctm\LaravelDataSync\Exceptions;
use Exception;
use Throwable;
class NoRecordsInvalidJSONException extends Exception
{
public function __construct(string $message = "", int $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
$this->message = "No records or invalid JSON for {$message} model.";
}
}

View File

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