diff --git a/app/Http/Controllers/Summary/ItemController.php b/app/Http/Controllers/Summary/ItemController.php index cc39c437..c6c71863 100644 --- a/app/Http/Controllers/Summary/ItemController.php +++ b/app/Http/Controllers/Summary/ItemController.php @@ -4,15 +4,15 @@ use App\Http\Controllers\Controller; use App\Item\Factory; -use App\Option\Get; -use App\Response\Cache; -use App\Request\Parameter; -use App\Request\Route; -use App\Request\Validate\Boolean; use App\Models\Transformers\Summary\ItemCategory as ItemCategoryTransformer; use App\Models\Transformers\Summary\ItemMonth as ItemMonthTransformer; use App\Models\Transformers\Summary\ItemSubcategory as ItemSubcategoryTransformer; use App\Models\Transformers\Summary\ItemYear as ItemYearTransformer; +use App\Option\Get; +use App\Request\Parameter; +use App\Request\Route; +use App\Request\Validate\Boolean; +use App\Response\Cache; use App\Response\Header\Headers; use Illuminate\Http\JsonResponse; @@ -62,17 +62,13 @@ public function index(string $resource_type_id, string $resource_id): JsonRespon $category = null; $subcategory = null; - if ( - array_key_exists('years', $parameters) === true && - Boolean::convertedValue($parameters['years']) === true - ) { + if (array_key_exists('years', $parameters) === true && + Boolean::convertedValue($parameters['years']) === true) { $years = true; } - if ( - array_key_exists('months', $parameters) === true && - Boolean::convertedValue($parameters['months']) === true - ) { + if (array_key_exists('months', $parameters) === true && + Boolean::convertedValue($parameters['months']) === true) { $months = true; } @@ -82,7 +78,7 @@ public function index(string $resource_type_id, string $resource_id): JsonRespon } if (array_key_exists('subcategories', $parameters) === true && - Boolean::convertedValue($parameters['subcategories']) === true) { + Boolean::convertedValue($parameters['subcategories']) === true) { $subcategories = true; } @@ -252,26 +248,21 @@ private function tcoSummary( $parameters ); + $total = '0.00'; + if (array_key_exists(0, $summary) && array_key_exists('total', $summary[0])) { + $total = number_format($summary[0]['total'], 2, '.', ''); + } + $collection = [ - 'total' => number_format($summary[0]['total'], 2, '.', '') + 'total' => $total ]; - $headers = new Headers(); - $headers->addCacheControl($cache_control->visibility(), $cache_control->ttl())-> - addETag($collection)-> - addParameters(Parameter\Request::xHeader()); - - if (array_key_exists(0, $summary)) { - if (array_key_exists('last_updated', $summary[0]) === true) { - $headers->addLastUpdated($summary[0]['last_updated']); - } - if (array_key_exists('total_count', $summary[0]) === true) { - $headers->addTotalCount((int) $summary[0]['total_count']); - } - } - - $cache_summary->create($collection, $headers->headers()); - $cache_control->put(request()->getRequestUri(), $cache_summary->content()); + $this->assignContentToCache( + $summary, + $collection, + $cache_control, + $cache_summary + ); } return response()->json($cache_summary->collection(), 200, $cache_summary->headers()); @@ -313,22 +304,12 @@ static function ($year) { $summary ); - $headers = new Headers(); - $headers->addCacheControl($cache_control->visibility(), $cache_control->ttl())-> - addETag($collection)-> - addParameters(Parameter\Request::xHeader()); - - if (array_key_exists(0, $summary)) { - if (array_key_exists('last_updated', $summary[0]) === true) { - $headers->addLastUpdated($summary[0]['last_updated']); - } - if (array_key_exists('total_count', $summary[0]) === true) { - $headers->addTotalCount((int) $summary[0]['total_count']); - } - } - - $cache_summary->create($collection, $headers->headers()); - $cache_control->put(request()->getRequestUri(), $cache_summary->content()); + $this->assignContentToCache( + $summary, + $collection, + $cache_control, + $cache_summary + ); } return response()->json($cache_summary->collection(), 200, $cache_summary->headers()); @@ -366,28 +347,17 @@ private function yearSummary( $parameters ); - if (count($summary) === 0) { - abort(404, 'Unable to generate the requested summary'); - } - - $collection = (new ItemYearTransformer($summary[0]))->toArray(); - - $headers = new Headers(); - $headers->addCacheControl($cache_control->visibility(), $cache_control->ttl())-> - addETag($collection)-> - addParameters(Parameter\Request::xHeader()); - + $collection = []; if (array_key_exists(0, $summary)) { - if (array_key_exists('last_updated', $summary[0]) === true) { - $headers->addLastUpdated($summary[0]['last_updated']); - } - if (array_key_exists('total_count', $summary[0]) === true) { - $headers->addTotalCount((int) $summary[0]['total_count']); - } + $collection = (new ItemYearTransformer($summary[0]))->toArray(); } - $cache_summary->create($collection, $headers->headers()); - $cache_control->put(request()->getRequestUri(), $cache_summary->content()); + $this->assignContentToCache( + $summary, + $collection, + $cache_control, + $cache_summary + ); } return response()->json($cache_summary->collection(), 200, $cache_summary->headers()); @@ -432,22 +402,12 @@ static function ($month) { $summary ); - $headers = new Headers(); - $headers->addCacheControl($cache_control->visibility(), $cache_control->ttl())-> - addETag($collection)-> - addParameters(Parameter\Request::xHeader()); - - if (array_key_exists(0, $summary)) { - if (array_key_exists('last_updated', $summary[0]) === true) { - $headers->addLastUpdated($summary[0]['last_updated']); - } - if (array_key_exists('total_count', $summary[0]) === true) { - $headers->addTotalCount((int) $summary[0]['total_count']); - } - } - - $cache_summary->create($collection, $headers->headers()); - $cache_control->put(request()->getRequestUri(), $cache_summary->content()); + $this->assignContentToCache( + $summary, + $collection, + $cache_control, + $cache_summary + ); } return response()->json($cache_summary->collection(), 200, $cache_summary->headers()); @@ -488,28 +448,17 @@ private function monthSummary( $parameters ); - if (count($summary) === 0) { - abort(404, 'Unable to generate the requested summary'); - } - - $collection = (new ItemMonthTransformer($summary[0]))->toArray(); - - $headers = new Headers(); - $headers->addCacheControl($cache_control->visibility(), $cache_control->ttl())-> - addETag($collection)-> - addParameters(Parameter\Request::xHeader()); - + $collection = []; if (array_key_exists(0, $summary)) { - if (array_key_exists('last_updated', $summary[0]) === true) { - $headers->addLastUpdated($summary[0]['last_updated']); - } - if (array_key_exists('total_count', $summary[0]) === true) { - $headers->addTotalCount((int) $summary[0]['total_count']); - } + $collection = (new ItemMonthTransformer($summary[0]))->toArray(); } - $cache_summary->create($collection, $headers->headers()); - $cache_control->put(request()->getRequestUri(), $cache_summary->content()); + $this->assignContentToCache( + $summary, + $collection, + $cache_control, + $cache_summary + ); } return response()->json($cache_summary->collection(), 200, $cache_summary->headers()); @@ -550,22 +499,12 @@ static function ($category) { $summary ); - $headers = new Headers(); - $headers->addCacheControl($cache_control->visibility(), $cache_control->ttl())-> - addETag($collection)-> - addParameters(Parameter\Request::xHeader()); - - if (array_key_exists(0, $summary)) { - if (array_key_exists('last_updated', $summary[0]) === true) { - $headers->addLastUpdated($summary[0]['last_updated']); - } - if (array_key_exists('total_count', $summary[0]) === true) { - $headers->addTotalCount((int) $summary[0]['total_count']); - } - } - - $cache_summary->create($collection, $headers->headers()); - $cache_control->put(request()->getRequestUri(), $cache_summary->content()); + $this->assignContentToCache( + $summary, + $collection, + $cache_control, + $cache_summary + ); } return response()->json($cache_summary->collection(), 200, $cache_summary->headers()); @@ -618,26 +557,21 @@ private function filteredSummary( $filter_parameters ); + $total = '0.00'; + if (array_key_exists(0, $summary) && array_key_exists('total', $summary[0])) { + $total = number_format($summary[0]['total'], 2, '.', ''); + } + $collection = [ - 'total' => number_format($summary[0]['total'], 2, '.', '') + 'total' => $total ]; - $headers = new Headers(); - $headers->addCacheControl($cache_control->visibility(), $cache_control->ttl())-> - addETag($collection)-> - addParameters(Parameter\Request::xHeader()); - - if (array_key_exists(0, $summary)) { - if (array_key_exists('last_updated', $summary[0]) === true) { - $headers->addLastUpdated($summary[0]['last_updated']); - } - if (array_key_exists('total_count', $summary[0]) === true) { - $headers->addTotalCount((int) $summary[0]['total_count']); - } - } - - $cache_summary->create($collection, $headers->headers()); - $cache_control->put(request()->getRequestUri(), $cache_summary->content()); + $this->assignContentToCache( + $summary, + $collection, + $cache_control, + $cache_summary + ); } return response()->json($cache_summary->collection(), 200, $cache_summary->headers()); @@ -675,28 +609,17 @@ private function categorySummary( $parameters ); - if (count($summary) === 0) { - abort(404, 'Unable to generate the requested summary'); - } - - $collection = (new ItemCategoryTransformer($summary[0]))->toArray(); - - $headers = new Headers(); - $headers->addCacheControl($cache_control->visibility(), $cache_control->ttl())-> - addETag($collection)-> - addParameters(Parameter\Request::xHeader()); - + $collection = []; if (array_key_exists(0, $summary)) { - if (array_key_exists('last_updated', $summary[0]) === true) { - $headers->addLastUpdated($summary[0]['last_updated']); - } - if (array_key_exists('total_count', $summary[0]) === true) { - $headers->addTotalCount((int) $summary[0]['total_count']); - } + $collection = (new ItemCategoryTransformer($summary[0]))->toArray(); } - $cache_summary->create($collection, $headers->headers()); - $cache_control->put(request()->getRequestUri(), $cache_summary->content()); + $this->assignContentToCache( + $summary, + $collection, + $cache_control, + $cache_summary + ); } return response()->json($cache_summary->collection(), 200, $cache_summary->headers()); @@ -741,22 +664,12 @@ static function ($subcategory) { $summary ); - $headers = new Headers(); - $headers->addCacheControl($cache_control->visibility(), $cache_control->ttl())-> - addETag($collection)-> - addParameters(Parameter\Request::xHeader()); - - if (array_key_exists(0, $summary)) { - if (array_key_exists('last_updated', $summary[0]) === true) { - $headers->addLastUpdated($summary[0]['last_updated']); - } - if (array_key_exists('total_count', $summary[0]) === true) { - $headers->addTotalCount((int) $summary[0]['total_count']); - } - } - - $cache_summary->create($collection, $headers->headers()); - $cache_control->put(request()->getRequestUri(), $cache_summary->content()); + $this->assignContentToCache( + $summary, + $collection, + $cache_control, + $cache_summary + ); } return response()->json($cache_summary->collection(), 200, $cache_summary->headers()); @@ -797,28 +710,17 @@ private function subcategorySummary( $parameters ); - if (count($summary) === 0) { - abort(404, 'Unable to generate the requested summary'); - } - - $collection = (new ItemSubcategoryTransformer($summary[0]))->toArray(); - - $headers = new Headers(); - $headers->addCacheControl($cache_control->visibility(), $cache_control->ttl())-> - addETag($collection)-> - addParameters(Parameter\Request::xHeader()); - + $collection = []; if (array_key_exists(0, $summary)) { - if (array_key_exists('last_updated', $summary[0]) === true) { - $headers->addLastUpdated($summary[0]['last_updated']); - } - if (array_key_exists('total_count', $summary[0]) === true) { - $headers->addTotalCount((int) $summary[0]['total_count']); - } + $collection = (new ItemSubcategoryTransformer($summary[0]))->toArray(); } - $cache_summary->create($collection, $headers->headers()); - $cache_control->put(request()->getRequestUri(), $cache_summary->content()); + $this->assignContentToCache( + $summary, + $collection, + $cache_control, + $cache_summary + ); } return response()->json($cache_summary->collection(), 200, $cache_summary->headers()); @@ -858,4 +760,31 @@ public function optionsIndex(string $resource_type_id, string $resource_id) return $this->optionsResponse($get, 200); } + + private function assignContentToCache( + array $summary, + array $collection, + Cache\Control $cache_control, + Cache\Summary $cache_summary + ) + { + $headers = new Headers(); + $headers->addCacheControl($cache_control->visibility(), $cache_control->ttl())-> + addETag($collection)-> + addParameters(Parameter\Request::xHeader()); + + if (array_key_exists(0, $summary)) { + if (array_key_exists('last_updated', $summary[0]) === true) { + $headers->addLastUpdated($summary[0]['last_updated']); + } + if (array_key_exists('total_count', $summary[0]) === true) { + $headers->addTotalCount((int) $summary[0]['total_count']); + } + } + + $cache_summary->create($collection, $headers->headers()); + $cache_control->put(request()->getRequestUri(), $cache_summary->content()); + + return $cache_summary; + } }