mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Clean up code for import routine.
This commit is contained in:
@@ -27,7 +27,6 @@ use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
@@ -70,18 +69,18 @@ class ImportAccount
|
||||
public function __construct()
|
||||
{
|
||||
$this->expectedType = AccountType::ASSET;
|
||||
$this->account = new Account;
|
||||
$this->repository = app(AccountRepositoryInterface::class);
|
||||
Log::debug('Created ImportAccount.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Account
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function getAccount(): Account
|
||||
{
|
||||
if (null === $this->account->id) {
|
||||
if (null === $this->account) {
|
||||
$this->store();
|
||||
}
|
||||
|
||||
@@ -90,6 +89,7 @@ class ImportAccount
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getExpectedType(): string
|
||||
@@ -187,43 +187,26 @@ class ImportAccount
|
||||
}
|
||||
|
||||
/**
|
||||
* Find account by IBAN and type.
|
||||
*
|
||||
* @param AccountType $type
|
||||
*
|
||||
* @return Account|null
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
*/
|
||||
private function findExistingObject(): ?Account
|
||||
private function findByIBAN(AccountType $type): ?Account
|
||||
{
|
||||
Log::debug('In findExistingObject() for Account');
|
||||
// 0: determin account type:
|
||||
/** @var AccountType $accountType */
|
||||
$accountType = $this->repository->getAccountType($this->expectedType);
|
||||
|
||||
// 1: find by ID, iban or name (and type)
|
||||
if (3 === count($this->accountId)) {
|
||||
Log::debug(sprintf('Finding account of type %d and ID %d', $accountType->id, $this->accountId['value']));
|
||||
/** @var Account $account */
|
||||
|
||||
$account = $this->user->accounts()->where('id', '!=', $this->forbiddenAccountId)->where('account_type_id', $accountType->id)->where(
|
||||
'id',
|
||||
$this->accountId['value']
|
||||
)->first();
|
||||
if (null !== $account) {
|
||||
Log::debug(sprintf('Found unmapped %s account by ID (#%d): %s', $this->expectedType, $account->id, $account->name));
|
||||
|
||||
return $account;
|
||||
}
|
||||
Log::debug('Found nothing.');
|
||||
}
|
||||
/** @var Collection $accounts */
|
||||
$accounts = $this->repository->getAccountsByType([$accountType->type]);
|
||||
// Two: find by IBAN (and type):
|
||||
if (3 === count($this->accountIban)) {
|
||||
$iban = $this->accountIban['value'];
|
||||
Log::debug(sprintf('Finding account of type %d and IBAN %s', $accountType->id, $iban));
|
||||
$accounts = $this->repository->getAccountsByType([$type->type]);
|
||||
$iban = $this->accountIban['value'];
|
||||
Log::debug(sprintf('Finding account of type %d and IBAN %s', $type->id, $iban));
|
||||
$filtered = $accounts->filter(
|
||||
function (Account $account) use ($iban) {
|
||||
if ($account->iban === $iban && $account->id !== $this->forbiddenAccountId) {
|
||||
Log::debug(
|
||||
sprintf('Found unmapped %s account by IBAN (#%d): %s (%s)', $this->expectedType, $account->id, $account->name, $account->iban)
|
||||
sprintf(
|
||||
'Found unmapped %s account by IBAN (#%d): %s (%s)',
|
||||
$this->expectedType, $account->id, $account->name, $account->iban
|
||||
)
|
||||
);
|
||||
|
||||
return $account;
|
||||
@@ -238,10 +221,52 @@ class ImportAccount
|
||||
Log::debug('Found nothing.');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find account of type X by its ID.
|
||||
*
|
||||
* @param AccountType $type
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
private function findById(AccountType $type): ?Account
|
||||
{
|
||||
if (3 === count($this->accountId)) {
|
||||
Log::debug(sprintf('Finding account of type %d and ID %d', $type->id, $this->accountId['value']));
|
||||
/** @var Account $account */
|
||||
$account = $this->user->accounts()
|
||||
->where('id', '!=', $this->forbiddenAccountId)
|
||||
->where('account_type_id', $type->id)
|
||||
->where('id', $this->accountId['value'])
|
||||
->first();
|
||||
|
||||
if (null !== $account) {
|
||||
Log::debug(sprintf('Found unmapped %s account by ID (#%d): %s', $this->expectedType, $account->id, $account->name));
|
||||
|
||||
return $account;
|
||||
}
|
||||
Log::debug('Found nothing.');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find account by account type and name.
|
||||
*
|
||||
* @param AccountType $type
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
private function findByName(AccountType $type): ?Account
|
||||
{
|
||||
// Three: find by name (and type):
|
||||
if (3 === count($this->accountName)) {
|
||||
$name = $this->accountName['value'];
|
||||
Log::debug(sprintf('Finding account of type %d and name %s', $accountType->id, $name));
|
||||
$accounts = $this->repository->getAccountsByType([$type->type]);
|
||||
$name = $this->accountName['value'];
|
||||
Log::debug(sprintf('Finding account of type %d and name %s', $type->id, $name));
|
||||
$filtered = $accounts->filter(
|
||||
function (Account $account) use ($name) {
|
||||
if ($account->name === $name && $account->id !== $this->forbiddenAccountId) {
|
||||
@@ -260,7 +285,36 @@ class ImportAccount
|
||||
Log::debug('Found nothing.');
|
||||
}
|
||||
|
||||
// 4: do not search by account number.
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determin account type to find, then use fields in object to try and find it.
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
private function findExistingObject(): ?Account
|
||||
{
|
||||
Log::debug('In findExistingObject() for Account');
|
||||
/** @var AccountType $accountType */
|
||||
$accountType = $this->repository->getAccountType($this->expectedType);
|
||||
$result = $this->findById($accountType);
|
||||
if (!is_null($result)) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$result = $this->findByIBAN($accountType);
|
||||
|
||||
if (!is_null($result)) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$result = $this->findByName($accountType);
|
||||
|
||||
if (!is_null($result)) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
Log::debug('Found NO existing accounts.');
|
||||
|
||||
return null;
|
||||
@@ -342,6 +396,7 @@ class ImportAccount
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function store(): bool
|
||||
@@ -349,7 +404,7 @@ class ImportAccount
|
||||
if (is_null($this->user)) {
|
||||
throw new FireflyException('ImportAccount cannot continue without user.');
|
||||
}
|
||||
if ((is_null($this->defaultAccountId) || intval($this->defaultAccountId) === 0) && AccountType::ASSET === $this->expectedType) {
|
||||
if ((is_null($this->defaultAccountId) || 0 === intval($this->defaultAccountId)) && AccountType::ASSET === $this->expectedType) {
|
||||
throw new FireflyException('ImportAccount cannot continue without a default account to fall back on.');
|
||||
}
|
||||
// 1: find mapped object:
|
||||
|
Reference in New Issue
Block a user