From 8ef7c5ac33d2f36cb15e82ad94d37b0e06e91c18 Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 10 Oct 2016 07:12:39 +0200 Subject: [PATCH] Moved find() method to new class. --- app/Crud/Account/AccountCrud.php | 15 ---------- app/Crud/Account/AccountCrudInterface.php | 7 ----- app/Http/Controllers/AccountController.php | 5 ++-- .../Controllers/Popup/ReportController.php | 29 +++++++++++-------- app/Import/Converter/AccountId.php | 7 +++-- app/Import/Converter/AssetAccountIban.php | 12 +++++--- app/Import/Converter/AssetAccountName.php | 12 +++++--- app/Import/Converter/AssetAccountNumber.php | 14 +++++---- app/Import/Converter/OpposingAccountIban.php | 12 +++++--- app/Import/Converter/OpposingAccountName.php | 12 +++++--- .../Converter/OpposingAccountNumber.php | 14 +++++---- app/Import/ImportProcedure.php | 6 ++-- .../Account/AccountRepository.php | 23 ++++++++++----- .../Account/AccountRepositoryInterface.php | 11 ++++--- 14 files changed, 99 insertions(+), 80 deletions(-) diff --git a/app/Crud/Account/AccountCrud.php b/app/Crud/Account/AccountCrud.php index 03b12eba24..5f977ccccb 100644 --- a/app/Crud/Account/AccountCrud.php +++ b/app/Crud/Account/AccountCrud.php @@ -52,21 +52,6 @@ class AccountCrud implements AccountCrudInterface } - /** - * @param $accountId - * - * @return Account - */ - public function find(int $accountId): Account - { - $account = $this->user->accounts()->find($accountId); - if (is_null($account)) { - return new Account; - } - - return $account; - } - /** * @param string $number * @param array $types diff --git a/app/Crud/Account/AccountCrudInterface.php b/app/Crud/Account/AccountCrudInterface.php index 15dd162de9..6196da869a 100644 --- a/app/Crud/Account/AccountCrudInterface.php +++ b/app/Crud/Account/AccountCrudInterface.php @@ -25,13 +25,6 @@ use Illuminate\Support\Collection; interface AccountCrudInterface { - /** - * @param int $accountId - * - * @return Account - */ - public function find(int $accountId): Account; - /** * @param string $number * @param array $types diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index 898f98e03b..4d0d115a04 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -95,17 +95,16 @@ 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(ARI $repository, Account $account) { $type = $account->accountType->type; $typeName = config('firefly.shortNamesByFullName.' . $type); $name = $account->name; - $moveTo = $crud->find(intval(Input::get('move_account_before_delete'))); + $moveTo = $repository->find(intval(Input::get('move_account_before_delete'))); $repository->destroy($account, $moveTo); diff --git a/app/Http/Controllers/Popup/ReportController.php b/app/Http/Controllers/Popup/ReportController.php index 64eb4b3266..c6893a6014 100644 --- a/app/Http/Controllers/Popup/ReportController.php +++ b/app/Http/Controllers/Popup/ReportController.php @@ -15,13 +15,13 @@ namespace FireflyIII\Http\Controllers\Popup; use Carbon\Carbon; -use FireflyIII\Crud\Account\AccountCrudInterface; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Helpers\Collection\BalanceLine; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionType; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountTaskerInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Category\CategoryRepositoryInterface; @@ -93,8 +93,11 @@ class ReportController extends Controller /** @var BudgetRepositoryInterface $budgetRepository */ $budgetRepository = app(BudgetRepositoryInterface::class); $budget = $budgetRepository->find(intval($attributes['budgetId'])); - $crud = app('FireflyIII\Crud\Account\AccountCrudInterface'); - $account = $crud->find(intval($attributes['accountId'])); + + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class); + + $account = $repository->find(intval($attributes['accountId'])); switch (true) { case ($role === BalanceLine::ROLE_DEFAULTROLE && !is_null($budget->id)): @@ -187,9 +190,11 @@ class ReportController extends Controller private function expenseEntry(array $attributes): string { /** @var AccountTaskerInterface $tasker */ - $tasker = app(AccountTaskerInterface::class); - $crud = app(AccountCrudInterface::class); - $account = $crud->find(intval($attributes['accountId'])); + $tasker = app(AccountTaskerInterface::class); + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class); + + $account = $repository->find(intval($attributes['accountId'])); $types = [TransactionType::WITHDRAWAL, TransactionType::TRANSFER]; $journals = $tasker->getJournalsInPeriod(new Collection([$account]), $types, $attributes['startDate'], $attributes['endDate']); $report = $attributes['accounts']->pluck('id')->toArray(); // accounts used in this report @@ -222,12 +227,12 @@ class ReportController extends Controller { /** @var AccountTaskerInterface $tasker */ $tasker = app(AccountTaskerInterface::class); - /** @var AccountCrudInterface $crud */ - $crud = app(AccountCrudInterface::class); - $account = $crud->find(intval($attributes['accountId'])); - $types = [TransactionType::DEPOSIT, TransactionType::TRANSFER]; - $journals = $tasker->getJournalsInPeriod(new Collection([$account]), $types, $attributes['startDate'], $attributes['endDate']); - $report = $attributes['accounts']->pluck('id')->toArray(); // accounts used in this report + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class); + $account = $repository->find(intval($attributes['accountId'])); + $types = [TransactionType::DEPOSIT, TransactionType::TRANSFER]; + $journals = $tasker->getJournalsInPeriod(new Collection([$account]), $types, $attributes['startDate'], $attributes['endDate']); + $report = $attributes['accounts']->pluck('id')->toArray(); // accounts used in this report // filter the set so the destinations outside of $attributes['accounts'] are not included. $journals = $journals->filter( diff --git a/app/Import/Converter/AccountId.php b/app/Import/Converter/AccountId.php index 98fa24434b..5b39999f2e 100644 --- a/app/Import/Converter/AccountId.php +++ b/app/Import/Converter/AccountId.php @@ -13,8 +13,8 @@ declare(strict_types = 1); namespace FireflyIII\Import\Converter; -use FireflyIII\Crud\Account\AccountCrudInterface; use FireflyIII\Models\Account; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; use Log; /** @@ -39,8 +39,9 @@ class AccountId extends BasicConverter implements ConverterInterface return new Account; } - /** @var AccountCrudInterface $repository */ - $repository = app(AccountCrudInterface::class, [$this->user]); + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class, [$this->user]); + if (isset($this->mapping[$value])) { Log::debug('Found account in mapping. Should exist.', ['value' => $value, 'map' => $this->mapping[$value]]); $account = $repository->find(intval($this->mapping[$value])); diff --git a/app/Import/Converter/AssetAccountIban.php b/app/Import/Converter/AssetAccountIban.php index f87f365630..d773487cfb 100644 --- a/app/Import/Converter/AssetAccountIban.php +++ b/app/Import/Converter/AssetAccountIban.php @@ -16,6 +16,7 @@ namespace FireflyIII\Import\Converter; use FireflyIII\Crud\Account\AccountCrudInterface; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; use Log; /** @@ -42,8 +43,11 @@ class AssetAccountIban extends BasicConverter implements ConverterInterface return new Account; } - /** @var AccountCrudInterface $repository */ - $repository = app(AccountCrudInterface::class, [$this->user]); + /** @var AccountCrudInterface $crud */ + $crud = app(AccountCrudInterface::class, [$this->user]); + + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class, [$this->user]); if (isset($this->mapping[$value])) { @@ -58,7 +62,7 @@ class AssetAccountIban extends BasicConverter implements ConverterInterface } // not mapped? Still try to find it first: - $account = $repository->findByIban($value, [AccountType::ASSET]); + $account = $crud->findByIban($value, [AccountType::ASSET]); if (!is_null($account->id)) { Log::debug('Found account by IBAN', ['id' => $account->id]); $this->setCertainty(50); @@ -67,7 +71,7 @@ class AssetAccountIban extends BasicConverter implements ConverterInterface } - $account = $repository->store( + $account = $crud->store( ['name' => 'Asset account with IBAN ' . $value, 'iban' => $value, 'user' => $this->user->id, 'accountType' => 'asset', 'virtualBalance' => 0, 'active' => true, 'openingBalance' => 0] ); diff --git a/app/Import/Converter/AssetAccountName.php b/app/Import/Converter/AssetAccountName.php index 9ee4b02f03..d99a9c3285 100644 --- a/app/Import/Converter/AssetAccountName.php +++ b/app/Import/Converter/AssetAccountName.php @@ -16,6 +16,7 @@ namespace FireflyIII\Import\Converter; use FireflyIII\Crud\Account\AccountCrudInterface; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; use Log; /** @@ -42,8 +43,11 @@ class AssetAccountName extends BasicConverter implements ConverterInterface return new Account; } - /** @var AccountCrudInterface $repository */ - $repository = app(AccountCrudInterface::class, [$this->user]); + /** @var AccountCrudInterface $crud */ + $crud = app(AccountCrudInterface::class, [$this->user]); + + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class, [$this->user]); if (isset($this->mapping[$value])) { @@ -58,7 +62,7 @@ class AssetAccountName extends BasicConverter implements ConverterInterface } // not mapped? Still try to find it first: - $account = $repository->findByName($value, [AccountType::ASSET]); + $account = $crud->findByName($value, [AccountType::ASSET]); if (!is_null($account->id)) { Log::debug('Found asset account by name', ['value' => $value, 'id' => $account->id]); @@ -66,7 +70,7 @@ class AssetAccountName extends BasicConverter implements ConverterInterface } - $account = $repository->store( + $account = $crud->store( ['name' => $value, 'iban' => null, 'openingBalance' => 0, 'user' => $this->user->id, 'accountType' => 'asset', 'virtualBalance' => 0, 'active' => true] ); diff --git a/app/Import/Converter/AssetAccountNumber.php b/app/Import/Converter/AssetAccountNumber.php index 2d866388f7..2c6a670c10 100644 --- a/app/Import/Converter/AssetAccountNumber.php +++ b/app/Import/Converter/AssetAccountNumber.php @@ -16,6 +16,7 @@ namespace FireflyIII\Import\Converter; use FireflyIII\Crud\Account\AccountCrudInterface; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; use Log; /** @@ -40,8 +41,11 @@ class AssetAccountNumber extends BasicConverter implements ConverterInterface return new Account; } - /** @var AccountCrudInterface $repository */ - $repository = app(AccountCrudInterface::class, [$this->user]); + /** @var AccountCrudInterface $crud */ + $crud = app(AccountCrudInterface::class, [$this->user]); + + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class, [$this->user]); if (isset($this->mapping[$value])) { @@ -55,7 +59,7 @@ class AssetAccountNumber extends BasicConverter implements ConverterInterface } // not mapped? Still try to find it first: - $account = $repository->findByAccountNumber($value, [AccountType::ASSET]); + $account = $crud->findByAccountNumber($value, [AccountType::ASSET]); if (!is_null($account->id)) { Log::debug('Found account by name', ['id' => $account->id]); $this->setCertainty(50); @@ -65,7 +69,7 @@ class AssetAccountNumber extends BasicConverter implements ConverterInterface // try to find by the name we would give it: $accountName = 'Asset account with number ' . e($value); - $account = $repository->findByName($accountName, [AccountType::ASSET]); + $account = $crud->findByName($accountName, [AccountType::ASSET]); if (!is_null($account->id)) { Log::debug('Found account by name', ['id' => $account->id]); $this->setCertainty(50); @@ -74,7 +78,7 @@ class AssetAccountNumber extends BasicConverter implements ConverterInterface } - $account = $repository->store( + $account = $crud->store( ['name' => $accountName, 'openingBalance' => 0, 'iban' => null, 'user' => $this->user->id, 'accountType' => 'asset', 'virtualBalance' => 0, 'accountNumber' => $value, 'active' => true] diff --git a/app/Import/Converter/OpposingAccountIban.php b/app/Import/Converter/OpposingAccountIban.php index f035bb2d7f..e04a128810 100644 --- a/app/Import/Converter/OpposingAccountIban.php +++ b/app/Import/Converter/OpposingAccountIban.php @@ -15,6 +15,7 @@ namespace FireflyIII\Import\Converter; use FireflyIII\Crud\Account\AccountCrudInterface; use FireflyIII\Models\Account; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; use Log; /** @@ -41,8 +42,11 @@ class OpposingAccountIban extends BasicConverter implements ConverterInterface return new Account; } - /** @var AccountCrudInterface $repository */ - $repository = app(AccountCrudInterface::class, [$this->user]); + /** @var AccountCrudInterface $crud */ + $crud = app(AccountCrudInterface::class, [$this->user]); + + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class, [$this->user]); if (isset($this->mapping[$value])) { @@ -57,7 +61,7 @@ class OpposingAccountIban extends BasicConverter implements ConverterInterface } // not mapped? Still try to find it first: - $account = $repository->findByIban($value, []); + $account = $crud->findByIban($value, []); if (!is_null($account->id)) { Log::debug('Found account by IBAN', ['id' => $account->id]); Log::info( @@ -69,7 +73,7 @@ class OpposingAccountIban extends BasicConverter implements ConverterInterface return $account; } - $account = $repository->store( + $account = $crud->store( ['name' => $value, 'iban' => $value, 'user' => $this->user->id, 'accountType' => 'import', 'virtualBalance' => 0, 'active' => true, 'openingBalance' => 0] ); diff --git a/app/Import/Converter/OpposingAccountName.php b/app/Import/Converter/OpposingAccountName.php index 38005919f8..567fbdb2f8 100644 --- a/app/Import/Converter/OpposingAccountName.php +++ b/app/Import/Converter/OpposingAccountName.php @@ -15,6 +15,7 @@ namespace FireflyIII\Import\Converter; use FireflyIII\Crud\Account\AccountCrudInterface; use FireflyIII\Models\Account; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; use Log; /** @@ -41,8 +42,11 @@ class OpposingAccountName extends BasicConverter implements ConverterInterface return new Account; } - /** @var AccountCrudInterface $repository */ - $repository = app(AccountCrudInterface::class, [$this->user]); + /** @var AccountCrudInterface $crud */ + $crud = app(AccountCrudInterface::class, [$this->user]); + + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class, [$this->user]); if (isset($this->mapping[$value])) { @@ -57,7 +61,7 @@ class OpposingAccountName extends BasicConverter implements ConverterInterface } // not mapped? Still try to find it first: - $account = $repository->findByName($value, []); + $account = $crud->findByName($value, []); if (!is_null($account->id)) { Log::debug('Found opposing account by name', ['id' => $account->id]); Log::info( @@ -69,7 +73,7 @@ class OpposingAccountName extends BasicConverter implements ConverterInterface return $account; } - $account = $repository->store( + $account = $crud->store( ['name' => $value, 'iban' => null, 'user' => $this->user->id, 'accountType' => 'import', 'virtualBalance' => 0, 'active' => true, 'openingBalance' => 0, ] diff --git a/app/Import/Converter/OpposingAccountNumber.php b/app/Import/Converter/OpposingAccountNumber.php index 524663ed51..1aeb221ebd 100644 --- a/app/Import/Converter/OpposingAccountNumber.php +++ b/app/Import/Converter/OpposingAccountNumber.php @@ -16,6 +16,7 @@ namespace FireflyIII\Import\Converter; use FireflyIII\Crud\Account\AccountCrudInterface; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; use Log; /** @@ -42,8 +43,11 @@ class OpposingAccountNumber extends BasicConverter implements ConverterInterface return new Account; } - /** @var AccountCrudInterface $repository */ - $repository = app(AccountCrudInterface::class, [$this->user]); + /** @var AccountCrudInterface $crud */ + $crud = app(AccountCrudInterface::class, [$this->user]); + + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class, [$this->user]); if (isset($this->mapping[$value])) { @@ -58,7 +62,7 @@ class OpposingAccountNumber extends BasicConverter implements ConverterInterface } // not mapped? Still try to find it first: - $account = $repository->findByAccountNumber($value, []); + $account = $crud->findByAccountNumber($value, []); if (!is_null($account->id)) { Log::debug('Found account by number', ['id' => $account->id]); $this->setCertainty(50); @@ -68,7 +72,7 @@ class OpposingAccountNumber extends BasicConverter implements ConverterInterface // try to find by the name we would give it: $accountName = 'Import account with number ' . e($value); - $account = $repository->findByName($accountName, [AccountType::IMPORT]); + $account = $crud->findByName($accountName, [AccountType::IMPORT]); if (!is_null($account->id)) { Log::debug('Found account by name', ['id' => $account->id]); $this->setCertainty(50); @@ -77,7 +81,7 @@ class OpposingAccountNumber extends BasicConverter implements ConverterInterface } - $account = $repository->store( + $account = $crud->store( ['name' => $accountName, 'openingBalance' => 0, 'iban' => null, 'user' => $this->user->id, 'accountType' => 'import', 'virtualBalance' => 0, 'accountNumber' => $value, 'active' => true] diff --git a/app/Import/ImportProcedure.php b/app/Import/ImportProcedure.php index a85872d5b3..ae6b2cf910 100644 --- a/app/Import/ImportProcedure.php +++ b/app/Import/ImportProcedure.php @@ -13,9 +13,9 @@ declare(strict_types = 1); namespace FireflyIII\Import; -use FireflyIII\Crud\Account\AccountCrud; use FireflyIII\Import\Importer\ImporterInterface; use FireflyIII\Models\ImportJob; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; use Illuminate\Support\Collection; /** @@ -56,7 +56,9 @@ class ImportProcedure $validator->setUser($job->user); $validator->setJob($job); if ($job->configuration['import-account'] != 0) { - $repository = app(AccountCrud::class, [$job->user]); + + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class, [$job->user]); $validator->setDefaultImportAccount($repository->find($job->configuration['import-account'])); } diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index e31e258d44..36cf671ef9 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -16,17 +16,9 @@ namespace FireflyIII\Repositories\Account; use Carbon\Carbon; use DB; -use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Account; -use FireflyIII\Models\PiggyBank; use FireflyIII\Models\Transaction; -use FireflyIII\Models\TransactionJournal; -use FireflyIII\Models\TransactionType; use FireflyIII\User; -use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Query\JoinClause; -use Illuminate\Support\Collection; -use Steam; /** @@ -85,6 +77,21 @@ class AccountRepository implements AccountRepositoryInterface return true; } + /** + * @param $accountId + * + * @return Account + */ + public function find(int $accountId): Account + { + $account = $this->user->accounts()->find($accountId); + if (is_null($account)) { + return new Account; + } + + return $account; + } + /** * Returns the date of the very first transaction in this account. * diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php index d3abb50be9..6cc227fc70 100644 --- a/app/Repositories/Account/AccountRepositoryInterface.php +++ b/app/Repositories/Account/AccountRepositoryInterface.php @@ -14,11 +14,7 @@ declare(strict_types = 1); namespace FireflyIII\Repositories\Account; use Carbon\Carbon; -use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Account; -use FireflyIII\Models\Transaction; -use FireflyIII\Models\TransactionJournal; -use Illuminate\Support\Collection; /** * Interface AccountRepositoryInterface @@ -47,6 +43,13 @@ interface AccountRepositoryInterface */ public function destroy(Account $account, Account $moveTo): bool; + /** + * @param int $accountId + * + * @return Account + */ + public function find(int $accountId): Account; + /** * Returns the date of the very first transaction in this account. *