Skip to content

Commit 692e70e

Browse files
authored
Merge pull request #10 from vicgonvt/master
Refactor to custom exceptions & minor code style fixes
2 parents ab17916 + 7d09b42 commit 692e70e

File tree

5 files changed

+87
-30
lines changed

5 files changed

+87
-30
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
"email": "jani@marketdistinctly.com"
99
}
1010
],
11-
"require": {},
11+
"require": {
12+
"ext-json": "*"
13+
},
1214
"require-dev": {
1315
"orchestra/testbench": "^3.7"
1416
},
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace distinctm\LaravelDataSync\Exceptions;
4+
5+
use Exception;
6+
7+
class FileDirectoryNotFoundException extends Exception
8+
{
9+
protected $message = 'Specified sync file directory does not exist';
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace distinctm\LaravelDataSync\Exceptions;
4+
5+
use Exception;
6+
7+
class NoCriteriaException extends Exception
8+
{
9+
protected $message = 'No criteria/attributes detected';
10+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace distinctm\LaravelDataSync\Exceptions;
4+
5+
use Exception;
6+
use Throwable;
7+
8+
class NoRecordsInvalidJSONException extends Exception
9+
{
10+
public function __construct(string $message = "", int $code = 0, Throwable $previous = null)
11+
{
12+
parent::__construct($message, $code, $previous);
13+
14+
$this->message = "No records or invalid JSON for {$message} model.";
15+
}
16+
}

src/Updater.php

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@
22

33
namespace distinctm\LaravelDataSync;
44

5+
use distinctm\LaravelDataSync\Exceptions\FileDirectoryNotFoundException;
6+
use distinctm\LaravelDataSync\Exceptions\NoCriteriaException;
7+
use distinctm\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException;
8+
use Illuminate\Support\Collection;
59
use Illuminate\Support\Facades\File;
6-
use Illuminate\Support\Facades\Schema;
710

811
class Updater
912
{
10-
1113
/**
1214
* Get files in sync directory
1315
*
1416
* @param string|null $path
17+
* @param string|null $model
18+
*
19+
* @throws \distinctm\LaravelDataSync\Exceptions\FileDirectoryNotFoundException
1520
*/
1621
public function __construct($path = null, $model = null)
1722
{
@@ -22,11 +27,11 @@ public function __construct($path = null, $model = null)
2227
/**
2328
* Execute syncModel for each file
2429
*
25-
* @return void
30+
* @return mixed
2631
*/
2732
public function run()
2833
{
29-
$records = collect($this->files)->map(function($file) {
34+
$records = collect($this->files)->map(function ($file) {
3035
return $this->syncModel($file);
3136
});
3237

@@ -37,14 +42,16 @@ public function run()
3742
* Parse each record for criteria/values and update/create model
3843
*
3944
* @param string $file
45+
*
4046
* @return \Illuminate\Support\Collection
47+
* @throws \distinctm\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException
4148
*/
4249
protected function syncModel(string $file)
4350
{
4451
$model = $this->getModel($file);
4552
$records = $this->getRecords($file);
4653

47-
$records->each(function($record) use ($model) {
54+
$records->each(function ($record) use ($model) {
4855
$criteria = $this->resolveObjects(
4956
$this->getCriteria($record)
5057
);
@@ -62,15 +69,17 @@ protected function syncModel(string $file)
6269
/**
6370
* Get directory path for sync files
6471
*
65-
* @param object $record
72+
* @param $path
73+
*
6674
* @return array
75+
* @throws \distinctm\LaravelDataSync\Exceptions\FileDirectoryNotFoundException
6776
*/
6877
protected function getDirectory($path)
6978
{
7079
$directory = $path ?? config('data-sync.path', base_path('sync'));
7180

72-
if(!file_exists($directory)) {
73-
throw new \Exception("Specified sync file directory does not exist");
81+
if (!file_exists($directory)) {
82+
throw new FileDirectoryNotFoundException;
7483
}
7584

7685
return $directory;
@@ -80,16 +89,17 @@ protected function getDirectory($path)
8089
* Get list of files in directory
8190
*
8291
* @param string $directory
83-
* @param string\null $model
84-
* @return array
92+
* @param string|null $model
93+
*
94+
* @return array|string
8595
*/
8696
protected function getFiles(string $directory, $model)
8797
{
88-
if($model) {
98+
if ($model) {
8999
return $directory . '/' . $model . '.json';
90100
}
91101

92-
return collect(File::files($directory))->map(function($path) {
102+
return collect(File::files($directory))->map(function ($path) {
93103
return $path->getPathname();
94104
})->toArray();
95105
}
@@ -98,19 +108,21 @@ protected function getFiles(string $directory, $model)
98108
* Filter record criteria
99109
*
100110
* @param object $record
111+
*
101112
* @return array
113+
* @throws \distinctm\LaravelDataSync\Exceptions\NoCriteriaException
102114
*/
103115
protected function getCriteria(object $record)
104116
{
105-
$criteria = collect($record)->filter(function($value, $key) {
117+
$criteria = collect($record)->filter(function ($value, $key) {
106118
return $this->isCriteria($key);
107119
});
108120

109-
if($criteria->count() == 0) {
110-
throw new \Exception("No criteria/attributes detected");
121+
if ($criteria->count() == 0) {
122+
throw new NoCriteriaException;
111123
}
112124

113-
return $criteria->mapWithKeys(function($value, $key) {
125+
return $criteria->mapWithKeys(function ($value, $key) {
114126
return [substr($key, 1) => $value];
115127
});
116128
}
@@ -119,16 +131,17 @@ protected function getCriteria(object $record)
119131
* Filter record values
120132
*
121133
* @param object $record
134+
*
122135
* @return array
123136
*/
124137
protected function getValues(object $record)
125138
{
126-
return collect($record)->reject(function($value, $key) {
127-
if($this->isCriteria($key)) {
139+
return collect($record)->reject(function ($value, $key) {
140+
if ($this->isCriteria($key)) {
128141
return true;
129142
}
130143

131-
if(empty($value)) {
144+
if (empty($value)) {
132145
return true;
133146
}
134147

@@ -139,7 +152,8 @@ protected function getValues(object $record)
139152
/**
140153
* Returns model name for file
141154
*
142-
* @param string $file
155+
* @param string $name
156+
*
143157
* @return string
144158
*/
145159
protected function getModel(string $name)
@@ -151,14 +165,16 @@ protected function getModel(string $name)
151165
* Parses JSON from file and returns collection
152166
*
153167
* @param string $file
168+
*
154169
* @return \Illuminate\Support\Collection
170+
* @throws \distinctm\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException
155171
*/
156172
protected function getRecords(string $file)
157173
{
158174
$records = collect(json_decode(File::get($file)));
159175

160-
if($records->isEmpty()) {
161-
throw new \Exception("No records or invalid JSON for {$file} model");
176+
if ($records->isEmpty()) {
177+
throw new NoRecordsInvalidJSONException($file);
162178
}
163179

164180
return $records;
@@ -168,6 +184,7 @@ protected function getRecords(string $file)
168184
* Check if column is criteria for a condition match
169185
*
170186
* @param string $key
187+
*
171188
* @return boolean
172189
*/
173190
protected function isCriteria($key)
@@ -180,15 +197,16 @@ protected function isCriteria($key)
180197
*
181198
* @param string $key
182199
* @param object $values
200+
*
183201
* @return array
184202
*/
185203
protected function resolveId(string $key, object $values)
186204
{
187205
$model = $this->getModel($key);
188-
189-
$values = collect($values)->mapWithKeys(function($value, $column) {
190206

191-
if(is_object($value)) {
207+
$values = collect($values)->mapWithKeys(function ($value, $column) {
208+
209+
if (is_object($value)) {
192210
return $this->resolveId($column, $value);
193211
}
194212

@@ -201,13 +219,14 @@ protected function resolveId(string $key, object $values)
201219
/**
202220
* Detect nested objects and resolve them
203221
*
204-
* @param \Illuminate\Support\Collection $records
222+
* @param \Illuminate\Support\Collection $record
223+
*
205224
* @return array
206225
*/
207-
protected function resolveObjects(\Illuminate\Support\Collection $record)
226+
protected function resolveObjects(Collection $record)
208227
{
209-
return $record->mapWithKeys(function($value, $key) {
210-
if(is_object($value)) {
228+
return $record->mapWithKeys(function ($value, $key) {
229+
if (is_object($value)) {
211230
return $this->resolveId($key, $value);
212231
}
213232

0 commit comments

Comments
 (0)