From 1a325f497840db485b180a5b2ba43a51b7927219 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 22 Oct 2017 20:13:02 +0200 Subject: [PATCH] Some code cleanup. --- app/Http/Controllers/BudgetController.php | 100 +++++++++----------- app/Http/Controllers/ReportController.php | 3 +- app/Http/Middleware/Sandstorm.php | 1 + app/Import/Object/ImportAccount.php | 1 + app/Import/Specifics/AbnAmroDescription.php | 2 +- app/Services/Bunq/Request/BunqRequest.php | 22 ++++- app/Support/Preferences.php | 2 +- app/Support/Search/Modifier.php | 8 ++ 8 files changed, 76 insertions(+), 63 deletions(-) diff --git a/app/Http/Controllers/BudgetController.php b/app/Http/Controllers/BudgetController.php index af590a6225..d31bec7c5e 100644 --- a/app/Http/Controllers/BudgetController.php +++ b/app/Http/Controllers/BudgetController.php @@ -27,7 +27,6 @@ use Carbon\Carbon; use Exception; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Helpers\Collector\JournalCollectorInterface; -use FireflyIII\Helpers\Filter\InternalTransferFilter; use FireflyIII\Http\Requests\BudgetFormRequest; use FireflyIII\Http\Requests\BudgetIncomeRequest; use FireflyIII\Models\Budget; @@ -49,6 +48,7 @@ use View; * * @package FireflyIII\Http\Controllers * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + * @SuppressWarnings(PHPMD.TooManyPublicMethods) */ class BudgetController extends Controller { @@ -256,6 +256,7 @@ class BudgetController extends Controller /** * @param Carbon $start * @param Carbon $end + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ @@ -269,50 +270,49 @@ class BudgetController extends Controller if ($cache->has()) { $result = $cache->get(); // @codeCoverageIgnore + + return view('budgets.info', compact('result', 'begin', 'currentEnd')); } - if (!$cache->has()) { - $result = [ - 'available' => '0', - 'earned' => '0', - 'suggested' => '0', - ]; - $currency = app('amount')->getDefaultCurrency(); - $range = Preferences::get('viewRange', '1M')->data; - $begin = Navigation::subtractPeriod($start, $range, 3); + $result = [ + 'available' => '0', + 'earned' => '0', + 'suggested' => '0', + ]; + $currency = app('amount')->getDefaultCurrency(); + $range = Preferences::get('viewRange', '1M')->data; + $begin = Navigation::subtractPeriod($start, $range, 3); - // get average amount available. - $total = '0'; - $count = 0; - $currentStart = clone $begin; - while ($currentStart < $start) { - $currentEnd = Navigation::endOfPeriod($currentStart, $range); - $total = bcadd($total, $this->repository->getAvailableBudget($currency, $currentStart, $currentEnd)); - $currentStart = Navigation::addPeriod($currentStart, $range, 0); - $count++; - } - $result['available'] = bcdiv($total, strval($count)); - - // amount earned in this period: - $subDay = clone $end; - $subDay->subDay(); - /** @var JournalCollectorInterface $collector */ - $collector = app(JournalCollectorInterface::class); - $collector->setAllAssetAccounts()->setRange($begin, $subDay)->setTypes([TransactionType::DEPOSIT])->withOpposingAccount(); - $result['earned'] = bcdiv(strval($collector->getJournals()->sum('transaction_amount')), strval($count)); - - // amount spent in period - /** @var JournalCollectorInterface $collector */ - $collector = app(JournalCollectorInterface::class); - $collector->setAllAssetAccounts()->setRange($begin, $subDay)->setTypes([TransactionType::WITHDRAWAL])->withOpposingAccount(); - $result['spent'] = bcdiv(strval($collector->getJournals()->sum('transaction_amount')), strval($count)); - // suggestion starts with the amount spent - $result['suggested'] = bcmul($result['spent'], '-1'); - $result['suggested'] = bccomp($result['suggested'], $result['earned']) === 1 ? $result['earned'] : $result['suggested']; - // unless it's more than you earned. So min() of suggested/earned - - - $cache->store($result); + // get average amount available. + $total = '0'; + $count = 0; + $currentStart = clone $begin; + while ($currentStart < $start) { + $currentEnd = Navigation::endOfPeriod($currentStart, $range); + $total = bcadd($total, $this->repository->getAvailableBudget($currency, $currentStart, $currentEnd)); + $currentStart = Navigation::addPeriod($currentStart, $range, 0); + $count++; } + $result['available'] = bcdiv($total, strval($count)); + + // amount earned in this period: + $subDay = clone $end; + $subDay->subDay(); + /** @var JournalCollectorInterface $collector */ + $collector = app(JournalCollectorInterface::class); + $collector->setAllAssetAccounts()->setRange($begin, $subDay)->setTypes([TransactionType::DEPOSIT])->withOpposingAccount(); + $result['earned'] = bcdiv(strval($collector->getJournals()->sum('transaction_amount')), strval($count)); + + // amount spent in period + /** @var JournalCollectorInterface $collector */ + $collector = app(JournalCollectorInterface::class); + $collector->setAllAssetAccounts()->setRange($begin, $subDay)->setTypes([TransactionType::WITHDRAWAL])->withOpposingAccount(); + $result['spent'] = bcdiv(strval($collector->getJournals()->sum('transaction_amount')), strval($count)); + // suggestion starts with the amount spent + $result['suggested'] = bcmul($result['spent'], '-1'); + $result['suggested'] = bccomp($result['suggested'], $result['earned']) === 1 ? $result['earned'] : $result['suggested']; + // unless it's more than you earned. So min() of suggested/earned + + $cache->store($result); return view('budgets.info', compact('result', 'begin', 'currentEnd')); @@ -573,9 +573,7 @@ class BudgetController extends Controller $start = Navigation::startOfPeriod($start, $range); $end = Navigation::endOfX(new Carbon, $range, null); $entries = new Collection; - - // properties for cache - $cache = new CacheProperties; + $cache = new CacheProperties; $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty('no-budget-period-entries'); @@ -588,8 +586,6 @@ class BudgetController extends Controller while ($end >= $start) { $end = Navigation::startOfPeriod($end, $range); $currentEnd = Navigation::endOfPeriod($end, $range); - - // count journals without budget in this period: /** @var JournalCollectorInterface $collector */ $collector = app(JournalCollectorInterface::class); $collector->setAllAssetAccounts()->setRange($end, $currentEnd)->withoutBudget()->withOpposingAccount()->setTypes([TransactionType::WITHDRAWAL]); @@ -598,15 +594,7 @@ class BudgetController extends Controller $journals = $set->count(); $dateStr = $end->format('Y-m-d'); $dateName = Navigation::periodShow($end, $range); - $entries->push( - [ - 'string' => $dateStr, - 'name' => $dateName, - 'count' => $journals, - 'sum' => $sum, - 'date' => clone $end, - ] - ); + $entries->push(['string' => $dateStr, 'name' => $dateName, 'count' => $journals, 'sum' => $sum, 'date' => clone $end,]); $end = Navigation::subtractPeriod($end, $range, 1); } $cache->store($entries); diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index 45894649b3..3b7a33b5d6 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -261,6 +261,7 @@ class ReportController extends Controller * @param ReportFormRequest $request * * @return RedirectResponse|\Illuminate\Routing\Redirector + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function postIndex(ReportFormRequest $request) { @@ -299,7 +300,7 @@ class ReportController extends Controller return redirect(route('reports.index')); } - if ($end < $start) { + if ($request->getEndDate() < $request->getStartDate()) { return view('error')->with('message', trans('firefly.end_after_start_date')); } diff --git a/app/Http/Middleware/Sandstorm.php b/app/Http/Middleware/Sandstorm.php index a171e3fd83..c67298e711 100644 --- a/app/Http/Middleware/Sandstorm.php +++ b/app/Http/Middleware/Sandstorm.php @@ -49,6 +49,7 @@ class Sandstorm * * @return mixed * @throws FireflyException + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function handle(Request $request, Closure $next, $guard = null) { diff --git a/app/Import/Object/ImportAccount.php b/app/Import/Object/ImportAccount.php index 5589368c06..d48d4a3ea1 100644 --- a/app/Import/Object/ImportAccount.php +++ b/app/Import/Object/ImportAccount.php @@ -165,6 +165,7 @@ class ImportAccount /** * @return Account + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ private function findExistingObject(): Account { diff --git a/app/Import/Specifics/AbnAmroDescription.php b/app/Import/Specifics/AbnAmroDescription.php index 3d86c75f13..59d30ffc00 100644 --- a/app/Import/Specifics/AbnAmroDescription.php +++ b/app/Import/Specifics/AbnAmroDescription.php @@ -125,7 +125,7 @@ class AbnAmroDescription implements SpecificInterface /** * Parses the current description in SEPA format - * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @return bool true if the description is SEPA format, false otherwise */ protected function parseSepaDescription() diff --git a/app/Services/Bunq/Request/BunqRequest.php b/app/Services/Bunq/Request/BunqRequest.php index 67c6f4b1c7..9ff87c3567 100644 --- a/app/Services/Bunq/Request/BunqRequest.php +++ b/app/Services/Bunq/Request/BunqRequest.php @@ -394,6 +394,21 @@ abstract class BunqRequest return false; } + /** + * @param array $headers + * + * @return string + */ + private function joinHeaders(array $headers): string + { + $string = ''; + foreach ($headers as $header => $value) { + $string .= $header . ': ' . trim($value) . "\n"; + } + + return $string; + } + /** * @param array $response * @@ -447,10 +462,7 @@ abstract class BunqRequest ksort($verifyHeaders); // add them to data to sign: - foreach ($verifyHeaders as $header => $value) { - $dataToVerify .= $header . ': ' . trim($value) . "\n"; - } - + $dataToVerify .= $this->joinHeaders($verifyHeaders); $signature = $headers['x-bunq-server-signature'][0]; $dataToVerify .= "\n" . $body; $result = openssl_verify($dataToVerify, base64_decode($signature), $this->serverPublicKey->getPublicKey(), OPENSSL_ALGO_SHA256); @@ -462,6 +474,8 @@ abstract class BunqRequest } if (!is_int($result)) { Log::error(sprintf('Result of verification is a boolean (%d), return false.', $result)); + + return false; } Log::info('Signature is a match, return true.'); diff --git a/app/Support/Preferences.php b/app/Support/Preferences.php index 2a5e4c2686..74d59ad23e 100644 --- a/app/Support/Preferences.php +++ b/app/Support/Preferences.php @@ -176,7 +176,7 @@ class Preferences /** * @param $name - * @param string $value + * @param $value * * @return Preference */ diff --git a/app/Support/Search/Modifier.php b/app/Support/Search/Modifier.php index e1bbe753d9..89ee78d544 100644 --- a/app/Support/Search/Modifier.php +++ b/app/Support/Search/Modifier.php @@ -51,6 +51,14 @@ class Modifier return $compare === $expected; } + /** + * @param array $modifier + * @param Transaction $transaction + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * + * @return bool + * @throws FireflyException + */ public static function apply(array $modifier, Transaction $transaction): bool { switch ($modifier['type']) {