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"
}
],
"require": {},
"require": {
"ext-json": "*"
},
"require-dev": {
"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;
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 @@ class Updater
/**
* 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 @@ class Updater
* 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 @@ class Updater
/**
* 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 @@ class Updater
* 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 @@ class Updater
* 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 @@ class Updater
* 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 @@ class Updater
/**
* Returns model name for file
*
* @param string $file
* @param string $name
*
* @return string
*/
protected function getModel(string $name)
@@ -151,14 +165,16 @@ class Updater
* 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 @@ class Updater
* Check if column is criteria for a condition match
*
* @param string $key
*
* @return boolean
*/
protected function isCriteria($key)
@@ -180,15 +197,16 @@ class Updater
*
* @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 @@ class Updater
/**
* 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);
}