Skip to content

Commit

Permalink
Merge pull request #50 from deanblackborough/split-middleware
Browse files Browse the repository at this point in the history
Split middleware
  • Loading branch information
deanblackborough authored Oct 7, 2018
2 parents 54c21b1 + 43d9fb3 commit 56b013c
Show file tree
Hide file tree
Showing 27 changed files with 574 additions and 442 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

Full changelog for the costs to expect REST API.

## 2019-xx-xx - v1.xx-x

* Corrected routes displayed in root of API.
* Split Hashids middleware, now ConvertGetParameters and ConvertRouteParameters.
* Added App\Http\Parameters\Get class to validate GET parameters, moved code from base controller.
* Added App\Http\Parameters\Route\Validate and child classes to validator route parameters.
* Updated controllers to use new App\Http\Parameters\* classes.
* Minor bug fix, booleans not being checked correctly.

## 2019-09-25 - v1.04.0

* GET parameters are now validated, invalid values are silently removed.
Expand Down
34 changes: 12 additions & 22 deletions app/Http/Controllers/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Http\Controllers;

use App\Http\Parameters\Get;
use App\Http\Parameters\Route\Validate;
use App\Models\Category;
use App\Transformers\Category as CategoryTransformer;
use App\Validators\Category as CategoryValidator;
Expand All @@ -19,6 +21,9 @@
*/
class CategoryController extends Controller
{
protected $collection_parameters = [];
protected $show_parameters = [];

/**
* Return all the categories
*
Expand All @@ -30,9 +35,7 @@ public function index(Request $request): JsonResponse
{
$categories = (new Category())->paginatedCollection();

$this->collection_parameters = [];
$this->collection_parameters['include_sub_categories'] =
boolval($request->query('include_sub_categories', false));
$this->collection_parameters = Get::parameters(['include_sub_categories']);

$headers = [
'X-Total-Count' => count($categories)
Expand Down Expand Up @@ -60,13 +63,9 @@ function ($category)
*/
public function show(Request $request, $category_id): JsonResponse
{
if ($category_id === 'nill') {
return $this->returnResourceNotFound();
}
Validate::category($category_id);

$this->collection_parameters = [];
$this->collection_parameters['include_sub_categories'] =
boolval($request->query('include_sub_categories', false));
$this->show_parameters = Get::parameters(['include_sub_categories']);

$category = (new Category)->single($category_id);

Expand All @@ -75,7 +74,7 @@ public function show(Request $request, $category_id): JsonResponse
}

return response()->json(
(new CategoryTransformer($category, $this->parameters_show))->toArray(),
(new CategoryTransformer($category, $this->show_parameters))->toArray(),
200,
[
'X-Total-Count' => 1
Expand Down Expand Up @@ -110,12 +109,7 @@ public function optionsIndex(Request $request): JsonResponse
*/
public function optionsShow(Request $request, string $category_id): JsonResponse
{
if (
$category_id === 'nill' ||
(new Category)->single($category_id) === null
) {
return $this->returnResourceNotFound();
}
Validate::category($category_id);

return $this->generateOptionsForShow(
'api.descriptions.category.GET_show',
Expand Down Expand Up @@ -175,14 +169,10 @@ public function delete(
string $category_id
): JsonResponse
{
$category = (new Category())->single($category_id);

if ($category === null) {
return $this->returnResourceNotFound();
}
Validate::category($category_id);

try {
$category->delete();
(new Category())->find($category_id)->delete();

return response()->json([], 204);
} catch (QueryException $e) {
Expand Down
88 changes: 0 additions & 88 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\SubCategory;
use App\Utilities\Hash;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\JsonResponse;
Expand All @@ -19,9 +17,6 @@ class Controller extends BaseController

protected $hash;

protected $collection_parameters = [];
protected $parameters_show = [];

public function __construct()
{
$this->hash = new Hash();
Expand Down Expand Up @@ -275,87 +270,4 @@ protected function generateLinkHeader(string $uri, string $parameters, int $limi
return null;
}
}

/**
* Check the $request for GET parameters, if any are valid set them the the
* parameters collection
*
* @param array $request_parameters Parameters from $request->all()
* @param array $parameters GET params to try and set
*
* @return void
*/
protected function fetchCollectionParameters(array $request_parameters = [], array $parameters = [])
{
$this->collection_parameters = [];

foreach ($parameters as $parameter) {
if (array_key_exists($parameter, $request_parameters) === true &&
$request_parameters[$parameter] !== null &&
$request_parameters[$parameter] !== 'nill') {
$this->collection_parameters[$parameter] = $request_parameters[$parameter];
}
}

$this->validateCollectionParameters($parameters);
}

/**
* Validate collection parameters, invalid collection parameters are silently removed
*
* @param array $parameters GET parameters to attempt to validate
*
* @return void
*/
protected function validateCollectionParameters(array $parameters = [])
{
foreach ($parameters as $parameter) {
switch ($parameter) {
case 'category':
if (array_key_exists($parameter, $this->collection_parameters) === true) {
if ((new Category())->where('id', '=', $this->collection_parameters[$parameter])->exists() === false) {
unset($this->collection_parameters[$parameter]);
}
}
break;

case 'month':
if (array_key_exists($parameter, $this->collection_parameters) === true) {
if (intval($this->collection_parameters[$parameter]) < 1 ||
$this->collection_parameters[$parameter] > 12) {

unset($this->collection_parameters[$parameter]);
}
}
break;

case 'sub_category':
if (array_key_exists($parameter, $this->collection_parameters) === true) {
if (
(new SubCategory())->
where('sub_category.id', '=', $this->collection_parameters[$parameter])->
where('sub_category.category_id', '=', $this->collection_parameters['category'])->
exists() === false
) {
unset($this->collection_parameters[$parameter]);
}
}
break;

case 'year':
if (array_key_exists($parameter, $this->collection_parameters) === true) {
if (intval($this->collection_parameters[$parameter]) < 2013 ||
$this->collection_parameters[$parameter] > intval(date('Y'))) {

unset($this->collection_parameters[$parameter]);
}
}
break;

default:
// Do nothing
break;
}
}
}
}
8 changes: 5 additions & 3 deletions app/Http/Controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ public function index(Request $request)
foreach (Route::getRoutes() as $route) {
if (starts_with($route->uri, Config::get('api.version.prefix') ) === true) {
if (isset($routes_to_display[$route->uri]['methods'])) {
$routes_to_display[$route->uri]['methods'] = array_merge($route->methods,
$routes_to_display[$route->uri]['methods']);
$routes_to_display[$route->uri]['methods'] = array_merge(
$route->methods,
$routes_to_display[$route->uri]['methods']
);
} else {
$routes_to_display[$route->uri]['methods'] = $route->methods;
}

$routes_to_display[$route->uri]['uri'] = $route->uri;
$routes_to_display[$route->uri]['uri'] = '/' . $route->uri;
}
}

Expand Down
34 changes: 12 additions & 22 deletions app/Http/Controllers/ItemCategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Http\Controllers;

use App\Http\Route\Validators\Item as ItemRouteValidator;
use App\Http\Parameters\Route\Validate;
use App\Models\Category;
use App\Models\ItemCategory;
use App\Transformers\ItemCategory as ItemCategoryTransformer;
Expand Down Expand Up @@ -35,9 +35,7 @@ class ItemCategoryController extends Controller
*/
public function index(Request $request, string $resource_type_id, string $resource_id, string $item_id): JsonResponse
{
if (ItemRouteValidator::validate($resource_type_id, $resource_id, $item_id) === false) {
return $this->returnResourceNotFound();
}
Validate::item($resource_type_id, $resource_id, $item_id);

$item_category = (new ItemCategory())->paginatedCollection(
$resource_type_id,
Expand Down Expand Up @@ -79,10 +77,9 @@ public function show(
string $item_category_id
): JsonResponse
{
if (
ItemRouteValidator::validate($resource_type_id, $resource_id, $item_id) === false ||
$item_category_id === 'nill'
) {
Validate::item($resource_type_id, $resource_id, $item_id);

if ($item_category_id === 'nill') {
return $this->returnResourceNotFound();
}

Expand Down Expand Up @@ -120,9 +117,7 @@ public function show(
*/
public function optionsIndex(Request $request, string $resource_type_id, string $resource_id, string $item_id): JsonResponse
{
if (ItemRouteValidator::validate($resource_type_id, $resource_id, $item_id) === false) {
return $this->returnResourceNotFound();
}
Validate::item($resource_type_id, $resource_id, $item_id);

$this->setConditionalPostParameters();

Expand Down Expand Up @@ -154,10 +149,9 @@ public function optionsShow(
string $item_category_id
): JsonResponse
{
if (
ItemRouteValidator::validate($resource_type_id, $resource_id, $item_id) === false ||
$item_category_id === 'nill'
) {
Validate::item($resource_type_id, $resource_id, $item_id);

if ($item_category_id === 'nill') {
return $this->returnResourceNotFound();
}

Expand Down Expand Up @@ -198,9 +192,7 @@ public function create(
string $item_id
): JsonResponse
{
if (ItemRouteValidator::validate($resource_type_id, $resource_id, $item_id) === false) {
return $this->returnResourceNotFound();
}
Validate::item($resource_type_id, $resource_id, $item_id);

$validator = (new ItemCategoryValidator)->create($request);

Expand Down Expand Up @@ -244,7 +236,7 @@ public function create(
* Set any conditional POST parameters, will be merged with the data arrays defined in
* config/api/route.php
*
* @return void|JsonResponse
* @return JsonResponse
*/
private function setConditionalPostParameters()
{
Expand Down Expand Up @@ -290,9 +282,7 @@ public function delete(
string $item_category_id
): JsonResponse
{
if (ItemRouteValidator::validate($resource_type_id, $resource_id, $item_id) === false) {
return $this->returnResourceNotFound();
}
Validate::itemCategory($resource_type_id, $resource_id, $item_id, $item_category_id);

$item_category = (new ItemCategory())->single(
$resource_type_id,
Expand Down
Loading

0 comments on commit 56b013c

Please sign in to comment.