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 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);

View File

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

View File

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

View File

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

View File

@ -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()

View File

@ -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.');

View File

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

View File

@ -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']) {