Skip to content

Firefly III: a personal finances manager

Simplify code

James Cole53a6af77794d
diff --git a/app/Console/Commands/Correction/FixRecurringTransactions.php b/app/Console/Commands/Correction/FixRecurringTransactions.php
index 6d3b9b0452..ce51e3a10e 100644
--- a/app/Console/Commands/Correction/FixRecurringTransactions.php
+++ b/app/Console/Commands/Correction/FixRecurringTransactions.php
@@ -81,28 +81,7 @@ class FixRecurringTransactions extends Command
$users = $this->userRepos->all();
/** @var User $user */
foreach ($users as $user) {
- $this->recurringRepos->setUser($user);
- $recurrences = $this->recurringRepos->get();
- /** @var Recurrence $recurrence */
- foreach ($recurrences as $recurrence) {
- /** @var RecurrenceTransaction $transaction */
- foreach ($recurrence->recurrenceTransactions as $transaction) {
- $source = $transaction->sourceAccount;
- $destination = $transaction->destinationAccount;
- $type = $recurrence->transactionType;
- $link = config(sprintf('firefly.account_to_transaction.%s.%s', $source->accountType->type, $destination->accountType->type));
- if (null !== $link && strtolower($type->type) !== strtolower($link)) {
- $this->warn(
- sprintf('Recurring transaction #%d should be a "%s" but is a "%s" and will be corrected.', $recurrence->id, $link, $type->type)
- );
- $transactionType = TransactionType::whereType($link)->first();
- if (null !== $transactionType) {
- $recurrence->transaction_type_id = $transactionType->id;
- $recurrence->save();
- }
- }
- }
- }
+ $this->processUser($user);
}
}
@@ -119,5 +98,49 @@ class FixRecurringTransactions extends Command
$this->userRepos = app(UserRepositoryInterface::class);
}
+ /**
+ * @param User $user
+ */
+ private function processUser(User $user): void
+ {
+ $this->recurringRepos->setUser($user);
+ $recurrences = $this->recurringRepos->get();
+ /** @var Recurrence $recurrence */
+ foreach ($recurrences as $recurrence) {
+ $this->processRecurrence($recurrence);
+ }
+ }
+
+ /**
+ * @param Recurrence $recurrence
+ */
+ private function processRecurrence(Recurrence $recurrence): void
+ {
+ /** @var RecurrenceTransaction $transaction */
+ foreach ($recurrence->recurrenceTransactions as $transaction) {
+ $this->processTransaction($recurrence, $transaction);
+ }
+ }
+ /**
+ * @param Recurrence $recurrence
+ * @param RecurrenceTransaction $transaction
+ */
+ private function processTransaction(Recurrence $recurrence, RecurrenceTransaction $transaction): void
+ {
+ $source = $transaction->sourceAccount;
+ $destination = $transaction->destinationAccount;
+ $type = $recurrence->transactionType;
+ $link = config(sprintf('firefly.account_to_transaction.%s.%s', $source->accountType->type, $destination->accountType->type));
+ if (null !== $link && strtolower($type->type) !== strtolower($link)) {
+ $this->warn(
+ sprintf('Recurring transaction #%d should be a "%s" but is a "%s" and will be corrected.', $recurrence->id, $link, $type->type)
+ );
+ $transactionType = TransactionType::whereType($link)->first();
+ if (null !== $transactionType) {
+ $recurrence->transaction_type_id = $transactionType->id;
+ $recurrence->save();
+ }
+ }
+ }
}