Two small method improvements.

This commit is contained in:
James Cole 2020-03-17 14:46:17 +01:00
parent 46382b0d21
commit 64462812fc
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
2 changed files with 64 additions and 42 deletions

View File

@ -178,23 +178,13 @@ class RecurrenceUpdateRequest extends Request
} }
/** /**
* Returns the transaction data as it is found in the submitted data. It's a complex method according to code * @param array $transaction
* standards but it just has a lot of ??-statements because of the fields that may or may not exist.
* *
* @return array|null * @return array
*/ */
private function getTransactionData(): ?array private function getSingleData(array $transaction): array
{ {
$return = []; return [
// transaction data:
/** @var array $transactions */
$transactions = $this->get('transactions');
if (null === $transactions) {
return null;
}
/** @var array $transaction */
foreach ($transactions as $transaction) {
$return[] = [
'amount' => $transaction['amount'], 'amount' => $transaction['amount'],
'currency_id' => isset($transaction['currency_id']) ? (int)$transaction['currency_id'] : null, 'currency_id' => isset($transaction['currency_id']) ? (int)$transaction['currency_id'] : null,
'currency_code' => $transaction['currency_code'] ?? null, 'currency_code' => $transaction['currency_code'] ?? null,
@ -219,6 +209,26 @@ class RecurrenceUpdateRequest extends Request
]; ];
} }
/**
* Returns the transaction data as it is found in the submitted data. It's a complex method according to code
* standards but it just has a lot of ??-statements because of the fields that may or may not exist.
*
* @return array|null
*/
private function getTransactionData(): ?array
{
$return = [];
// transaction data:
/** @var array $transactions */
$transactions = $this->get('transactions');
if (null === $transactions) {
return null;
}
/** @var array $transaction */
foreach ($transactions as $transaction) {
$return[] = $this->getSingleData($transaction);
}
return $return; return $return;
} }
} }

View File

@ -76,24 +76,7 @@ class AccountUpdateService
{ {
$this->accountRepository->setUser($account->user); $this->accountRepository->setUser($account->user);
$this->user = $account->user; $this->user = $account->user;
$account = $this->updateAccount($account, $data);
// update the account itself:
$account->name = $data['name'] ?? $account->name;
$account->active = $data['active'] ?? $account->active;
$account->iban = $data['iban'] ?? $account->iban;
// if account type is a liability, the liability type (account type)
// can be updated to another one.
if ($this->isLiability($account) && $this->isLiabilityTypeId((int)($data['account_type_id'] ?? 0))) {
$account->account_type_id = (int)$data['account_type_id'];
}
// update virtual balance (could be set to zero if empty string).
if (null !== $data['virtual_balance']) {
$account->virtual_balance = '' === trim($data['virtual_balance']) ? '0' : $data['virtual_balance'];
}
$account->save();
// find currency, or use default currency instead. // find currency, or use default currency instead.
if (isset($data['currency_id']) && (null !== $data['currency_id'] || null !== $data['currency_code'])) { if (isset($data['currency_id']) && (null !== $data['currency_id'] || null !== $data['currency_code'])) {
@ -178,4 +161,33 @@ class AccountUpdateService
return 1 === AccountType::whereIn('type', [AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE])->where('id', $accountTypeId)->count(); return 1 === AccountType::whereIn('type', [AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE])->where('id', $accountTypeId)->count();
} }
/**
* @param Account $account
* @param array $data
*
* @return Account
*/
private function updateAccount(Account $account, array $data): Account
{
// update the account itself:
$account->name = $data['name'] ?? $account->name;
$account->active = $data['active'] ?? $account->active;
$account->iban = $data['iban'] ?? $account->iban;
// if account type is a liability, the liability type (account type)
// can be updated to another one.
if ($this->isLiability($account) && $this->isLiabilityTypeId((int) ($data['account_type_id'] ?? 0))) {
$account->account_type_id = (int) $data['account_type_id'];
}
// update virtual balance (could be set to zero if empty string).
if (null !== $data['virtual_balance']) {
$account->virtual_balance = '' === trim($data['virtual_balance']) ? '0' : $data['virtual_balance'];
}
$account->save();
return $account;
}
} }