diff --git a/app/Console/Commands/UpgradeDatabase.php b/app/Console/Commands/UpgradeDatabase.php index 07cebe2fdf..a526e31a5a 100644 --- a/app/Console/Commands/UpgradeDatabase.php +++ b/app/Console/Commands/UpgradeDatabase.php @@ -57,8 +57,6 @@ class UpgradeDatabase extends Command public function handle() { $this->setTransactionIdentifier(); - - } /** @@ -85,42 +83,52 @@ class UpgradeDatabase extends Command $journalIds = array_unique($result->pluck('id')->toArray()); foreach ($journalIds as $journalId) { - // grab all positive transactiosn from this journal that are not deleted. - // for each one, grab the negative opposing one which has 0 as an identifier and give it the same identifier. - $identifier = 0; - $processed = []; - $transactions = Transaction::where('transaction_journal_id', $journalId)->where('amount', '>', 0)->get(); - /** @var Transaction $transaction */ - foreach ($transactions as $transaction) { - // find opposing: - $amount = bcmul(strval($transaction->amount), '-1'); + $this->updateJournal($journalId); + } + } - try { - /** @var Transaction $opposing */ - $opposing = Transaction::where('transaction_journal_id', $journalId) - ->where('amount', $amount)->where('identifier', '=', 0) - ->whereNotIn('id', $processed) - ->first(); - } catch (QueryException $e) { - Log::error($e->getMessage()); - $this->error('Firefly III could not find the "identifier" field in the "transactions" table.'); - $this->error(sprintf('This field is required for Firefly III version %s to run.', config('firefly.version'))); - $this->error('Please run "php artisan migrate" to add this field to the table.'); - $this->info('Then, run "php artisan firefly:upgrade-database" to try again.'); - break 2; - } - if (!is_null($opposing)) { - // give both a new identifier: - $transaction->identifier = $identifier; - $transaction->save(); - $opposing->identifier = $identifier; - $opposing->save(); - $processed[] = $transaction->id; - $processed[] = $opposing->id; - $this->line(sprintf('Database upgrade for journal #%d, transactions #%d and #%d', $journalId, $transaction->id, $opposing->id)); - } - $identifier++; + /** + * grab all positive transactiosn from this journal that are not deleted. for each one, grab the negative opposing one + * which has 0 as an identifier and give it the same identifier. + * + * @param int $journalId + */ + private function updateJournal(int $journalId) + { + $identifier = 0; + $processed = []; + $transactions = Transaction::where('transaction_journal_id', $journalId)->where('amount', '>', 0)->get(); + /** @var Transaction $transaction */ + foreach ($transactions as $transaction) { + // find opposing: + $amount = bcmul(strval($transaction->amount), '-1'); + + try { + /** @var Transaction $opposing */ + $opposing = Transaction::where('transaction_journal_id', $journalId) + ->where('amount', $amount)->where('identifier', '=', 0) + ->whereNotIn('id', $processed) + ->first(); + } catch (QueryException $e) { + Log::error($e->getMessage()); + $this->error('Firefly III could not find the "identifier" field in the "transactions" table.'); + $this->error(sprintf('This field is required for Firefly III version %s to run.', config('firefly.version'))); + $this->error('Please run "php artisan migrate" to add this field to the table.'); + $this->info('Then, run "php artisan firefly:upgrade-database" to try again.'); + + return; } + if (!is_null($opposing)) { + // give both a new identifier: + $transaction->identifier = $identifier; + $transaction->save(); + $opposing->identifier = $identifier; + $opposing->save(); + $processed[] = $transaction->id; + $processed[] = $opposing->id; + $this->line(sprintf('Database upgrade for journal #%d, transactions #%d and #%d', $journalId, $transaction->id, $opposing->id)); + } + $identifier++; } } } diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 7c9c52fc01..aa2805e9ae 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -76,15 +76,4 @@ class Kernel extends ConsoleKernel { require base_path('routes/console.php'); } - - /** - * Define the application's command schedule. - * - * @param \Illuminate\Console\Scheduling\Schedule $schedule - * - * @return void - */ - protected function schedule(Schedule $schedule) - { - } } diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index c8b15f984d..2d1a846044 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -72,6 +72,7 @@ class Handler extends ExceptionHandler * This is a great spot to send exceptions to Sentry, Bugsnag, etc. * * @param Exception $exception + * @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's exactly five. * * @return void */ @@ -98,8 +99,8 @@ class Handler extends ExceptionHandler ]; // create job that will mail. - $ip = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0'; - $job = new MailError($userData, env('SITE_OWNER', ''), $ip, $data); + $ipAddress = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0'; + $job = new MailError($userData, env('SITE_OWNER', ''), $ipAddress, $data); dispatch($job); } diff --git a/app/Export/Collector/JournalExportCollector.php b/app/Export/Collector/JournalExportCollector.php index 57d65fc4e7..e9bfbca879 100644 --- a/app/Export/Collector/JournalExportCollector.php +++ b/app/Export/Collector/JournalExportCollector.php @@ -287,7 +287,7 @@ class JournalExportCollector extends BasicCollector implements CollectorInterfac } /** - * + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ private function getWorkSet() { diff --git a/app/Export/Entry/Entry.php b/app/Export/Entry/Entry.php index 259dd675ab..c29467a178 100644 --- a/app/Export/Entry/Entry.php +++ b/app/Export/Entry/Entry.php @@ -29,6 +29,7 @@ use Crypt; * * * Class Entry + * @SuppressWarnings(PHPMD.LongVariable) * * @package FireflyIII\Export\Entry */ @@ -71,33 +72,21 @@ final class Entry */ public static function fromObject($object): Entry { - $entry = new self; - - // journal information: - $entry->journal_id = $object->transaction_journal_id; - $entry->description = $object->journal_encrypted === 1 ? Crypt::decrypt($object->journal_description) : $object->journal_description; - $entry->amount = round($object->amount, 2); // always positive - $entry->date = $object->date; - $entry->transaction_type = $object->transaction_type; - $entry->currency_code = $object->transaction_currency_code; - - // source information: - $entry->source_account_id = $object->account_id; - $entry->source_account_name = $object->account_name_encrypted === 1 ? Crypt::decrypt($object->account_name) : $object->account_name; - - - // destination information + $entry = new self; + $entry->journal_id = $object->transaction_journal_id; + $entry->description = self::decrypt($object->journal_encrypted, $object->journal_description); + $entry->amount = round($object->amount, 2); // always positive + $entry->date = $object->date; + $entry->transaction_type = $object->transaction_type; + $entry->currency_code = $object->transaction_currency_code; + $entry->source_account_id = $object->account_id; + $entry->source_account_name = self::decrypt($object->account_name_encrypted, $object->account_name); $entry->destination_account_id = $object->opposing_account_id; - $entry->destination_account_name = $object->opposing_account_encrypted === 1 ? Crypt::decrypt($object->opposing_account_name) - : $object->opposing_account_name; - - - // category and budget - $entry->category_id = $object->category_id ?? ''; - $entry->category_name = $object->category_name ?? ''; - $entry->budget_id = $object->budget_id ?? ''; - $entry->budget_name = $object->budget_name ?? ''; - + $entry->destination_account_name = self::decrypt($object->opposing_account_encrypted, $object->opposing_account_name); + $entry->category_id = $object->category_id ?? ''; + $entry->category_name = $object->category_name ?? ''; + $entry->budget_id = $object->budget_id ?? ''; + $entry->budget_name = $object->budget_name ?? ''; // update description when transaction description is different: if (!is_null($object->description) && $object->description != $entry->description) { @@ -107,4 +96,19 @@ final class Entry return $entry; } + /** + * @param int $isEncrypted + * @param $value + * + * @return string + */ + protected static function decrypt(int $isEncrypted, $value) + { + if ($isEncrypted === 1) { + return Crypt::decrypt($value); + } + + return $value; + } + }