Refactor basic methods to trait

This commit is contained in:
James Cole 2018-08-09 16:16:27 +02:00
parent 0c2093753d
commit aa0758cd2b
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
3 changed files with 71 additions and 33 deletions

View File

@ -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;
}
}

View File

@ -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.
*

View File

@ -0,0 +1,67 @@
<?php
/**
* BasicDataSupport.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
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;
}
}