Refactor code to traits.

This commit is contained in:
James Cole
2018-12-31 08:11:57 +01:00
parent e7bcc01fe8
commit f80de95bb0
14 changed files with 972 additions and 922 deletions

View File

@@ -25,10 +25,8 @@ namespace FireflyIII\Http\Controllers\Transaction;
use FireflyIII\Events\UpdatedTransactionJournal;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Support\Http\Controllers\ModelInformation;
use Illuminate\Http\Request;
@@ -163,6 +161,7 @@ class ConvertController extends Controller
if ($errors->count() > 0) {
Log::error('Errors while converting: ', $errors->toArray());
return redirect(route('transactions.convert.index', [strtolower($destinationType->type), $journal->id]))->withErrors($errors)->withInput();
}
@@ -174,126 +173,4 @@ class ConvertController extends Controller
return redirect(route('transactions.show', [$journal->id]));
}
/**
* Get the destination account. Is complex.
*
* @param TransactionJournal $journal
* @param TransactionType $destinationType
* @param array $data
*
* @return Account
*
* @throws FireflyException
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function getDestinationAccount(TransactionJournal $journal, TransactionType $destinationType, array $data
): Account // helper for conversion. Get info from obj.
{
/** @var AccountRepositoryInterface $accountRepository */
$accountRepository = app(AccountRepositoryInterface::class);
$sourceAccount = $this->repository->getJournalSourceAccounts($journal)->first();
$destinationAccount = $this->repository->getJournalDestinationAccounts($journal)->first();
$sourceType = $journal->transactionType;
$joined = $sourceType->type . '-' . $destinationType->type;
switch ($joined) {
default:
throw new FireflyException('Cannot handle ' . $joined); // @codeCoverageIgnore
case TransactionType::WITHDRAWAL . '-' . TransactionType::DEPOSIT:
// one
$destination = $sourceAccount;
break;
case TransactionType::WITHDRAWAL . '-' . TransactionType::TRANSFER:
// two
$destination = $accountRepository->findNull((int)$data['destination_account_asset']);
break;
case TransactionType::DEPOSIT . '-' . TransactionType::WITHDRAWAL:
case TransactionType::TRANSFER . '-' . TransactionType::WITHDRAWAL:
// three and five
if ('' === $data['destination_account_expense'] || null === $data['destination_account_expense']) {
// destination is a cash account.
return $accountRepository->getCashAccount();
}
$data = [
'name' => $data['destination_account_expense'],
'accountType' => 'expense',
'account_type_id' => null,
'virtualBalance' => 0,
'active' => true,
'iban' => null,
];
$destination = $accountRepository->store($data);
break;
case TransactionType::DEPOSIT . '-' . TransactionType::TRANSFER:
case TransactionType::TRANSFER . '-' . TransactionType::DEPOSIT:
// four and six
$destination = $destinationAccount;
break;
}
return $destination;
}
/**
* Get the source account.
*
* @param TransactionJournal $journal
* @param TransactionType $destinationType
* @param array $data
*
* @return Account
*
* @throws FireflyException
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function getSourceAccount(TransactionJournal $journal, TransactionType $destinationType, array $data
): Account // helper for conversion. Get info from obj.
{
/** @var AccountRepositoryInterface $accountRepository */
$accountRepository = app(AccountRepositoryInterface::class);
$sourceAccount = $this->repository->getJournalSourceAccounts($journal)->first();
$destinationAccount = $this->repository->getJournalDestinationAccounts($journal)->first();
$sourceType = $journal->transactionType;
$joined = $sourceType->type . '-' . $destinationType->type;
switch ($joined) {
default:
throw new FireflyException('Cannot handle ' . $joined); // @codeCoverageIgnore
case TransactionType::WITHDRAWAL . '-' . TransactionType::DEPOSIT:
case TransactionType::TRANSFER . '-' . TransactionType::DEPOSIT:
if ('' === $data['source_account_revenue'] || null === $data['source_account_revenue']) {
// destination is a cash account.
return $accountRepository->getCashAccount();
}
$data = [
'name' => $data['source_account_revenue'],
'accountType' => 'revenue',
'virtualBalance' => 0,
'active' => true,
'account_type_id' => null,
'iban' => null,
];
$source = $accountRepository->store($data);
break;
case TransactionType::WITHDRAWAL . '-' . TransactionType::TRANSFER:
case TransactionType::TRANSFER . '-' . TransactionType::WITHDRAWAL:
$source = $sourceAccount;
break;
case TransactionType::DEPOSIT . '-' . TransactionType::WITHDRAWAL:
$source = $destinationAccount;
break;
case TransactionType::DEPOSIT . '-' . TransactionType::TRANSFER:
$source = $accountRepository->findNull((int)$data['source_account_asset']);
break;
}
return $source;
}
}

View File

@@ -171,42 +171,4 @@ class SplitController extends Controller
return redirect($this->getPreviousUri('transactions.edit-split.uri'));
}
/**
* Get info from old input.
*
* @param $array
* @param $old
*
* @return array
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function updateWithPrevious($array, $old): array // update object with new info
{
if (0 === \count($old) || !isset($old['transactions'])) {
return $array;
}
$old = $old['transactions'];
foreach ($old as $index => $row) {
if (isset($array[$index])) {
/** @noinspection SlowArrayOperationsInLoopInspection */
$array[$index] = array_merge($array[$index], $row);
continue;
}
// take some info from first transaction, that should at least exist.
$array[$index] = $row;
$array[$index]['currency_id'] = $array[0]['currency_id'];
$array[$index]['currency_code'] = $array[0]['currency_code'] ?? '';
$array[$index]['currency_symbol'] = $array[0]['currency_symbol'] ?? '';
$array[$index]['foreign_amount'] = round($array[0]['foreign_destination_amount'] ?? '0', 12);
$array[$index]['foreign_currency_id'] = $array[0]['foreign_currency_id'];
$array[$index]['foreign_currency_code'] = $array[0]['foreign_currency_code'];
$array[$index]['foreign_currency_symbol'] = $array[0]['foreign_currency_symbol'];
}
return $array;
}
}