@@ -6,38 +6,38 @@ |
||
6 | 6 | |
7 | 7 | class RedirectIfAuthenticated { |
8 | 8 | |
9 | - /** |
|
10 | - * The Guard implementation. |
|
11 | - * |
|
12 | - * @var Guard |
|
13 | - */ |
|
14 | - protected $auth; |
|
9 | + /** |
|
10 | + * The Guard implementation. |
|
11 | + * |
|
12 | + * @var Guard |
|
13 | + */ |
|
14 | + protected $auth; |
|
15 | 15 | |
16 | - /** |
|
17 | - * Create a new filter instance. |
|
18 | - * |
|
19 | - * @param Guard $auth |
|
20 | - */ |
|
21 | - public function __construct(Guard $auth) |
|
22 | - { |
|
23 | - $this->auth = $auth; |
|
24 | - } |
|
16 | + /** |
|
17 | + * Create a new filter instance. |
|
18 | + * |
|
19 | + * @param Guard $auth |
|
20 | + */ |
|
21 | + public function __construct(Guard $auth) |
|
22 | + { |
|
23 | + $this->auth = $auth; |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * Handle an incoming request. |
|
28 | - * |
|
29 | - * @param \Illuminate\Http\Request $request |
|
30 | - * @param \Closure $next |
|
31 | - * @return mixed |
|
32 | - */ |
|
33 | - public function handle($request, Closure $next) |
|
34 | - { |
|
35 | - if ($this->auth->check()) |
|
36 | - { |
|
37 | - return new RedirectResponse(url('/app')); |
|
38 | - } |
|
26 | + /** |
|
27 | + * Handle an incoming request. |
|
28 | + * |
|
29 | + * @param \Illuminate\Http\Request $request |
|
30 | + * @param \Closure $next |
|
31 | + * @return mixed |
|
32 | + */ |
|
33 | + public function handle($request, Closure $next) |
|
34 | + { |
|
35 | + if ($this->auth->check()) |
|
36 | + { |
|
37 | + return new RedirectResponse(url('/app')); |
|
38 | + } |
|
39 | 39 | |
40 | - return $next($request); |
|
41 | - } |
|
40 | + return $next($request); |
|
41 | + } |
|
42 | 42 | |
43 | 43 | } |
@@ -5,45 +5,45 @@ |
||
5 | 5 | |
6 | 6 | class Authenticate { |
7 | 7 | |
8 | - /** |
|
9 | - * The Guard implementation. |
|
10 | - * |
|
11 | - * @var Guard |
|
12 | - */ |
|
13 | - protected $auth; |
|
8 | + /** |
|
9 | + * The Guard implementation. |
|
10 | + * |
|
11 | + * @var Guard |
|
12 | + */ |
|
13 | + protected $auth; |
|
14 | 14 | |
15 | - /** |
|
16 | - * Create a new filter instance. |
|
17 | - * |
|
18 | - * @param Guard $auth |
|
19 | - */ |
|
20 | - public function __construct(Guard $auth) |
|
21 | - { |
|
22 | - $this->auth = $auth; |
|
23 | - } |
|
15 | + /** |
|
16 | + * Create a new filter instance. |
|
17 | + * |
|
18 | + * @param Guard $auth |
|
19 | + */ |
|
20 | + public function __construct(Guard $auth) |
|
21 | + { |
|
22 | + $this->auth = $auth; |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * Handle an incoming request. |
|
27 | - * |
|
28 | - * @param \Illuminate\Http\Request $request |
|
29 | - * @param \Closure $next |
|
30 | - * @return mixed |
|
31 | - */ |
|
32 | - public function handle($request, Closure $next) |
|
33 | - { |
|
34 | - if ($this->auth->guest()) |
|
35 | - { |
|
36 | - if ($request->ajax()) |
|
37 | - { |
|
38 | - return response('Unauthorized.', 401); |
|
39 | - } |
|
40 | - else |
|
41 | - { |
|
42 | - return redirect()->guest('/'); |
|
43 | - } |
|
44 | - } |
|
25 | + /** |
|
26 | + * Handle an incoming request. |
|
27 | + * |
|
28 | + * @param \Illuminate\Http\Request $request |
|
29 | + * @param \Closure $next |
|
30 | + * @return mixed |
|
31 | + */ |
|
32 | + public function handle($request, Closure $next) |
|
33 | + { |
|
34 | + if ($this->auth->guest()) |
|
35 | + { |
|
36 | + if ($request->ajax()) |
|
37 | + { |
|
38 | + return response('Unauthorized.', 401); |
|
39 | + } |
|
40 | + else |
|
41 | + { |
|
42 | + return redirect()->guest('/'); |
|
43 | + } |
|
44 | + } |
|
45 | 45 | |
46 | - return $next($request); |
|
47 | - } |
|
46 | + return $next($request); |
|
47 | + } |
|
48 | 48 | |
49 | 49 | } |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | { |
13 | 13 | $model = $app->make($this->getModelName()); |
14 | 14 | |
15 | - if( !$model instanceof Model ) { |
|
15 | + if (!$model instanceof Model) { |
|
16 | 16 | throw new RepositoryException('Model must be an instance of Illuminate\\Database\\Eloquent\\Model'); |
17 | 17 | } |
18 | 18 | |
@@ -58,7 +58,7 @@ discard block |
||
58 | 58 | |
59 | 59 | public function validateID($id) |
60 | 60 | { |
61 | - if( (bool) preg_match('/^[0-9]{1,10}$/', $id) === false ) |
|
61 | + if ((bool) preg_match('/^[0-9]{1,10}$/', $id) === false) |
|
62 | 62 | { |
63 | 63 | throw new RepositoryException('Parameter must be a positive integer', RepositoryException::INCORRECT_PARAMETER); |
64 | 64 | } |
@@ -17,74 +17,74 @@ discard block |
||
17 | 17 | return 'App\Models\Products'; |
18 | 18 | } |
19 | 19 | |
20 | - public function all() { |
|
20 | + public function all() { |
|
21 | 21 | |
22 | - try { |
|
23 | - return $this->model->group( Session::get('groupID') )->get(); |
|
24 | - } |
|
25 | - catch(\Exception $e) { |
|
26 | - throw new RepositoryException('Database error', RepositoryException::DATABASE_ERROR); |
|
27 | - } |
|
28 | - } |
|
22 | + try { |
|
23 | + return $this->model->group( Session::get('groupID') )->get(); |
|
24 | + } |
|
25 | + catch(\Exception $e) { |
|
26 | + throw new RepositoryException('Database error', RepositoryException::DATABASE_ERROR); |
|
27 | + } |
|
28 | + } |
|
29 | 29 | |
30 | - public function get($id) { |
|
30 | + public function get($id) { |
|
31 | 31 | |
32 | - $this->validateID($id); |
|
32 | + $this->validateID($id); |
|
33 | 33 | |
34 | - try { |
|
35 | - return $this->model->group( Session::get('groupID') )->where('product_id', $id)->first(); |
|
36 | - } |
|
37 | - catch(\Exception $e) { |
|
38 | - throw new RepositoryException('Database error', RepositoryException::DATABASE_ERROR); |
|
39 | - } |
|
40 | - } |
|
34 | + try { |
|
35 | + return $this->model->group( Session::get('groupID') )->where('product_id', $id)->first(); |
|
36 | + } |
|
37 | + catch(\Exception $e) { |
|
38 | + throw new RepositoryException('Database error', RepositoryException::DATABASE_ERROR); |
|
39 | + } |
|
40 | + } |
|
41 | 41 | |
42 | - public function store(array $data) { |
|
42 | + public function store(array $data) { |
|
43 | 43 | |
44 | - $this->validate($data); |
|
44 | + $this->validate($data); |
|
45 | 45 | |
46 | - $product = $this->createModel($data); |
|
46 | + $product = $this->createModel($data); |
|
47 | 47 | |
48 | - try { |
|
49 | - $product->save(); |
|
50 | - } |
|
51 | - catch(\Exception $e) { |
|
52 | - throw new RepositoryException('Database error', RepositoryException::DATABASE_ERROR); |
|
53 | - } |
|
48 | + try { |
|
49 | + $product->save(); |
|
50 | + } |
|
51 | + catch(\Exception $e) { |
|
52 | + throw new RepositoryException('Database error', RepositoryException::DATABASE_ERROR); |
|
53 | + } |
|
54 | 54 | |
55 | - $product->id = DB::getPdo()->lastInsertId(); |
|
56 | - return $product; |
|
57 | - } |
|
55 | + $product->id = DB::getPdo()->lastInsertId(); |
|
56 | + return $product; |
|
57 | + } |
|
58 | 58 | |
59 | - public function update($id, array $data) { |
|
59 | + public function update($id, array $data) { |
|
60 | 60 | |
61 | - $this->validateID($id); |
|
62 | - $this->validate($data, false); |
|
61 | + $this->validateID($id); |
|
62 | + $this->validate($data, false); |
|
63 | 63 | |
64 | - try { |
|
65 | - $product = $this->model->group( Session::get('groupID') )->withTrashed()->find($id); |
|
66 | - } |
|
67 | - catch(\Exception $e) { |
|
68 | - throw new RepositoryException('Database error while fetching'.$e->getMessage(), RepositoryException::DATABASE_ERROR); |
|
69 | - } |
|
64 | + try { |
|
65 | + $product = $this->model->group( Session::get('groupID') )->withTrashed()->find($id); |
|
66 | + } |
|
67 | + catch(\Exception $e) { |
|
68 | + throw new RepositoryException('Database error while fetching'.$e->getMessage(), RepositoryException::DATABASE_ERROR); |
|
69 | + } |
|
70 | 70 | |
71 | - if( $product == null ) |
|
72 | - throw new RepositoryException('Product with ID '.$id.' not found', RepositoryException::RESOURCE_NOT_FOUND); |
|
71 | + if( $product == null ) |
|
72 | + throw new RepositoryException('Product with ID '.$id.' not found', RepositoryException::RESOURCE_NOT_FOUND); |
|
73 | 73 | |
74 | - if( $product->trashed() ) |
|
75 | - throw new RepositoryException('Product with ID '.$id.' has been deleted', RepositoryException::RESOURCE_DENIED); |
|
74 | + if( $product->trashed() ) |
|
75 | + throw new RepositoryException('Product with ID '.$id.' has been deleted', RepositoryException::RESOURCE_DENIED); |
|
76 | 76 | |
77 | - $this->createModel($data, $product); |
|
77 | + $this->createModel($data, $product); |
|
78 | 78 | |
79 | - try { |
|
80 | - $product->save(); |
|
81 | - } |
|
82 | - catch(\Exception $e) { |
|
83 | - throw new RepositoryException('Database error while saving', RepositoryException::DATABASE_ERROR); |
|
84 | - } |
|
79 | + try { |
|
80 | + $product->save(); |
|
81 | + } |
|
82 | + catch(\Exception $e) { |
|
83 | + throw new RepositoryException('Database error while saving', RepositoryException::DATABASE_ERROR); |
|
84 | + } |
|
85 | 85 | |
86 | - return $product; |
|
87 | - } |
|
86 | + return $product; |
|
87 | + } |
|
88 | 88 | |
89 | 89 | public function decrementQuantity($id, $number) { |
90 | 90 | |
@@ -116,32 +116,32 @@ discard block |
||
116 | 116 | return $product->quantity; |
117 | 117 | } |
118 | 118 | |
119 | - public function softDelete($id) { |
|
119 | + public function softDelete($id) { |
|
120 | 120 | |
121 | - $this->validateID($id); |
|
121 | + $this->validateID($id); |
|
122 | 122 | |
123 | - try { |
|
124 | - $product = $this->model->group( Session::get('groupID') )->withTrashed()->find($id); |
|
125 | - } |
|
126 | - catch(\Exception $e) { |
|
127 | - throw new RepositoryException('Database error while fetching', RepositoryException::DATABASE_ERROR); |
|
128 | - } |
|
123 | + try { |
|
124 | + $product = $this->model->group( Session::get('groupID') )->withTrashed()->find($id); |
|
125 | + } |
|
126 | + catch(\Exception $e) { |
|
127 | + throw new RepositoryException('Database error while fetching', RepositoryException::DATABASE_ERROR); |
|
128 | + } |
|
129 | 129 | |
130 | - if( $product == null ) |
|
131 | - throw new RepositoryException('Product with ID'.$id.' not found', RepositoryException::RESOURCE_NOT_FOUND); |
|
130 | + if( $product == null ) |
|
131 | + throw new RepositoryException('Product with ID'.$id.' not found', RepositoryException::RESOURCE_NOT_FOUND); |
|
132 | 132 | |
133 | - if( $product->trashed() ) |
|
134 | - throw new RepositoryException('Product with ID '.$id.' has been deleted', RepositoryException::RESOURCE_DENIED); |
|
133 | + if( $product->trashed() ) |
|
134 | + throw new RepositoryException('Product with ID '.$id.' has been deleted', RepositoryException::RESOURCE_DENIED); |
|
135 | 135 | |
136 | - try { |
|
137 | - $product->delete(); |
|
138 | - } |
|
139 | - catch(\Exception $e) { |
|
140 | - throw new RepositoryException('Database error while deleting', RepositoryException::DATABASE_ERROR); |
|
141 | - } |
|
136 | + try { |
|
137 | + $product->delete(); |
|
138 | + } |
|
139 | + catch(\Exception $e) { |
|
140 | + throw new RepositoryException('Database error while deleting', RepositoryException::DATABASE_ERROR); |
|
141 | + } |
|
142 | 142 | |
143 | - return $product; |
|
144 | - } |
|
143 | + return $product; |
|
144 | + } |
|
145 | 145 | |
146 | 146 | /** |
147 | 147 | * Returns a top-10 from the most sold product in a given interval. |
@@ -183,101 +183,101 @@ discard block |
||
183 | 183 | return $productRank; |
184 | 184 | } |
185 | 185 | |
186 | - /** |
|
187 | - * Format a Product object or an Eloquent Collection to a REST format |
|
188 | - * |
|
189 | - * @param object $products MUST be a Product object or an Eloquent Collection |
|
190 | - * @return array |
|
191 | - */ |
|
192 | - public function APIFormat($products) |
|
186 | + /** |
|
187 | + * Format a Product object or an Eloquent Collection to a REST format |
|
188 | + * |
|
189 | + * @param object $products MUST be a Product object or an Eloquent Collection |
|
190 | + * @return array |
|
191 | + */ |
|
192 | + public function APIFormat($products) |
|
193 | 193 | { |
194 | 194 | if($products instanceof Products) { |
195 | 195 | return [$this->formatRecord($products)]; |
196 | 196 | } |
197 | 197 | |
198 | - $responseArray = []; |
|
199 | - foreach ($products as $product) { |
|
200 | - array_push($responseArray, $this->formatRecord( $product )); |
|
201 | - } |
|
202 | - |
|
203 | - return $responseArray; |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * Validates an array of data |
|
208 | - * |
|
209 | - * @param array $data Data array of the form: column_alias => value_to_validate |
|
210 | - * @param bool $required Indicates if validation must follow 'required' instructions |
|
211 | - * @return void |
|
212 | - * @throws RepositoryException If the validation failed |
|
213 | - */ |
|
214 | - public function validate(array $data, $required=true) |
|
198 | + $responseArray = []; |
|
199 | + foreach ($products as $product) { |
|
200 | + array_push($responseArray, $this->formatRecord( $product )); |
|
201 | + } |
|
202 | + |
|
203 | + return $responseArray; |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * Validates an array of data |
|
208 | + * |
|
209 | + * @param array $data Data array of the form: column_alias => value_to_validate |
|
210 | + * @param bool $required Indicates if validation must follow 'required' instructions |
|
211 | + * @return void |
|
212 | + * @throws RepositoryException If the validation failed |
|
213 | + */ |
|
214 | + public function validate(array $data, $required=true) |
|
215 | 215 | { |
216 | - $rules = Products::$validationRules; |
|
216 | + $rules = Products::$validationRules; |
|
217 | 217 | |
218 | - if( $required === false ) { |
|
218 | + if( $required === false ) { |
|
219 | 219 | |
220 | - foreach ($rules as $rule => $value) { |
|
220 | + foreach ($rules as $rule => $value) { |
|
221 | 221 | |
222 | - $rules[$rule] = str_replace('required', '', $value); |
|
223 | - } |
|
224 | - } |
|
222 | + $rules[$rule] = str_replace('required', '', $value); |
|
223 | + } |
|
224 | + } |
|
225 | 225 | |
226 | - $validator = Validator::make($data, $rules); |
|
226 | + $validator = Validator::make($data, $rules); |
|
227 | 227 | |
228 | - if( $validator->fails() ) { |
|
229 | - throw new RepositoryException('Validation failed', RepositoryException::VALIDATION_FAILED); |
|
230 | - } |
|
231 | - } |
|
228 | + if( $validator->fails() ) { |
|
229 | + throw new RepositoryException('Validation failed', RepositoryException::VALIDATION_FAILED); |
|
230 | + } |
|
231 | + } |
|
232 | 232 | |
233 | - /** |
|
234 | - * Create or update an Eloquent model with the specified data |
|
235 | - * |
|
236 | - * @param array $data Data array with the new data |
|
237 | - * @param object $model Existing Eloquent model to update. If NULL, a new model will be created |
|
238 | - * @return null|object The new or updated Eloquent model |
|
239 | - */ |
|
240 | - private function createModel($data, $model=NULL) |
|
233 | + /** |
|
234 | + * Create or update an Eloquent model with the specified data |
|
235 | + * |
|
236 | + * @param array $data Data array with the new data |
|
237 | + * @param object $model Existing Eloquent model to update. If NULL, a new model will be created |
|
238 | + * @return null|object The new or updated Eloquent model |
|
239 | + */ |
|
240 | + private function createModel($data, $model=NULL) |
|
241 | 241 | { |
242 | - if( $model == NULL ) |
|
243 | - $model = new Products(); |
|
242 | + if( $model == NULL ) |
|
243 | + $model = new Products(); |
|
244 | 244 | |
245 | - $columnAlias = ['category' => 'category_id', |
|
246 | - 'name' => 'product_name', |
|
247 | - 'price' => 'price', |
|
248 | - 'quantity' => 'quantity', |
|
249 | - 'description' => 'description' ]; |
|
245 | + $columnAlias = ['category' => 'category_id', |
|
246 | + 'name' => 'product_name', |
|
247 | + 'price' => 'price', |
|
248 | + 'quantity' => 'quantity', |
|
249 | + 'description' => 'description' ]; |
|
250 | 250 | |
251 | - foreach ($data as $name => $value) { |
|
251 | + foreach ($data as $name => $value) { |
|
252 | 252 | |
253 | - if( !isset( $columnAlias[$name] ) ) |
|
254 | - continue; |
|
255 | - |
|
256 | - if( $value !== '' ) |
|
257 | - $model->$columnAlias[$name] = $value; |
|
258 | - } |
|
259 | - |
|
260 | - return $model; |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * Format an Eloquent 'record' into a REST oriented object |
|
265 | - * |
|
266 | - * @param object $product An Eloquent object that represents a record |
|
267 | - * @return object A stdObject which contains some casted attributes |
|
268 | - */ |
|
269 | - private function formatRecord($product) |
|
253 | + if( !isset( $columnAlias[$name] ) ) |
|
254 | + continue; |
|
255 | + |
|
256 | + if( $value !== '' ) |
|
257 | + $model->$columnAlias[$name] = $value; |
|
258 | + } |
|
259 | + |
|
260 | + return $model; |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * Format an Eloquent 'record' into a REST oriented object |
|
265 | + * |
|
266 | + * @param object $product An Eloquent object that represents a record |
|
267 | + * @return object A stdObject which contains some casted attributes |
|
268 | + */ |
|
269 | + private function formatRecord($product) |
|
270 | 270 | { |
271 | - $productObject = new stdClass(); |
|
271 | + $productObject = new stdClass(); |
|
272 | 272 | |
273 | - $productObject->id = intval($product->product_id); |
|
274 | - $productObject->category = intval($product->category_id); |
|
275 | - $productObject->price = floatval($product->price); |
|
276 | - $productObject->quantity = intval($product->quantity); |
|
273 | + $productObject->id = intval($product->product_id); |
|
274 | + $productObject->category = intval($product->category_id); |
|
275 | + $productObject->price = floatval($product->price); |
|
276 | + $productObject->quantity = intval($product->quantity); |
|
277 | 277 | |
278 | - $productObject->name = $product->product_name; |
|
279 | - $productObject->description = $product->description; |
|
278 | + $productObject->name = $product->product_name; |
|
279 | + $productObject->description = $product->description; |
|
280 | 280 | |
281 | - return $productObject; |
|
282 | - } |
|
281 | + return $productObject; |
|
282 | + } |
|
283 | 283 | } |
284 | 284 | \ No newline at end of file |