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 */ /** @var AccountRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface'); $repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');
$account = isset($this->mapped[$this->index][$this->value]) $var = isset($this->mapped[$this->index][$this->value]) ? $this->mapped[$this->index][$this->value] : $this->value;
? $repository->find($this->mapped[$this->index][$this->value]) $account = $repository->find($var);
: $repository->find($this->value);
return $account; return $account;
} }

View File

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

View File

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

View File

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

View File

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

View File

@@ -25,9 +25,11 @@ class BudgetName extends BasicConverter implements ConverterInterface
// is mapped? Then it's easy! // is mapped? Then it's easy!
if (isset($this->mapped[$this->index][$this->value])) { if (isset($this->mapped[$this->index][$this->value])) {
$budget = $repository->find($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; return $budget;
} }

View File

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

View File

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

View File

@@ -21,13 +21,15 @@ class CurrencyCode extends BasicConverter implements ConverterInterface
/** @var CurrencyRepositoryInterface $repository */ /** @var CurrencyRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface'); $repository = app('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface');
if (isset($this->mapped[$this->index][$this->value])) { if (isset($this->mapped[$this->index][$this->value])) {
$currency = $repository->find($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; return $currency;
} }
} }

View File

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

View File

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

View File

@@ -23,10 +23,13 @@ class CurrencySymbol extends BasicConverter implements ConverterInterface
if (isset($this->mapped[$this->index][$this->value])) { if (isset($this->mapped[$this->index][$this->value])) {
$currency = $repository->find($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; return $currency;
} }
} }

View File

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

View File

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

View File

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