Code cleanup.

This commit is contained in:
James Cole
2023-12-20 19:35:52 +01:00
parent c4f6366642
commit 64ec0cf62e
997 changed files with 12908 additions and 28136 deletions

View File

@@ -33,7 +33,6 @@ use FireflyIII\Rules\UniqueIban;
use FireflyIII\Services\Internal\Update\AccountUpdateService;
use FireflyIII\User;
use Illuminate\Database\QueryException;
use Validator;
/**
* Class TransactionFactory
@@ -49,8 +48,6 @@ class TransactionFactory
/**
* Constructor.
*
*/
public function __construct()
{
@@ -61,10 +58,6 @@ class TransactionFactory
/**
* Create transaction with negative amount (for source accounts).
*
* @param string $amount
* @param string|null $foreignAmount
*
* @return Transaction
* @throws FireflyException
*/
public function createNegative(string $amount, ?string $foreignAmount): Transaction
@@ -80,10 +73,64 @@ class TransactionFactory
}
/**
* @param string $amount
* @param string|null $foreignAmount
* Create transaction with positive amount (for destination accounts).
*
* @return Transaction
* @throws FireflyException
*/
public function createPositive(string $amount, ?string $foreignAmount): Transaction
{
if ('' === $foreignAmount) {
$foreignAmount = null;
}
if (null !== $foreignAmount) {
$foreignAmount = app('steam')->positive($foreignAmount);
}
return $this->create(app('steam')->positive($amount), $foreignAmount);
}
public function setAccount(Account $account): void
{
$this->account = $account;
}
public function setAccountInformation(array $accountInformation): void
{
$this->accountInformation = $accountInformation;
}
public function setCurrency(TransactionCurrency $currency): void
{
$this->currency = $currency;
}
/**
* @param null|TransactionCurrency $foreignCurrency |null
*/
public function setForeignCurrency(?TransactionCurrency $foreignCurrency): void
{
$this->foreignCurrency = $foreignCurrency;
}
public function setJournal(TransactionJournal $journal): void
{
$this->journal = $journal;
}
public function setReconciled(bool $reconciled): void
{
$this->reconciled = $reconciled;
}
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setUser(User $user): void
{
// empty function.
}
/**
* @throws FireflyException
*/
private function create(string $amount, ?string $foreignAmount): Transaction
@@ -102,13 +149,15 @@ class TransactionFactory
'foreign_currency_id' => null,
'identifier' => 0,
];
try {
/** @var Transaction|null $result */
/** @var null|Transaction $result */
$result = Transaction::create($data);
} catch (QueryException $e) {
app('log')->error(sprintf('Could not create transaction: %s', $e->getMessage()), $data);
app('log')->error($e->getMessage());
app('log')->error($e->getTraceAsString());
throw new FireflyException(sprintf('Query exception when creating transaction: %s', $e->getMessage()), 0, $e);
}
if (null === $result) {
@@ -127,9 +176,9 @@ class TransactionFactory
);
// do foreign currency thing: add foreign currency info to $one and $two if necessary.
if (null !== $this->foreignCurrency &&
null !== $foreignAmount &&
$this->foreignCurrency->id !== $this->currency->id) {
if (null !== $this->foreignCurrency
&& null !== $foreignAmount
&& $this->foreignCurrency->id !== $this->currency->id) {
$result->foreign_currency_id = $this->foreignCurrency->id;
$result->foreign_amount = $foreignAmount;
}
@@ -142,29 +191,32 @@ class TransactionFactory
}
/**
* @return void
* @throws FireflyException
*/
private function updateAccountInformation(): void
{
if (!array_key_exists('iban', $this->accountInformation)) {
app('log')->debug('No IBAN information in array, will not update.');
return;
}
if ('' !== (string)$this->account->iban) {
app('log')->debug('Account already has IBAN information, will not update.');
return;
}
if ($this->account->iban === $this->accountInformation['iban']) {
app('log')->debug('Account already has this IBAN, will not update.');
return;
}
// validate info:
$validator = Validator::make(['iban' => $this->accountInformation['iban']], [
$validator = \Validator::make(['iban' => $this->accountInformation['iban']], [
'iban' => ['required', new UniqueIban($this->account, $this->account->accountType->type)],
]);
if ($validator->fails()) {
app('log')->debug('Invalid or non-unique IBAN, will not update.');
return;
}
@@ -172,92 +224,4 @@ class TransactionFactory
$service = app(AccountUpdateService::class);
$service->update($this->account, ['iban' => $this->accountInformation['iban']]);
}
/**
* Create transaction with positive amount (for destination accounts).
*
* @param string $amount
* @param string|null $foreignAmount
*
* @return Transaction
* @throws FireflyException
*/
public function createPositive(string $amount, ?string $foreignAmount): Transaction
{
if ('' === $foreignAmount) {
$foreignAmount = null;
}
if (null !== $foreignAmount) {
$foreignAmount = app('steam')->positive($foreignAmount);
}
return $this->create(app('steam')->positive($amount), $foreignAmount);
}
/**
* @param Account $account
*
*/
public function setAccount(Account $account): void
{
$this->account = $account;
}
/**
* @param array $accountInformation
*/
public function setAccountInformation(array $accountInformation): void
{
$this->accountInformation = $accountInformation;
}
/**
* @param TransactionCurrency $currency
*
*/
public function setCurrency(TransactionCurrency $currency): void
{
$this->currency = $currency;
}
/**
* @param TransactionCurrency|null $foreignCurrency |null
*
*/
public function setForeignCurrency(?TransactionCurrency $foreignCurrency): void
{
$this->foreignCurrency = $foreignCurrency;
}
/**
* @param TransactionJournal $journal
*
*/
public function setJournal(TransactionJournal $journal): void
{
$this->journal = $journal;
}
/**
* @param bool $reconciled
*
*/
public function setReconciled(bool $reconciled): void
{
$this->reconciled = $reconciled;
}
/**
* @param User $user
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setUser(User $user): void
{
// empty function.
}
}