From 724f42369249dec404cbe119191470bb48cd55ac Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 20 May 2016 09:45:24 +0200 Subject: [PATCH] Move some stuff around. --- app/Crud/Account/AccountCrud.php | 39 +++++++++++++------ app/Crud/Account/AccountCrudInterface.php | 8 ++-- app/Helpers/Csv/Converter/AccountId.php | 9 ++--- .../Csv/Converter/AssetAccountIban.php | 5 ++- .../Csv/Converter/AssetAccountName.php | 7 +--- .../Csv/Converter/AssetAccountNumber.php | 7 +--- .../Csv/Converter/OpposingAccountIban.php | 3 +- .../Csv/Converter/OpposingAccountId.php | 8 ++-- .../Csv/Converter/OpposingAccountName.php | 6 +-- .../Csv/PostProcessing/AssetAccount.php | 3 -- app/Http/Controllers/AccountController.php | 5 +-- .../Controllers/Popup/ReportController.php | 16 ++++---- .../Account/AccountRepository.php | 15 ------- .../Account/AccountRepositoryInterface.php | 7 ---- 14 files changed, 58 insertions(+), 80 deletions(-) diff --git a/app/Crud/Account/AccountCrud.php b/app/Crud/Account/AccountCrud.php index cd40e0ca17..a30dca3e70 100644 --- a/app/Crud/Account/AccountCrud.php +++ b/app/Crud/Account/AccountCrud.php @@ -64,23 +64,18 @@ class AccountCrud implements AccountCrudInterface } /** - * @param Account $account + * @param $accountId * - * @return TransactionJournal|null + * @return Account */ - public function openingBalanceTransaction(Account $account): TransactionJournal + public function find(int $accountId): Account { - $journal = TransactionJournal - ::sortCorrectly() - ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') - ->where('transactions.account_id', $account->id) - ->transactionTypes([TransactionType::OPENING_BALANCE]) - ->first(['transaction_journals.*']); - if (is_null($journal)) { - return new TransactionJournal; + $account = $this->user->accounts()->find($accountId); + if (is_null($account)) { + $account = new Account; } - return $journal; + return $account; } /** @@ -339,4 +334,24 @@ class AccountCrud implements AccountCrudInterface } } + + /** + * @param Account $account + * + * @return TransactionJournal|null + */ + private function openingBalanceTransaction(Account $account): TransactionJournal + { + $journal = TransactionJournal + ::sortCorrectly() + ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') + ->where('transactions.account_id', $account->id) + ->transactionTypes([TransactionType::OPENING_BALANCE]) + ->first(['transaction_journals.*']); + if (is_null($journal)) { + return new TransactionJournal; + } + + return $journal; + } } \ No newline at end of file diff --git a/app/Crud/Account/AccountCrudInterface.php b/app/Crud/Account/AccountCrudInterface.php index a00d494a6b..4c2b3a8f1d 100644 --- a/app/Crud/Account/AccountCrudInterface.php +++ b/app/Crud/Account/AccountCrudInterface.php @@ -13,7 +13,6 @@ namespace FireflyIII\Crud\Account; use FireflyIII\Models\Account; use FireflyIII\Models\AccountMeta; -use FireflyIII\Models\TransactionJournal; /** * Interface AccountCrudInterface @@ -31,12 +30,11 @@ interface AccountCrudInterface public function destroy(Account $account, Account $moveTo): bool; /** + * @param int $accountId * - * @param Account $account - * - * @return TransactionJournal + * @return Account */ - public function openingBalanceTransaction(Account $account) : TransactionJournal; + public function find(int $accountId): Account; /** * @param array $data diff --git a/app/Helpers/Csv/Converter/AccountId.php b/app/Helpers/Csv/Converter/AccountId.php index c1ec021dee..e69544357e 100644 --- a/app/Helpers/Csv/Converter/AccountId.php +++ b/app/Helpers/Csv/Converter/AccountId.php @@ -3,7 +3,6 @@ declare(strict_types = 1); namespace FireflyIII\Helpers\Csv\Converter; use FireflyIII\Models\Account; -use FireflyIII\Repositories\Account\AccountRepositoryInterface; /** * Class AccountId @@ -18,10 +17,10 @@ class AccountId extends BasicConverter implements ConverterInterface */ public function convert(): Account { - /** @var AccountRepositoryInterface $repository */ - $repository = app(AccountRepositoryInterface::class); - $var = isset($this->mapped[$this->index][$this->value]) ? $this->mapped[$this->index][$this->value] : $this->value; - $account = $repository->find($var); + + $crud = app('FireflyIII\Crud\Account\AccountCrudInterface'); + $var = isset($this->mapped[$this->index][$this->value]) ? $this->mapped[$this->index][$this->value] : $this->value; + $account = $crud->find($var); return $account; } diff --git a/app/Helpers/Csv/Converter/AssetAccountIban.php b/app/Helpers/Csv/Converter/AssetAccountIban.php index 2df18d4f08..a404355c33 100644 --- a/app/Helpers/Csv/Converter/AssetAccountIban.php +++ b/app/Helpers/Csv/Converter/AssetAccountIban.php @@ -23,17 +23,18 @@ class AssetAccountIban extends BasicConverter implements ConverterInterface { /** @var AccountRepositoryInterface $repository */ $repository = app(AccountRepositoryInterface::class); + $crud = app('FireflyIII\Crud\Account\AccountCrudInterface'); // is mapped? Then it's easy! if (isset($this->mapped[$this->index][$this->value])) { - $account = $repository->find(intval($this->mapped[$this->index][$this->value])); + $account = $crud->find(intval($this->mapped[$this->index][$this->value])); return $account; } if (strlen($this->value) > 0) { - $account = $this->searchOrCreate($repository); + $account = $this->searchOrCreate($repository, $crud); return $account; } diff --git a/app/Helpers/Csv/Converter/AssetAccountName.php b/app/Helpers/Csv/Converter/AssetAccountName.php index c60ad87aa2..932ff621ec 100644 --- a/app/Helpers/Csv/Converter/AssetAccountName.php +++ b/app/Helpers/Csv/Converter/AssetAccountName.php @@ -4,7 +4,6 @@ namespace FireflyIII\Helpers\Csv\Converter; use Auth; use Carbon\Carbon; -use FireflyIII\Crud\Account\AccountCrudInterface; use FireflyIII\Models\Account; use FireflyIII\Repositories\Account\AccountRepositoryInterface; @@ -23,12 +22,10 @@ class AssetAccountName extends BasicConverter implements ConverterInterface { /** @var AccountRepositoryInterface $repository */ $repository = app(AccountRepositoryInterface::class); - - /** @var AccountCrudInterface $crud */ - $crud = app(AccountCrudInterface::class); + $crud = app('FireflyIII\Crud\Account\AccountCrudInterface'); if (isset($this->mapped[$this->index][$this->value])) { - $account = $repository->find(intval($this->mapped[$this->index][$this->value])); + $account = $crud->find(intval($this->mapped[$this->index][$this->value])); return $account; } diff --git a/app/Helpers/Csv/Converter/AssetAccountNumber.php b/app/Helpers/Csv/Converter/AssetAccountNumber.php index ad742b3904..550d7d7e63 100644 --- a/app/Helpers/Csv/Converter/AssetAccountNumber.php +++ b/app/Helpers/Csv/Converter/AssetAccountNumber.php @@ -13,7 +13,6 @@ namespace FireflyIII\Helpers\Csv\Converter; use Auth; use Carbon\Carbon; -use FireflyIII\Crud\Account\AccountCrudInterface; use FireflyIII\Models\Account; use FireflyIII\Repositories\Account\AccountRepositoryInterface; @@ -32,13 +31,11 @@ class AssetAccountNumber extends BasicConverter implements ConverterInterface { /** @var AccountRepositoryInterface $repository */ $repository = app(AccountRepositoryInterface::class); - - /** @var AccountCrudInterface $crud */ - $crud = app(AccountCrudInterface::class); + $crud = app('FireflyIII\Crud\Account\AccountCrudInterface'); // is mapped? Then it's easy! if (isset($this->mapped[$this->index][$this->value])) { - $account = $repository->find(intval($this->mapped[$this->index][$this->value])); + $account = $crud->find(intval($this->mapped[$this->index][$this->value])); return $account; } diff --git a/app/Helpers/Csv/Converter/OpposingAccountIban.php b/app/Helpers/Csv/Converter/OpposingAccountIban.php index 7702419a39..9108fde3fe 100644 --- a/app/Helpers/Csv/Converter/OpposingAccountIban.php +++ b/app/Helpers/Csv/Converter/OpposingAccountIban.php @@ -22,9 +22,10 @@ class OpposingAccountIban extends BasicConverter implements ConverterInterface { /** @var AccountRepositoryInterface $repository */ $repository = app(AccountRepositoryInterface::class); + $crud = app('FireflyIII\Crud\Account\AccountCrudInterface'); if (isset($this->mapped[$this->index][$this->value])) { - $account = $repository->find($this->mapped[$this->index][$this->value]); + $account = $crud->find($this->mapped[$this->index][$this->value]); return $account; } diff --git a/app/Helpers/Csv/Converter/OpposingAccountId.php b/app/Helpers/Csv/Converter/OpposingAccountId.php index a28fbe0e2b..2ebe07dbd8 100644 --- a/app/Helpers/Csv/Converter/OpposingAccountId.php +++ b/app/Helpers/Csv/Converter/OpposingAccountId.php @@ -3,7 +3,6 @@ declare(strict_types = 1); namespace FireflyIII\Helpers\Csv\Converter; use FireflyIII\Models\Account; -use FireflyIII\Repositories\Account\AccountRepositoryInterface; /** * Class OpposingAccountId @@ -19,10 +18,9 @@ class OpposingAccountId extends BasicConverter implements ConverterInterface */ public function convert(): Account { - /** @var AccountRepositoryInterface $repository */ - $repository = app(AccountRepositoryInterface::class); - $value = isset($this->mapped[$this->index][$this->value]) ? $this->mapped[$this->index][$this->value] : $this->value; - $account = $repository->find($value); + $crud = app('FireflyIII\Crud\Account\AccountCrudInterface'); + $value = isset($this->mapped[$this->index][$this->value]) ? $this->mapped[$this->index][$this->value] : $this->value; + $account = $crud->find($value); return $account; } diff --git a/app/Helpers/Csv/Converter/OpposingAccountName.php b/app/Helpers/Csv/Converter/OpposingAccountName.php index bda3aa1938..9890b7fb39 100644 --- a/app/Helpers/Csv/Converter/OpposingAccountName.php +++ b/app/Helpers/Csv/Converter/OpposingAccountName.php @@ -3,7 +3,6 @@ declare(strict_types = 1); namespace FireflyIII\Helpers\Csv\Converter; use FireflyIII\Models\Account; -use FireflyIII\Repositories\Account\AccountRepositoryInterface; /** * Class OpposingAccountName @@ -20,11 +19,10 @@ class OpposingAccountName extends BasicConverter implements ConverterInterface */ public function convert() { - /** @var AccountRepositoryInterface $repository */ - $repository = app(AccountRepositoryInterface::class); + $crud = app('FireflyIII\Crud\Account\AccountCrudInterface'); if (isset($this->mapped[$this->index][$this->value])) { - $account = $repository->find($this->mapped[$this->index][$this->value]); + $account = $crud->find($this->mapped[$this->index][$this->value]); return $account; } diff --git a/app/Helpers/Csv/PostProcessing/AssetAccount.php b/app/Helpers/Csv/PostProcessing/AssetAccount.php index 4a4b4a3525..077848a594 100644 --- a/app/Helpers/Csv/PostProcessing/AssetAccount.php +++ b/app/Helpers/Csv/PostProcessing/AssetAccount.php @@ -7,7 +7,6 @@ use Carbon\Carbon; use FireflyIII\Crud\Account\AccountCrudInterface; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; -use FireflyIII\Repositories\Account\AccountRepositoryInterface; use Validator; /** @@ -246,8 +245,6 @@ class AssetAccount implements PostProcessorInterface } } // create new if not exists and return that one: - /** @var AccountRepositoryInterface $repository */ - $repository = app(AccountRepositoryInterface::class); $accountData = [ 'name' => $accountNumber, 'accountType' => 'asset', diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index 62a29fa8cf..6ce37dfc89 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -84,18 +84,17 @@ class AccountController extends Controller } /** - * @param ARI $repository * @param AccountCrudInterface $crud * @param Account $account * * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ - public function destroy(ARI $repository, AccountCrudInterface $crud, Account $account) + public function destroy(AccountCrudInterface $crud, Account $account) { $type = $account->accountType->type; $typeName = config('firefly.shortNamesByFullName.' . $type); $name = $account->name; - $moveTo = $repository->find(intval(Input::get('move_account_before_delete'))); + $moveTo = $crud->find(intval(Input::get('move_account_before_delete'))); $crud->destroy($account, $moveTo); diff --git a/app/Http/Controllers/Popup/ReportController.php b/app/Http/Controllers/Popup/ReportController.php index 66923ff735..1b5a7afe0b 100644 --- a/app/Http/Controllers/Popup/ReportController.php +++ b/app/Http/Controllers/Popup/ReportController.php @@ -88,10 +88,8 @@ class ReportController extends Controller /** @var BudgetRepositoryInterface $budgetRepository */ $budgetRepository = app(BudgetRepositoryInterface::class); $budget = $budgetRepository->find(intval($attributes['budgetId'])); - - /** @var AccountRepositoryInterface $accountRepository */ - $accountRepository = app(AccountRepositoryInterface::class); - $account = $accountRepository->find(intval($attributes['accountId'])); + $crud = app('FireflyIII\Crud\Account\AccountCrudInterface'); + $account = $crud->find(intval($attributes['accountId'])); switch (true) { case ($role === BalanceLine::ROLE_DEFAULTROLE && !is_null($budget->id)): @@ -183,7 +181,8 @@ class ReportController extends Controller { /** @var AccountRepositoryInterface $repository */ $repository = app(AccountRepositoryInterface::class); - $account = $repository->find(intval($attributes['accountId'])); + $crud = app('FireflyIII\Crud\Account\AccountCrudInterface'); + $account = $crud->find(intval($attributes['accountId'])); $types = [TransactionType::WITHDRAWAL, TransactionType::TRANSFER]; $journals = $repository->journalsInPeriod($attributes['accounts'], $types, $attributes['startDate'], $attributes['endDate']); @@ -213,7 +212,8 @@ class ReportController extends Controller { /** @var AccountRepositoryInterface $repository */ $repository = app(AccountRepositoryInterface::class); - $account = $repository->find(intval($attributes['accountId'])); + $crud = app('FireflyIII\Crud\Account\AccountCrudInterface'); + $account = $crud->find(intval($attributes['accountId'])); $types = [TransactionType::DEPOSIT, TransactionType::TRANSFER]; $journals = $repository->journalsInPeriod(new Collection([$account]), $types, $attributes['startDate'], $attributes['endDate']); $destinations = $attributes['accounts']->pluck('id')->toArray(); @@ -221,8 +221,8 @@ class ReportController extends Controller $journals = $journals->filter( function (TransactionJournal $journal) use ($account, $destinations) { if ( - $journal->source_account_id === $account->id && - in_array($journal->destination_account_id, $destinations) + $journal->source_account_id === $account->id + && in_array($journal->destination_account_id, $destinations) ) { return $journal; } diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index ae9e7c4a81..76a180a854 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -170,21 +170,6 @@ class AccountRepository implements AccountRepositoryInterface return $journals; } - /** - * @param $accountId - * - * @return Account - */ - public function find(int $accountId): Account - { - $account = $this->user->accounts()->find($accountId); - if (is_null($account)) { - $account = new Account; - } - - return $account; - } - /** * @param Account $account * diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php index 074b2588e8..cd1393125b 100644 --- a/app/Repositories/Account/AccountRepositoryInterface.php +++ b/app/Repositories/Account/AccountRepositoryInterface.php @@ -63,13 +63,6 @@ interface AccountRepositoryInterface */ public function expensesInPeriod(Collection $accounts, Carbon $start, Carbon $end): Collection; - /** - * @param int $accountId - * - * @return Account - */ - public function find(int $accountId): Account; - /** * @param Account $account *