Some code cleanup.

This commit is contained in:
James Cole 2017-10-22 20:13:02 +02:00
parent d72b652453
commit 1a325f4978
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
8 changed files with 76 additions and 63 deletions

View File

@ -27,7 +27,6 @@ use Carbon\Carbon;
use Exception; use Exception;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\JournalCollectorInterface; use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Helpers\Filter\InternalTransferFilter;
use FireflyIII\Http\Requests\BudgetFormRequest; use FireflyIII\Http\Requests\BudgetFormRequest;
use FireflyIII\Http\Requests\BudgetIncomeRequest; use FireflyIII\Http\Requests\BudgetIncomeRequest;
use FireflyIII\Models\Budget; use FireflyIII\Models\Budget;
@ -49,6 +48,7 @@ use View;
* *
* @package FireflyIII\Http\Controllers * @package FireflyIII\Http\Controllers
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
*/ */
class BudgetController extends Controller class BudgetController extends Controller
{ {
@ -256,6 +256,7 @@ class BudgetController extends Controller
/** /**
* @param Carbon $start * @param Carbon $start
* @param Carbon $end * @param Carbon $end
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* *
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/ */
@ -269,50 +270,49 @@ class BudgetController extends Controller
if ($cache->has()) { if ($cache->has()) {
$result = $cache->get(); // @codeCoverageIgnore $result = $cache->get(); // @codeCoverageIgnore
return view('budgets.info', compact('result', 'begin', 'currentEnd'));
} }
if (!$cache->has()) { $result = [
$result = [ 'available' => '0',
'available' => '0', 'earned' => '0',
'earned' => '0', 'suggested' => '0',
'suggested' => '0', ];
]; $currency = app('amount')->getDefaultCurrency();
$currency = app('amount')->getDefaultCurrency(); $range = Preferences::get('viewRange', '1M')->data;
$range = Preferences::get('viewRange', '1M')->data; $begin = Navigation::subtractPeriod($start, $range, 3);
$begin = Navigation::subtractPeriod($start, $range, 3);
// get average amount available. // get average amount available.
$total = '0'; $total = '0';
$count = 0; $count = 0;
$currentStart = clone $begin; $currentStart = clone $begin;
while ($currentStart < $start) { while ($currentStart < $start) {
$currentEnd = Navigation::endOfPeriod($currentStart, $range); $currentEnd = Navigation::endOfPeriod($currentStart, $range);
$total = bcadd($total, $this->repository->getAvailableBudget($currency, $currentStart, $currentEnd)); $total = bcadd($total, $this->repository->getAvailableBudget($currency, $currentStart, $currentEnd));
$currentStart = Navigation::addPeriod($currentStart, $range, 0); $currentStart = Navigation::addPeriod($currentStart, $range, 0);
$count++; $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);
} }
$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')); return view('budgets.info', compact('result', 'begin', 'currentEnd'));
@ -573,9 +573,7 @@ class BudgetController extends Controller
$start = Navigation::startOfPeriod($start, $range); $start = Navigation::startOfPeriod($start, $range);
$end = Navigation::endOfX(new Carbon, $range, null); $end = Navigation::endOfX(new Carbon, $range, null);
$entries = new Collection; $entries = new Collection;
$cache = new CacheProperties;
// properties for cache
$cache = new CacheProperties;
$cache->addProperty($start); $cache->addProperty($start);
$cache->addProperty($end); $cache->addProperty($end);
$cache->addProperty('no-budget-period-entries'); $cache->addProperty('no-budget-period-entries');
@ -588,8 +586,6 @@ class BudgetController extends Controller
while ($end >= $start) { while ($end >= $start) {
$end = Navigation::startOfPeriod($end, $range); $end = Navigation::startOfPeriod($end, $range);
$currentEnd = Navigation::endOfPeriod($end, $range); $currentEnd = Navigation::endOfPeriod($end, $range);
// count journals without budget in this period:
/** @var JournalCollectorInterface $collector */ /** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class); $collector = app(JournalCollectorInterface::class);
$collector->setAllAssetAccounts()->setRange($end, $currentEnd)->withoutBudget()->withOpposingAccount()->setTypes([TransactionType::WITHDRAWAL]); $collector->setAllAssetAccounts()->setRange($end, $currentEnd)->withoutBudget()->withOpposingAccount()->setTypes([TransactionType::WITHDRAWAL]);
@ -598,15 +594,7 @@ class BudgetController extends Controller
$journals = $set->count(); $journals = $set->count();
$dateStr = $end->format('Y-m-d'); $dateStr = $end->format('Y-m-d');
$dateName = Navigation::periodShow($end, $range); $dateName = Navigation::periodShow($end, $range);
$entries->push( $entries->push(['string' => $dateStr, 'name' => $dateName, 'count' => $journals, 'sum' => $sum, 'date' => clone $end,]);
[
'string' => $dateStr,
'name' => $dateName,
'count' => $journals,
'sum' => $sum,
'date' => clone $end,
]
);
$end = Navigation::subtractPeriod($end, $range, 1); $end = Navigation::subtractPeriod($end, $range, 1);
} }
$cache->store($entries); $cache->store($entries);

View File

@ -261,6 +261,7 @@ class ReportController extends Controller
* @param ReportFormRequest $request * @param ReportFormRequest $request
* *
* @return RedirectResponse|\Illuminate\Routing\Redirector * @return RedirectResponse|\Illuminate\Routing\Redirector
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/ */
public function postIndex(ReportFormRequest $request) public function postIndex(ReportFormRequest $request)
{ {
@ -299,7 +300,7 @@ class ReportController extends Controller
return redirect(route('reports.index')); return redirect(route('reports.index'));
} }
if ($end < $start) { if ($request->getEndDate() < $request->getStartDate()) {
return view('error')->with('message', trans('firefly.end_after_start_date')); return view('error')->with('message', trans('firefly.end_after_start_date'));
} }

View File

@ -49,6 +49,7 @@ class Sandstorm
* *
* @return mixed * @return mixed
* @throws FireflyException * @throws FireflyException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/ */
public function handle(Request $request, Closure $next, $guard = null) public function handle(Request $request, Closure $next, $guard = null)
{ {

View File

@ -165,6 +165,7 @@ class ImportAccount
/** /**
* @return Account * @return Account
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/ */
private function findExistingObject(): Account private function findExistingObject(): Account
{ {

View File

@ -125,7 +125,7 @@ class AbnAmroDescription implements SpecificInterface
/** /**
* Parses the current description in SEPA format * Parses the current description in SEPA format
* * @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @return bool true if the description is SEPA format, false otherwise * @return bool true if the description is SEPA format, false otherwise
*/ */
protected function parseSepaDescription() protected function parseSepaDescription()

View File

@ -394,6 +394,21 @@ abstract class BunqRequest
return false; 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 * @param array $response
* *
@ -447,10 +462,7 @@ abstract class BunqRequest
ksort($verifyHeaders); ksort($verifyHeaders);
// add them to data to sign: // add them to data to sign:
foreach ($verifyHeaders as $header => $value) { $dataToVerify .= $this->joinHeaders($verifyHeaders);
$dataToVerify .= $header . ': ' . trim($value) . "\n";
}
$signature = $headers['x-bunq-server-signature'][0]; $signature = $headers['x-bunq-server-signature'][0];
$dataToVerify .= "\n" . $body; $dataToVerify .= "\n" . $body;
$result = openssl_verify($dataToVerify, base64_decode($signature), $this->serverPublicKey->getPublicKey(), OPENSSL_ALGO_SHA256); $result = openssl_verify($dataToVerify, base64_decode($signature), $this->serverPublicKey->getPublicKey(), OPENSSL_ALGO_SHA256);
@ -462,6 +474,8 @@ abstract class BunqRequest
} }
if (!is_int($result)) { if (!is_int($result)) {
Log::error(sprintf('Result of verification is a boolean (%d), return false.', $result)); Log::error(sprintf('Result of verification is a boolean (%d), return false.', $result));
return false;
} }
Log::info('Signature is a match, return true.'); Log::info('Signature is a match, return true.');

View File

@ -176,7 +176,7 @@ class Preferences
/** /**
* @param $name * @param $name
* @param string $value * @param $value
* *
* @return Preference * @return Preference
*/ */

View File

@ -51,6 +51,14 @@ class Modifier
return $compare === $expected; 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 public static function apply(array $modifier, Transaction $transaction): bool
{ {
switch ($modifier['type']) { switch ($modifier['type']) {