Clean up some code.

This commit is contained in:
James Cole
2019-06-07 18:13:54 +02:00
parent fba3cb6d90
commit 9c5df6ab6e
16 changed files with 55 additions and 32 deletions

View File

@@ -25,6 +25,8 @@ use DB;
use FireflyIII\Models\Transaction; use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Exception;
use Log;
/** /**
* Class DeleteEmptyJournals * Class DeleteEmptyJournals
@@ -68,7 +70,11 @@ class DeleteEmptyJournals extends Command
->get(['transaction_journals.id']); ->get(['transaction_journals.id']);
foreach ($set as $entry) { foreach ($set as $entry) {
TransactionJournal::find($entry->id)->delete(); try {
TransactionJournal::find($entry->id)->delete();
} catch (Exception $e) {
Log::info(sprintf('Could not delete entry: %s', $e->getMessage()));
}
$this->info(sprintf('Deleted empty transaction journal #%d', $entry->id)); $this->info(sprintf('Deleted empty transaction journal #%d', $entry->id));
++$count; ++$count;
} }
@@ -103,7 +109,11 @@ class DeleteEmptyJournals extends Command
$count = (int)$row->the_count; $count = (int)$row->the_count;
if (1 === $count % 2) { if (1 === $count % 2) {
// uneven number, delete journal and transactions: // uneven number, delete journal and transactions:
TransactionJournal::find((int)$row->transaction_journal_id)->delete(); try {
TransactionJournal::find((int)$row->transaction_journal_id)->delete();
} catch(Exception $e) {
Log::info(sprintf('Could not delete journal: %s', $e->getMessage()));
}
Transaction::where('transaction_journal_id', (int)$row->transaction_journal_id)->delete(); Transaction::where('transaction_journal_id', (int)$row->transaction_journal_id)->delete();
$this->info(sprintf('Deleted transaction journal #%d because it had an uneven number of transactions.', $row->transaction_journal_id)); $this->info(sprintf('Deleted transaction journal #%d because it had an uneven number of transactions.', $row->transaction_journal_id));
$total++; $total++;

View File

@@ -26,6 +26,7 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use stdClass; use stdClass;
use Log;
/** /**
* Deletes transactions where the journal has been deleted. * Deletes transactions where the journal has been deleted.
@@ -78,7 +79,11 @@ class DeleteOrphanedTransactions extends Command
// delete journals // delete journals
$journal = TransactionJournal::find((int)$transaction->transaction_journal_id); $journal = TransactionJournal::find((int)$transaction->transaction_journal_id);
if ($journal) { if ($journal) {
$journal->delete(); try {
$journal->delete();
} catch (Exception $e) {
Log::info(sprintf('Could not delete transaction %s', $e->getMessage()));
}
} }
Transaction::where('transaction_journal_id', (int)$transaction->transaction_journal_id)->delete(); Transaction::where('transaction_journal_id', (int)$transaction->transaction_journal_id)->delete();
$this->line( $this->line(

View File

@@ -28,7 +28,6 @@ use Crypt;
use DB; use DB;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Preference; use FireflyIII\Models\Preference;
use FireflyIII\Support\Facades\FireflyConfig;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Contracts\Encryption\DecryptException; use Illuminate\Contracts\Encryption\DecryptException;
use Log; use Log;
@@ -56,6 +55,7 @@ class DecryptDatabase extends Command
* Execute the console command. * Execute the console command.
* *
* @return mixed * @return mixed
* @throws FireflyException
*/ */
public function handle() public function handle()
{ {
@@ -113,7 +113,7 @@ class DecryptDatabase extends Command
$this->line(sprintf('Decrypted the data in table "%s".', $table)); $this->line(sprintf('Decrypted the data in table "%s".', $table));
// mark as decrypted: // mark as decrypted:
$configName = sprintf('is_decrypted_%s', $table); $configName = sprintf('is_decrypted_%s', $table);
FireflyConfig::set($configName, true); app('fireflyconfig')->set($configName, true);
} }
$this->info('Done!'); $this->info('Done!');
@@ -129,7 +129,7 @@ class DecryptDatabase extends Command
private function isDecrypted(string $table): bool private function isDecrypted(string $table): bool
{ {
$configName = sprintf('is_decrypted_%s', $table); $configName = sprintf('is_decrypted_%s', $table);
$configVar = FireflyConfig::get($configName, false); $configVar = app('fireflyconfig')->get($configName, false);
if (null !== $configVar) { if (null !== $configVar) {
return (bool)$configVar->data; return (bool)$configVar->data;
} }
@@ -139,9 +139,11 @@ class DecryptDatabase extends Command
/** /**
* @param $value * Tries to decrypt data. Will only throw an exception when the MAC is invalid.
* *
* @return mixed * @param $value
* @return string
* @throws FireflyException
*/ */
private function tryDecrypt($value) private function tryDecrypt($value)
{ {

View File

@@ -75,10 +75,10 @@ class ScanAttachments extends Command
$this->error(sprintf('Could not decrypt data of attachment #%d: %s', $attachment->id, $e->getMessage())); $this->error(sprintf('Could not decrypt data of attachment #%d: %s', $attachment->id, $e->getMessage()));
continue; continue;
} }
$tmpfname = tempnam(sys_get_temp_dir(), 'FireflyIII'); $tempFileName = tempnam(sys_get_temp_dir(), 'FireflyIII');
file_put_contents($tmpfname, $decrypted); file_put_contents($tempFileName, $decrypted);
$md5 = md5_file($tmpfname); $md5 = md5_file($tempFileName);
$mime = mime_content_type($tmpfname); $mime = mime_content_type($tempFileName);
$attachment->md5 = $md5; $attachment->md5 = $md5;
$attachment->mime = $mime; $attachment->mime = $mime;
$attachment->save(); $attachment->save();

View File

@@ -36,7 +36,6 @@ use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\Rule\RuleRepositoryInterface; use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface; use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
use FireflyIII\TransactionRules\Engine\RuleEngine; use FireflyIII\TransactionRules\Engine\RuleEngine;
use FireflyIII\TransactionRules\Processor;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log; use Log;

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Console\Commands\Tools; namespace FireflyIII\Console\Commands\Tools;
use Exception;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Support\Cronjobs\RecurringCronjob; use FireflyIII\Support\Cronjobs\RecurringCronjob;
use Illuminate\Console\Command; use Illuminate\Console\Command;
@@ -52,9 +53,8 @@ class Cron extends Command
'; ';
/** /**
* Execute the console command. * @return int
* * @throws Exception
* @return mixed
*/ */
public function handle(): int public function handle(): int
{ {

View File

@@ -151,7 +151,7 @@ class BackToJournals extends Command
{ {
$journalIds = $this->getIdsForBudgets(); $journalIds = $this->getIdsForBudgets();
$journals = TransactionJournal::whereIn('id', $journalIds)->with(['transactions', 'budgets', 'transactions.budgets'])->get(); $journals = TransactionJournal::whereIn('id', $journalIds)->with(['transactions', 'budgets', 'transactions.budgets'])->get();
$this->line(sprintf('Check %d transaction journals for budget info.', $journals->count())); $this->line(sprintf('Check %d transaction journals for budget info.',count($journals)));
/** @var TransactionJournal $journal */ /** @var TransactionJournal $journal */
foreach ($journals as $journal) { foreach ($journals as $journal) {
$this->migrateBudgetsForJournal($journal); $this->migrateBudgetsForJournal($journal);
@@ -193,7 +193,7 @@ class BackToJournals extends Command
{ {
$journalIds = $this->getIdsForCategories(); $journalIds = $this->getIdsForCategories();
$journals = TransactionJournal::whereIn('id', $journalIds)->with(['transactions', 'categories', 'transactions.categories'])->get(); $journals = TransactionJournal::whereIn('id', $journalIds)->with(['transactions', 'categories', 'transactions.categories'])->get();
$this->line(sprintf('Check %d transaction journals for category info.', $journals->count())); $this->line(sprintf('Check %d transaction journals for category info.', count($journals)));
/** @var TransactionJournal $journal */ /** @var TransactionJournal $journal */
foreach ($journals as $journal) { foreach ($journals as $journal) {
$this->migrateCategoriesForJournal($journal); $this->migrateCategoriesForJournal($journal);

View File

@@ -279,7 +279,9 @@ class JournalCurrencies extends Command
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.NPathComplexity)
* *
* @param Transaction $transaction * @param TransactionJournal $journal
* @param Transaction $source
* @param Transaction $destination
*/ */
private function updateTransactionCurrency(TransactionJournal $journal, Transaction $source, Transaction $destination): void private function updateTransactionCurrency(TransactionJournal $journal, Transaction $source, Transaction $destination): void
{ {

View File

@@ -85,7 +85,7 @@ class TransactionIdentifier extends Command
$journalIds = array_unique($result->pluck('id')->toArray()); $journalIds = array_unique($result->pluck('id')->toArray());
$count= 0; $count= 0;
foreach ($journalIds as $journalId) { foreach ($journalIds as $journalId) {
$this->updateJournalidentifiers((int)$journalId); $this->updateJournalIdentifiers((int)$journalId);
$count++; $count++;
} }
$end = round(microtime(true) - $start, 2); $end = round(microtime(true) - $start, 2);
@@ -117,12 +117,12 @@ class TransactionIdentifier extends Command
} }
/** /**
* grab all positive transactiosn from this journal that are not deleted. for each one, grab the negative opposing one * grab all positive transactions 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. * which has 0 as an identifier and give it the same identifier.
* *
* @param int $journalId * @param int $journalId
*/ */
private function updateJournalidentifiers(int $journalId): void private function updateJournalIdentifiers(int $journalId): void
{ {
$identifier = 0; $identifier = 0;
$processed = []; $processed = [];

View File

@@ -49,7 +49,7 @@ class Kernel extends ConsoleKernel
/** /**
* Define the application's command schedule. * Define the application's command schedule.
* *
* @param \Illuminate\Console\Scheduling\Schedule $schedule * @param Schedule $schedule
*/ */
protected function schedule(Schedule $schedule): void protected function schedule(Schedule $schedule): void
{ {

View File

@@ -43,6 +43,7 @@ declare(strict_types=1);
namespace FireflyIII\Events; namespace FireflyIII\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel; use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Foundation\Events\Dispatchable;
@@ -80,7 +81,7 @@ class RequestedReportOnJournals
/** /**
* Get the channels the event should broadcast on. * Get the channels the event should broadcast on.
* *
* @return \Illuminate\Broadcasting\Channel|array * @return Channel|array
*/ */
public function broadcastOn() public function broadcastOn()
{ {

View File

@@ -45,6 +45,7 @@ class StoredTransactionGroup extends Event
* Create a new event instance. * Create a new event instance.
* *
* @param TransactionGroup $transactionGroup * @param TransactionGroup $transactionGroup
* @param bool $applyRules
*/ */
public function __construct(TransactionGroup $transactionGroup, bool $applyRules = true) public function __construct(TransactionGroup $transactionGroup, bool $applyRules = true)
{ {

View File

@@ -25,7 +25,6 @@ declare(strict_types=1);
namespace FireflyIII\Events; namespace FireflyIII\Events;
use FireflyIII\Models\TransactionGroup; use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
/** /**

View File

@@ -81,7 +81,7 @@ class Handler extends ExceptionHandler
return response()->json( return response()->json(
[ [
'message' => $exception->getMessage(), 'message' => $exception->getMessage(),
'exception' => \get_class($exception), 'exception' => get_class($exception),
'line' => $exception->getLine(), 'line' => $exception->getLine(),
'file' => $exception->getFile(), 'file' => $exception->getFile(),
'trace' => $exception->getTrace(), 'trace' => $exception->getTrace(),
@@ -89,7 +89,7 @@ class Handler extends ExceptionHandler
); );
} }
return response()->json(['message' => 'Internal Firefly III Exception. See log files.', 'exception' => \get_class($exception)], 500); return response()->json(['message' => 'Internal Firefly III Exception. See log files.', 'exception' => get_class($exception)], 500);
} }
if ($exception instanceof FireflyException || $exception instanceof ErrorException || $exception instanceof OAuthServerException) { if ($exception instanceof FireflyException || $exception instanceof ErrorException || $exception instanceof OAuthServerException) {
@@ -104,11 +104,11 @@ class Handler extends ExceptionHandler
/** /**
* Report or log an exception. * Report or log an exception.
* *
* This is a great spot to send exceptions to Sentry, Bugsnag, etc. * This is a great spot to send exceptions to Sentry etc.
* *
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's five its fine. * @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's five its fine.
* *
* @param \Exception $exception * @param Exception $exception
* *
* @return mixed|void * @return mixed|void
* *
@@ -131,7 +131,7 @@ class Handler extends ExceptionHandler
$userData['email'] = auth()->user()->email; $userData['email'] = auth()->user()->email;
} }
$data = [ $data = [
'class' => \get_class($exception), 'class' => get_class($exception),
'errorMessage' => $exception->getMessage(), 'errorMessage' => $exception->getMessage(),
'time' => date('r'), 'time' => date('r'),
'stackTrace' => $exception->getTraceAsString(), 'stackTrace' => $exception->getTraceAsString(),

View File

@@ -24,9 +24,11 @@ declare(strict_types=1);
namespace FireflyIII\Exceptions; namespace FireflyIII\Exceptions;
use Exception;
/** /**
* Class NotImplementedException. * Class NotImplementedException.
*/ */
class NotImplementedException extends \Exception class NotImplementedException extends Exception
{ {
} }

View File

@@ -24,9 +24,11 @@ declare(strict_types=1);
namespace FireflyIII\Exceptions; namespace FireflyIII\Exceptions;
use Exception;
/** /**
* Class ValidationExceptions. * Class ValidationExceptions.
*/ */
class ValidationException extends \Exception class ValidationException extends Exception
{ {
} }