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,7 +27,7 @@ class Updater
/** /**
* Execute syncModel for each file * Execute syncModel for each file
* *
* @return void * @return mixed
*/ */
public function run() public function run()
{ {
@@ -37,7 +42,9 @@ 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)
{ {
@@ -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,8 +89,9 @@ 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)
{ {
@@ -98,7 +108,9 @@ 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)
{ {
@@ -107,7 +119,7 @@ class Updater
}); });
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) {
@@ -119,6 +131,7 @@ 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)
@@ -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,6 +197,7 @@ 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)
@@ -201,10 +219,11 @@ 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)) {