Cleaned up some code.

This commit is contained in:
James Cole 2016-04-27 06:43:17 +02:00
parent 3833a41acb
commit f34aa77d1d
17 changed files with 107 additions and 116 deletions

View File

@ -20,9 +20,8 @@ class AccountId extends BasicConverter implements ConverterInterface
{
/** @var AccountRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');
$account = isset($this->mapped[$this->index][$this->value])
? $repository->find($this->mapped[$this->index][$this->value])
: $repository->find($this->value);
$var = isset($this->mapped[$this->index][$this->value]) ? $this->mapped[$this->index][$this->value] : $this->value;
$account = $repository->find($var);
return $account;
}

View File

@ -32,41 +32,55 @@ class AssetAccountIban extends BasicConverter implements ConverterInterface
return $account;
}
if (strlen($this->value) > 0) {
// find or create new account:
$set = $repository->getAccounts(['Default account', 'Asset account']);
/** @var Account $entry */
foreach ($set as $entry) {
if ($entry->iban == $this->value) {
Log::debug('Found an account with the same IBAN ("' . $this->value . '"). It is account #' . $entry->id);
return $entry;
}
}
Log::debug('Found no account with the same IBAN ("' . $this->value . '"), so will create a new one.');
// create it if doesn't exist.
$accountData = [
'name' => $this->value,
'accountType' => 'asset',
'virtualBalance' => 0,
'virtualBalanceCurrency' => 1, // hard coded.
'active' => true,
'user' => Auth::user()->id,
'iban' => $this->value,
'accountNumber' => $this->value,
'accountRole' => null,
'openingBalance' => 0,
'openingBalanceDate' => new Carbon,
'openingBalanceCurrency' => 1, // hard coded.
];
$account = $repository->store($accountData);
$account = $this->searchOrCreate($repository);
return $account;
}
return new Account;
}
/**
* @param AccountRepositoryInterface $repository
* @param $value
*
* @return Account
*/
private function searchOrCreate(AccountRepositoryInterface $repository)
{
// find or create new account:
$set = $repository->getAccounts(['Default account', 'Asset account']);
/** @var Account $entry */
foreach ($set as $entry) {
if ($entry->iban == $this->value) {
Log::debug('Found an account with the same IBAN ("' . $this->value . '"). It is account #' . $entry->id);
return $entry;
}
}
Log::debug('Found no account with the same IBAN ("' . $this->value . '"), so will create a new one.');
// create it if doesn't exist.
$accountData = [
'name' => $this->value,
'accountType' => 'asset',
'virtualBalance' => 0,
'virtualBalanceCurrency' => 1, // hard coded.
'active' => true,
'user' => Auth::user()->id,
'iban' => $this->value,
'accountNumber' => $this->value,
'accountRole' => null,
'openingBalance' => 0,
'openingBalanceDate' => new Carbon,
'openingBalanceCurrency' => 1, // hard coded.
];
$account = $repository->store($accountData);
return $account;
}
}

View File

@ -22,16 +22,12 @@ class AssetAccountName extends BasicConverter implements ConverterInterface
{
/** @var AccountRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');
// is mapped? Then it's easy!
if (isset($this->mapped[$this->index][$this->value])) {
$account = $repository->find(intval($this->mapped[$this->index][$this->value]));
return $account;
}
// find or create new account:
$set = $repository->getAccounts(['Default account', 'Asset account']);
/** @var Account $entry */
foreach ($set as $entry) {
@ -39,8 +35,6 @@ class AssetAccountName extends BasicConverter implements ConverterInterface
return $entry;
}
}
// create it if doesnt exist.
$accountData = [
'name' => $this->value,
'accountType' => 'asset',
@ -54,7 +48,6 @@ class AssetAccountName extends BasicConverter implements ConverterInterface
'openingBalance' => 0,
'openingBalanceDate' => new Carbon,
'openingBalanceCurrency' => 1, // hard coded.
];
$account = $repository->store($accountData);

View File

@ -5,6 +5,8 @@ namespace FireflyIII\Helpers\Csv\Converter;
/**
* Class BasicConverter
*
*
* @SuppressWarnings(PHPMD.NumberOfChildren)
* @package FireflyIII\Helpers\Csv\Converter
*/
class BasicConverter

View File

@ -20,13 +20,8 @@ class BillId extends BasicConverter implements ConverterInterface
{
/** @var BillRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Bill\BillRepositoryInterface');
// is mapped? Then it's easy!
if (isset($this->mapped[$this->index][$this->value])) {
$bill = $repository->find($this->mapped[$this->index][$this->value]);
} else {
$bill = $repository->find($this->value);
}
$value = isset($this->mapped[$this->index][$this->value]) ? $this->mapped[$this->index][$this->value] : $this->value;
$bill = $repository->find($value);
return $bill;
}

View File

@ -21,20 +21,18 @@ class BillName extends BasicConverter implements ConverterInterface
/** @var BillRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Bill\BillRepositoryInterface');
$bill = null;
// is mapped? Then it's easy!
if (isset($this->mapped[$this->index][$this->value])) {
$bill = $repository->find($this->mapped[$this->index][$this->value]);
} else {
$bills = $repository->getBills();
/** @var Bill $bill */
foreach ($bills as $bill) {
if ($bill->name == $this->value) {
return $bill;
}
return $repository->find($this->mapped[$this->index][$this->value]);
}
$bills = $repository->getBills();
/** @var Bill $bill */
foreach ($bills as $bill) {
if ($bill->name == $this->value) {
return $bill;
}
}
return $bill;
return new Bill;
}
}

View File

@ -20,14 +20,8 @@ class BudgetId extends BasicConverter implements ConverterInterface
{
/** @var BudgetRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
// is mapped? Then it's easy!
if (isset($this->mapped[$this->index][$this->value])) {
$budget = $repository->find($this->mapped[$this->index][$this->value]);
} else {
$budget = $repository->find($this->value);
}
$value = isset($this->mapped[$this->index][$this->value]) ? $this->mapped[$this->index][$this->value] : $this->value;
$budget = $repository->find($value);
return $budget;
}

View File

@ -25,9 +25,11 @@ class BudgetName extends BasicConverter implements ConverterInterface
// is mapped? Then it's easy!
if (isset($this->mapped[$this->index][$this->value])) {
$budget = $repository->find($this->mapped[$this->index][$this->value]);
} else {
$budget = $repository->store(['name' => $this->value, 'user' => Auth::user()->id]);
return $budget;
}
$budget = $repository->store(['name' => $this->value, 'user' => Auth::user()->id]);
return $budget;
}

View File

@ -20,13 +20,8 @@ class CategoryId extends BasicConverter implements ConverterInterface
{
/** @var SingleCategoryRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface');
// is mapped? Then it's easy!
if (isset($this->mapped[$this->index][$this->value])) {
$category = $repository->find($this->mapped[$this->index][$this->value]);
} else {
$category = $repository->find($this->value);
}
$value = isset($this->mapped[$this->index][$this->value]) ? $this->mapped[$this->index][$this->value] : $this->value;
$category = $repository->find($value);
return $category;
}

View File

@ -25,16 +25,17 @@ class CategoryName extends BasicConverter implements ConverterInterface
// is mapped? Then it's easy!
if (isset($this->mapped[$this->index][$this->value])) {
$category = $repository->find($this->mapped[$this->index][$this->value]);
} else {
$data = [
'name' => $this->value,
'user' => Auth::user()->id,
];
$category = $repository->store($data);
return $category;
}
$data = [
'name' => $this->value,
'user' => Auth::user()->id,
];
$category = $repository->store($data);
return $category;
}
}

View File

@ -21,12 +21,14 @@ class CurrencyCode extends BasicConverter implements ConverterInterface
/** @var CurrencyRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface');
if (isset($this->mapped[$this->index][$this->value])) {
$currency = $repository->find($this->mapped[$this->index][$this->value]);
} else {
$currency = $repository->findByCode($this->value);
return $currency;
}
$currency = $repository->findByCode($this->value);
return $currency;
}

View File

@ -20,12 +20,8 @@ class CurrencyId extends BasicConverter implements ConverterInterface
{
/** @var CurrencyRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface');
if (isset($this->mapped[$this->index][$this->value])) {
$currency = $repository->find($this->mapped[$this->index][$this->value]);
} else {
$currency = $repository->find($this->value);
}
$value = isset($this->mapped[$this->index][$this->value]) ? $this->mapped[$this->index][$this->value] : $this->value;
$currency = $repository->find($value);
return $currency;
}

View File

@ -22,11 +22,12 @@ class CurrencyName extends BasicConverter implements ConverterInterface
$repository = app('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface');
if (isset($this->mapped[$this->index][$this->value])) {
$currency = $repository->find($this->mapped[$this->index][$this->value]);
} else {
$currency = $repository->findByName($this->value);
return $currency;
}
$currency = $repository->findByName($this->value);
return $currency;
}

View File

@ -23,9 +23,12 @@ class CurrencySymbol extends BasicConverter implements ConverterInterface
if (isset($this->mapped[$this->index][$this->value])) {
$currency = $repository->find($this->mapped[$this->index][$this->value]);
} else {
$currency = $repository->findBySymbol($this->value);
return $currency;
}
$currency = $repository->findBySymbol($this->value);
return $currency;
}

View File

@ -27,21 +27,22 @@ class OpposingAccountIban extends BasicConverter implements ConverterInterface
$account = $repository->find($this->mapped[$this->index][$this->value]);
return $account;
} else {
if (strlen($this->value) > 0) {
}
if (strlen($this->value) > 0) {
$set = $repository->getAccounts([]);
/** @var Account $account */
foreach ($set as $account) {
if ($account->iban == $this->value) {
$set = $repository->getAccounts([]);
/** @var Account $account */
foreach ($set as $account) {
if ($account->iban == $this->value) {
return $account;
}
return $account;
}
}
return $this->value;
}
return $this->value;
}
}

View File

@ -21,15 +21,9 @@ class OpposingAccountId extends BasicConverter implements ConverterInterface
{
/** @var AccountRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');
if (isset($this->mapped[$this->index][$this->value])) {
$account = $repository->find($this->mapped[$this->index][$this->value]);
} else {
$account = $repository->find($this->value);
}
$value = isset($this->mapped[$this->index][$this->value]) ? $this->mapped[$this->index][$this->value] : $this->value;
$account = $repository->find($value);
return $account;
}
}

View File

@ -27,8 +27,9 @@ class OpposingAccountName extends BasicConverter implements ConverterInterface
$account = $repository->find($this->mapped[$this->index][$this->value]);
return $account;
} else {
return $this->value;
}
return $this->value;
}
}