Code cleanup.

This commit is contained in:
James Cole 2016-05-02 20:49:19 +02:00
parent 5e1167b8ae
commit 3344bb7263
21 changed files with 105 additions and 89 deletions

View File

@ -16,6 +16,7 @@ use FireflyIII\Helpers\Collection\Budget as BudgetCollection;
use FireflyIII\Helpers\Collection\BudgetLine;
use FireflyIII\Models\Budget;
use FireflyIII\Models\LimitRepetition;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use Illuminate\Support\Collection;
/**
@ -36,8 +37,8 @@ class BudgetReportHelper implements BudgetReportHelperInterface
public function getBudgetReport(Carbon $start, Carbon $end, Collection $accounts): BudgetCollection
{
$object = new BudgetCollection;
/** @var \FireflyIII\Repositories\Budget\BudgetRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
/** @var BudgetRepositoryInterface $repository */
$repository = app(BudgetRepositoryInterface::class);
$set = $repository->getBudgets();
$allRepetitions = $repository->getAllBudgetLimitRepetitions($start, $end);
$allTotalSpent = $repository->spentAllPerDayForAccounts($accounts, $start, $end);
@ -116,8 +117,8 @@ class BudgetReportHelper implements BudgetReportHelperInterface
*/
public function getBudgetsWithExpenses(Carbon $start, Carbon $end, Collection $accounts): Collection
{
/** @var \FireflyIII\Repositories\Budget\BudgetRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
/** @var BudgetRepositoryInterface $repository */
$repository = app(BudgetRepositoryInterface::class);
$budgets = $repository->getActiveBudgets();
$set = new Collection;

View File

@ -14,7 +14,9 @@ use FireflyIII\Models\Bill;
use FireflyIII\Models\Category;
use FireflyIII\Models\Tag;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\JoinClause;
@ -64,8 +66,8 @@ class ReportHelper implements ReportHelperInterface
*/
public function getBillReport(Carbon $start, Carbon $end, Collection $accounts): BillCollection
{
/** @var \FireflyIII\Repositories\Bill\BillRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Bill\BillRepositoryInterface');
/** @var BillRepositoryInterface $repository */
$repository = app(BillRepositoryInterface::class);
$bills = $repository->getBillsForAccounts($accounts);
$journals = $repository->getAllJournalsInRange($bills, $start, $end);
$collection = new BillCollection;
@ -109,8 +111,8 @@ class ReportHelper implements ReportHelperInterface
*/
public function getCategoriesWithExpenses(Carbon $start, Carbon $end, Collection $accounts): Collection
{
/** @var \FireflyIII\Repositories\Category\CategoryRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Category\CategoryRepositoryInterface');
/** @var CategoryRepositoryInterface $repository */
$repository = app(CategoryRepositoryInterface::class);
$collection = $repository->earnedForAccountsPerMonth($accounts, $start, $end);
$second = $repository->spentForAccountsPerMonth($accounts, $start, $end);
$collection = $collection->merge($second);
@ -147,7 +149,7 @@ class ReportHelper implements ReportHelperInterface
* GET CATEGORIES:
*/
/** @var \FireflyIII\Repositories\Category\CategoryRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Category\CategoryRepositoryInterface');
$repository = app(CategoryRepositoryInterface::class);
$set = $repository->spentForAccountsPerMonth($accounts, $start, $end);
foreach ($set as $category) {
@ -209,7 +211,7 @@ class ReportHelper implements ReportHelperInterface
public function listOfMonths(Carbon $date): array
{
/** @var FiscalHelperInterface $fiscalHelper */
$fiscalHelper = app('FireflyIII\Helpers\FiscalHelperInterface');
$fiscalHelper = app(FiscalHelperInterface::class);
$start = clone $date;
$start->startOfMonth();
$end = Carbon::now();

View File

@ -230,7 +230,7 @@ class CategoryController extends Controller
$pageSize = Preferences::get('transactionPageSize', 50)->data;
$set = $repository->getJournalsInRange($category, $start, $end, $page, $pageSize);
$count = $repository->countJournalsInRange($category, $start, $end);
$count = $repository->countJournals($category, $start, $end);
$journals = new LengthAwarePaginator($set, $count, $pageSize, $page);
$journals->setPath('categories/show/' . $category->id . '/' . $date);

View File

@ -4,6 +4,7 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
use FireflyIII\Generator\Chart\Account\AccountChartGeneratorInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
@ -30,7 +31,7 @@ class AccountController extends Controller
{
parent::__construct();
// create chart generator:
$this->generator = app('FireflyIII\Generator\Chart\Account\AccountChartGeneratorInterface');
$this->generator = app(AccountChartGeneratorInterface::class);
}

View File

@ -4,6 +4,7 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
use FireflyIII\Generator\Chart\Bill\BillChartGeneratorInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Bill;
use FireflyIII\Models\TransactionJournal;
@ -29,7 +30,7 @@ class BillController extends Controller
{
parent::__construct();
// create chart generator:
$this->generator = app('FireflyIII\Generator\Chart\Bill\BillChartGeneratorInterface');
$this->generator = app(BillChartGeneratorInterface::class);
}
/**

View File

@ -4,6 +4,7 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
use FireflyIII\Generator\Chart\Budget\BudgetChartGeneratorInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Budget;
use FireflyIII\Models\LimitRepetition;
@ -34,7 +35,7 @@ class BudgetController extends Controller
{
parent::__construct();
// create chart generator:
$this->generator = app('FireflyIII\Generator\Chart\Budget\BudgetChartGeneratorInterface');
$this->generator = app(BudgetChartGeneratorInterface::class);
}
/**
@ -331,7 +332,7 @@ class BudgetController extends Controller
}
/** @var BudgetRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
$repository = app(BudgetRepositoryInterface::class);
// loop over period, add by users range:
$current = clone $start;
$viewRange = Preferences::get('viewRange', '1M')->data;

View File

@ -5,6 +5,7 @@ namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
use FireflyIII\Generator\Chart\Category\CategoryChartGeneratorInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Category;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
@ -29,7 +30,7 @@ class CategoryController extends Controller
const KEEP_POSITIVE = 1;
/** @var \FireflyIII\Generator\Chart\Category\CategoryChartGeneratorInterface */
/** @var CategoryChartGeneratorInterface */
protected $generator;
/**
@ -39,7 +40,7 @@ class CategoryController extends Controller
{
parent::__construct();
// create chart generator:
$this->generator = app('FireflyIII\Generator\Chart\Category\CategoryChartGeneratorInterface');
$this->generator = app(CategoryChartGeneratorInterface::class);
}
@ -198,7 +199,7 @@ class CategoryController extends Controller
public function multiYear(string $reportType, Carbon $start, Carbon $end, Collection $accounts, Collection $categories)
{
/** @var CRI $repository */
$repository = app('FireflyIII\Repositories\Category\CategoryRepositoryInterface');
$repository = app(CRI::class);
// chart properties for cache:
$cache = new CacheProperties();
@ -295,7 +296,7 @@ class CategoryController extends Controller
}
/** @var SingleCategoryRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface');
$repository = app(SingleCategoryRepositoryInterface::class);
// loop over period, add by users range:
$current = clone $start;
$viewRange = Preferences::get('viewRange', '1M')->data;

View File

@ -3,6 +3,7 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Controllers\Chart;
use FireflyIII\Generator\Chart\PiggyBank\PiggyBankChartGeneratorInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\PiggyBankEvent;
@ -20,7 +21,7 @@ use Response;
class PiggyBankController extends Controller
{
/** @var \FireflyIII\Generator\Chart\PiggyBank\PiggyBankChartGeneratorInterface */
/** @var PiggyBankChartGeneratorInterface */
protected $generator;
/**
@ -30,7 +31,7 @@ class PiggyBankController extends Controller
{
parent::__construct();
// create chart generator:
$this->generator = app('FireflyIII\Generator\Chart\PiggyBank\PiggyBankChartGeneratorInterface');
$this->generator = app(PiggyBankChartGeneratorInterface::class);
}
/**

View File

@ -5,6 +5,7 @@ namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
use FireflyIII\Generator\Chart\Report\ReportChartGeneratorInterface;
use FireflyIII\Helpers\Report\ReportQueryInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Support\CacheProperties;
@ -20,7 +21,7 @@ use Steam;
class ReportController extends Controller
{
/** @var \FireflyIII\Generator\Chart\Report\ReportChartGeneratorInterface */
/** @var ReportChartGeneratorInterface */
protected $generator;
/**
@ -30,7 +31,7 @@ class ReportController extends Controller
{
parent::__construct();
// create chart generator:
$this->generator = app('FireflyIII\Generator\Chart\Report\ReportChartGeneratorInterface');
$this->generator = app(ReportChartGeneratorInterface::class);
}
/**

View File

@ -42,8 +42,8 @@ class CsvController extends Controller
throw new FireflyException('CSV Import is not enabled.');
}
$this->wizard = app('FireflyIII\Helpers\Csv\WizardInterface');
$this->data = app('FireflyIII\Helpers\Csv\Data');
$this->wizard = app(WizardInterface::class);
$this->data = app(Data::class);
}
@ -313,7 +313,7 @@ class CsvController extends Controller
Log::debug('Created importer');
/** @var Importer $importer */
$importer = app('FireflyIII\Helpers\Csv\Importer');
$importer = app(Importer::class);
$importer->setData($this->data);
$importer->run();
Log::debug('Done importing!');

View File

@ -14,6 +14,7 @@ namespace FireflyIII\Http\Controllers\Popup;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collection\BalanceLine;
use FireflyIII\Helpers\Csv\Mapper\Budget;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
@ -83,7 +84,7 @@ class ReportController extends Controller
$role = intval($attributes['role']);
/** @var BudgetRepositoryInterface $budgetRepository */
$budgetRepository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
$budgetRepository = app(BudgetRepositoryInterface::class);
$budget = $budgetRepository->find(intval($attributes['budgetId']));
/** @var AccountRepositoryInterface $accountRepository */
@ -133,7 +134,7 @@ class ReportController extends Controller
// then search for expenses in the given period
// list them in some table format.
/** @var BudgetRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
$repository = app(BudgetRepositoryInterface::class);
$budget = $repository->find(intval($attributes['budgetId']));
if (is_null($budget->id)) {
$journals = $repository->getWithoutBudgetForAccounts($attributes['accounts'], $attributes['startDate'], $attributes['endDate']);
@ -158,7 +159,7 @@ class ReportController extends Controller
private function categoryEntry(array $attributes): string
{
/** @var SingleCategoryRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface');
$repository = app(SingleCategoryRepositoryInterface::class);
$category = $repository->find(intval($attributes['categoryId']));
$journals = $repository->getJournalsForAccountsInRange($category, $attributes['accounts'], $attributes['startDate'], $attributes['endDate']);
$view = view('popup.report.category-entry', compact('journals', 'category'))->render();

View File

@ -9,6 +9,8 @@ use FireflyIII\Helpers\Report\ReportHelperInterface;
use FireflyIII\Models\Account;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
use Preferences;
@ -44,9 +46,9 @@ class ReportController extends Controller
parent::__construct();
$this->helper = $helper;
$this->accountHelper = app('FireflyIII\Helpers\Report\AccountReportHelperInterface');
$this->budgetHelper = app('FireflyIII\Helpers\Report\BudgetReportHelperInterface');
$this->balanceHelper = app('FireflyIII\Helpers\Report\BalanceReportHelperInterface');
$this->accountHelper = app(AccountReportHelperInterface::class);
$this->budgetHelper = app(BudgetReportHelperInterface::class);
$this->balanceHelper = app(BalanceReportHelperInterface::class);
View::share('title', trans('firefly.reports'));
View::share('mainTitleIcon', 'fa-line-chart');
@ -269,8 +271,8 @@ class ReportController extends Controller
$incomeTopLength = 8;
$expenseTopLength = 8;
// list of users stuff:
$budgets = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface')->getActiveBudgets();
$categories = app('FireflyIII\Repositories\Category\CategoryRepositoryInterface')->getCategories();
$budgets = app(BudgetRepositoryInterface::class)->getActiveBudgets();
$categories = app(CategoryRepositoryInterface::class)->getCategories();
$accountReport = $this->accountHelper->getAccountReport($start, $end, $accounts);
$incomes = $this->helper->getIncomeReport($start, $end, $accounts);
$expenses = $this->helper->getExpenseReport($start, $end, $accounts);

View File

@ -6,6 +6,7 @@ namespace FireflyIII\Http\Middleware;
use Carbon\Carbon;
use Closure;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
@ -66,8 +67,8 @@ class Range
Session::put('end', $end);
}
if (!Session::has('first')) {
/** @var \FireflyIII\Repositories\Journal\JournalRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Journal\JournalRepositoryInterface');
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$journal = $repository->first();
if (!is_null($journal->id)) {
Session::put('first', $journal->date);

View File

@ -3,6 +3,7 @@ declare(strict_types = 1);
namespace FireflyIII\Repositories\Attachment;
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Models\Attachment;
use FireflyIII\User;
use Illuminate\Support\Collection;
@ -34,8 +35,8 @@ class AttachmentRepository implements AttachmentRepositoryInterface
*/
public function destroy(Attachment $attachment): bool
{
/** @var \FireflyIII\Helpers\Attachments\AttachmentHelperInterface $helper */
$helper = app('FireflyIII\Helpers\Attachments\AttachmentHelperInterface');
/** @var AttachmentHelperInterface $helper */
$helper = app(AttachmentHelperInterface::class);
$file = $helper->getAttachmentLocation($attachment);
unlink($file);

View File

@ -33,27 +33,24 @@ class SingleCategoryRepository extends ComponentRepository implements SingleCate
}
/**
* @param Category $category
* @param Category $category
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return int
*/
public function countJournals(Category $category): int
public function countJournals(Category $category, Carbon $start = null, Carbon $end = null): int
{
return $category->transactionjournals()->count();
$query = $category->transactionjournals();
if (!is_null($start)) {
$query->after($start);
}
if (!is_null($end)) {
$query->before($end);
}
}
return $query->count();
/**
* @param Category $category
*
* @param Carbon $start
* @param Carbon $end
*
* @return int
*/
public function countJournalsInRange(Category $category, Carbon $start, Carbon $end): int
{
return $category->transactionjournals()->before($end)->after($start)->count();
}
/**
@ -153,11 +150,7 @@ class SingleCategoryRepository extends ComponentRepository implements SingleCate
{
$offset = $page > 0 ? $page * $pageSize : 0;
return $category->transactionjournals()->expanded()->take($pageSize)->offset($offset)
->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.order', 'ASC')
->orderBy('transaction_journals.id', 'DESC')
->get(TransactionJournal::queryFields());
return $category->transactionjournals()->expanded()->take($pageSize)->offset($offset)->get(TransactionJournal::queryFields());
}

View File

@ -16,21 +16,13 @@ interface SingleCategoryRepositoryInterface
{
/**
* @param Category $category
* @param Category $category
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return int
*/
public function countJournals(Category $category): int;
/**
* @param Category $category
*
* @param Carbon $start
* @param Carbon $end
*
* @return int
*/
public function countJournalsInRange(Category $category, Carbon $start, Carbon $end): int;
public function countJournals(Category $category, Carbon $start = null, Carbon $end = null): int;
/**
* @param Category $category

View File

@ -68,16 +68,26 @@ class ExportJobRepository implements ExportJobRepositoryInterface
*/
public function create(): ExportJob
{
$exportJob = new ExportJob;
$exportJob->user()->associate($this->user);
/*
* In theory this random string could give db error.
*/
$exportJob->key = Str::random(12);
$exportJob->status = 'export_status_never_started';
$exportJob->save();
$count = 0;
while ($count < 30) {
$key = Str::random(12);
$existing = $this->findByKey($key);
if (is_null($existing->id)) {
$exportJob = new ExportJob;
$exportJob->user()->associate($this->user);
$exportJob->key = Str::random(12);
$exportJob->status = 'export_status_never_started';
$exportJob->save();
// breaks the loop:
return $exportJob;
}
$count++;
}
return new ExportJob;
return $exportJob;
}
/**
@ -90,7 +100,12 @@ class ExportJobRepository implements ExportJobRepositoryInterface
*/
public function findByKey(string $key): ExportJob
{
return $this->user->exportJobs()->where('key', $key)->first();
$result = $this->user->exportJobs()->where('key', $key)->first();
if (is_null($result)) {
return new ExportJob;
}
return $result;
}
}

View File

@ -14,6 +14,7 @@ use FireflyIII\Models\Tag;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Pagination\LengthAwarePaginator;
@ -309,8 +310,8 @@ class JournalRepository implements JournalRepositoryInterface
*/
private function saveTags(TransactionJournal $journal, array $array): bool
{
/** @var \FireflyIII\Repositories\Tag\TagRepositoryInterface $tagRepository */
$tagRepository = app('FireflyIII\Repositories\Tag\TagRepositoryInterface');
/** @var TagRepositoryInterface $tagRepository */
$tagRepository = app(TagRepositoryInterface::class);
foreach ($array as $name) {
if (strlen(trim($name)) > 0) {
@ -434,8 +435,8 @@ class JournalRepository implements JournalRepositoryInterface
private function updateTags(TransactionJournal $journal, array $array): bool
{
// create tag repository
/** @var \FireflyIII\Repositories\Tag\TagRepositoryInterface $tagRepository */
$tagRepository = app('FireflyIII\Repositories\Tag\TagRepositoryInterface');
/** @var TagRepositoryInterface $tagRepository */
$tagRepository = app(TagRepositoryInterface::class);
// find or create all tags:

View File

@ -46,7 +46,7 @@ class SetBudget implements ActionInterface
public function act(TransactionJournal $journal): bool
{
/** @var BudgetRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
$repository = app(BudgetRepositoryInterface::class);
$search = $this->action->action_value;
$budgets = $repository->getActiveBudgets();
$budget = $budgets->filter(

View File

@ -12,6 +12,7 @@ namespace FireflyIII\Support\Events;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
/**
* Class BillScanner
@ -25,8 +26,8 @@ class BillScanner
*/
public static function scan(TransactionJournal $journal)
{
/** @var \FireflyIII\Repositories\Bill\BillRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Bill\BillRepositoryInterface');
/** @var BillRepositoryInterface $repository */
$repository = app(BillRepositoryInterface::class);
$list = $journal->user->bills()->where('active', 1)->where('automatch', 1)->get();
/** @var \FireflyIII\Models\Bill $bill */

View File

@ -60,7 +60,7 @@ class FireflyValidator extends Validator
$secret = Session::get('two-factor-secret');
/** @var Google2FA $google2fa */
$google2fa = app('PragmaRX\Google2FA\Google2FA');
$google2fa = app(Google2FA::class);
return $google2fa->verifyKey($secret, $value);
}
@ -138,7 +138,7 @@ class FireflyValidator extends Validator
return true;
case 'set_budget':
/** @var BudgetRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
$repository = app(BudgetRepositoryInterface::class);
$budgets = $repository->getBudgets();
// count budgets, should have at least one
$count = $budgets->filter(