. */ declare(strict_types=1); namespace FireflyIII\Support\Import\Routine\File; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\User; use Log; /** * Class AssetAccountMapper */ class AssetAccountMapper { /** @var int */ private $defaultAccount; /** @var AccountRepositoryInterface */ private $repository; /** @var User */ private $user; /** * Based upon data in the importable, try to find or create the asset account account. * * @param int|null $accountId * @param array $data * * @return Account * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function map(?int $accountId, array $data): Account { Log::debug('Now in AssetAccountMapper::map()'); if ((int)$accountId > 0) { // find asset account with this ID: $result = $this->repository->findNull($accountId); if (null !== $result && $result->accountType->type === AccountType::ASSET) { Log::debug(sprintf('Found asset account "%s" based on given ID %d', $result->name, $accountId)); return $result; } if (null !== $result && $result->accountType->type !== AccountType::ASSET) { Log::warning( sprintf('Found account "%s" based on given ID %d but its a %s, return nothing.', $result->name, $accountId, $result->accountType->type) ); } } // find by (respectively): // IBAN, accountNumber, name, $fields = ['iban' => 'findByIbanNull', 'number' => 'findByAccountNumber', 'name' => 'findByName']; foreach ($fields as $field => $function) { $value = (string)($data[$field] ?? ''); if ('' === $value) { Log::debug(sprintf('Array does not contain a value for %s. Continue', $field)); continue; } $result = $this->repository->$function($value, [AccountType::ASSET]); Log::debug(sprintf('Going to run %s() with argument "%s" (asset account)', $function, $value)); if (null !== $result) { Log::debug(sprintf('Found asset account "%s". Return it!', $result->name)); return $result; } } Log::debug('Found nothing. Will return default account.'); // still NULL? Return default account. $default = null; if ($this->defaultAccount > 0) { $default = $this->repository->findNull($this->defaultAccount); } if (null === $default) { Log::debug('Default account is NULL! Simply result first account in system.'); $default = $this->repository->getAccountsByType([AccountType::ASSET])->first(); } Log::debug(sprintf('Return default account "%s" (#%d). Return it!', $default->name, $default->id)); return $default; } /** * @param int $defaultAccount */ public function setDefaultAccount(int $defaultAccount): void { $this->defaultAccount = $defaultAccount; } /** * @param User $user */ public function setUser(User $user): void { $this->user = $user; $this->repository = app(AccountRepositoryInterface::class); $this->defaultAccount = 0; $this->repository->setUser($user); } }