Improve results when presented with invalid data. #701

This commit is contained in:
James Cole
2017-07-14 17:57:20 +02:00
parent 5cf8f2f4f4
commit 3b3579025d
3 changed files with 53 additions and 15 deletions

View File

@@ -25,6 +25,7 @@ use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\User;
use Log;
use Validator;
/**
@@ -236,7 +237,7 @@ class AccountRepository implements AccountRepositoryInterface
$data['accountType'] = $data['accountType'] ?? 'invalid';
$type = config('firefly.accountTypeByIdentifier.' . $data['accountType']);
$accountType = AccountType::whereType($type)->first();
$data['iban'] = $this->filterIban($data['iban']);
// verify account type
if (is_null($accountType)) {
throw new FireflyException(sprintf('Account type "%s" is invalid. Cannot create account.', $data['accountType']));
@@ -251,16 +252,17 @@ class AccountRepository implements AccountRepositoryInterface
}
// create it:
$newAccount = new Account(
[
'user_id' => $this->user->id,
'account_type_id' => $accountType->id,
'name' => $data['name'],
'virtual_balance' => $data['virtualBalance'],
'active' => $data['active'] === true ? true : false,
'iban' => $data['iban'],
]
);
$databaseData
= [
'user_id' => $this->user->id,
'account_type_id' => $accountType->id,
'name' => $data['name'],
'virtual_balance' => $data['virtualBalance'],
'active' => $data['active'] === true ? true : false,
'iban' => $data['iban'],
];
$newAccount = new Account($databaseData);
Log::debug('Final account creation dataset', $databaseData);
$newAccount->save();
// verify its creation:
if (is_null($newAccount->id)) {
@@ -268,6 +270,7 @@ class AccountRepository implements AccountRepositoryInterface
sprintf('Could not create account "%s" (%d error(s))', $data['name'], $newAccount->getErrors()->count()), $newAccount->getErrors()->toArray()
);
throw new FireflyException(sprintf('Tried to create account named "%s" but failed. The logs have more details.', $data['name']));
}
Log::debug(sprintf('Created new account #%d named "%s" of type %s.', $newAccount->id, $newAccount->name, $accountType->type));
@@ -491,4 +494,26 @@ class AccountRepository implements AccountRepositoryInterface
return false;
}
/**
* @param string $iban
*
* @return null|string
*/
private function filterIban(string $iban = null)
{
if (is_null($iban)) {
return null;
}
$data = ['iban' => $iban];
$rules = ['iban' => 'required|iban'];
$validator = Validator::make($data, $rules);
if ($validator->fails()) {
Log::error(sprintf('Detected invalid IBAN ("%s"). Return NULL instead.', $iban));
return null;
}
return $iban;
}
}