From aa0758cd2b394b00d36159287fd3278e73c70c69 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 9 Aug 2018 16:16:27 +0200 Subject: [PATCH] Refactor basic methods to trait --- .../Controllers/Account/IndexController.php | 18 +---- .../Controllers/Chart/ReportController.php | 19 +----- .../Http/Controllers/BasicDataSupport.php | 67 +++++++++++++++++++ 3 files changed, 71 insertions(+), 33 deletions(-) create mode 100644 app/Support/Http/Controllers/BasicDataSupport.php diff --git a/app/Http/Controllers/Account/IndexController.php b/app/Http/Controllers/Account/IndexController.php index b9e429ab91..1b393e7291 100644 --- a/app/Http/Controllers/Account/IndexController.php +++ b/app/Http/Controllers/Account/IndexController.php @@ -27,6 +27,7 @@ use Carbon\Carbon; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Models\Account; use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use FireflyIII\Support\Http\Controllers\BasicDataSupport; use Illuminate\Http\Request; use Illuminate\Pagination\LengthAwarePaginator; @@ -36,6 +37,7 @@ use Illuminate\Pagination\LengthAwarePaginator; */ class IndexController extends Controller { + use BasicDataSupport; /** @var AccountRepositoryInterface The account repository */ private $repository; @@ -119,23 +121,7 @@ class IndexController extends Controller } - /** - * Find the ID in a given array. Return '0' of not there (amount). - * - * @param array $array - * @param int $entryId - * - * @return null|mixed - */ - protected function isInArray(array $array, int $entryId) // helper for data (math, calculations) - { - $result = '0'; - if (isset($array[$entryId])) { - $result = $array[$entryId]; - } - return $result; - } } diff --git a/app/Http/Controllers/Chart/ReportController.php b/app/Http/Controllers/Chart/ReportController.php index aea82b8330..c68d8d6a23 100644 --- a/app/Http/Controllers/Chart/ReportController.php +++ b/app/Http/Controllers/Chart/ReportController.php @@ -27,6 +27,7 @@ use FireflyIII\Generator\Chart\Basic\GeneratorInterface; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Repositories\Account\AccountTaskerInterface; use FireflyIII\Support\CacheProperties; +use FireflyIII\Support\Http\Controllers\BasicDataSupport; use Illuminate\Http\JsonResponse; use Illuminate\Support\Collection; use Log; @@ -36,6 +37,7 @@ use Log; */ class ReportController extends Controller { + use BasicDataSupport; /** @var GeneratorInterface Chart generation methods. */ protected $generator; @@ -219,23 +221,6 @@ class ReportController extends Controller return response()->json($data); } - /** - * Sum up an array. - * - * @param array $array - * - * @return string - */ - protected function arraySum(array $array): string // filter + group data - { - $sum = '0'; - foreach ($array as $entry) { - $sum = bcadd($sum, $entry); - } - - return $sum; - } - /** * Collects the incomes and expenses for the given periods, grouped per month. Will cache its results. * diff --git a/app/Support/Http/Controllers/BasicDataSupport.php b/app/Support/Http/Controllers/BasicDataSupport.php new file mode 100644 index 0000000000..7e29ab9452 --- /dev/null +++ b/app/Support/Http/Controllers/BasicDataSupport.php @@ -0,0 +1,67 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Support\Http\Controllers; + +/** + * Trait BasicDataSupport + * + * @package FireflyIII\Support\Http\Controllers + */ +trait BasicDataSupport +{ + /** + * Find the ID in a given array. Return '0' of not there (amount). + * + * @param array $array + * @param int $entryId + * + * @return null|mixed + */ + protected function isInArray(array $array, int $entryId) // helper for data (math, calculations) + { + $result = '0'; + if (isset($array[$entryId])) { + $result = $array[$entryId]; + } + + return $result; + } + + /** + * Sum up an array. + * + * @param array $array + * + * @return string + */ + protected function arraySum(array $array): string // filter + group data + { + $sum = '0'; + foreach ($array as $entry) { + $sum = bcadd($sum, $entry); + } + + return $sum; + } +} \ No newline at end of file