Moved find() method to new class.

This commit is contained in:
James Cole 2016-10-10 07:12:39 +02:00
parent 7180a40cd8
commit 8ef7c5ac33
14 changed files with 99 additions and 80 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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]

View File

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

View File

@ -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,
]

View File

@ -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]

View File

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

View File

@ -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.
*

View File

@ -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.
*