Code cleanup

This commit is contained in:
James Cole
2021-03-21 09:15:40 +01:00
parent da1751940e
commit 206845575c
317 changed files with 7418 additions and 7362 deletions

View File

@@ -159,7 +159,7 @@ class CorrectOpeningBalanceCurrencies extends Command
}
/**
* @param TransactionJournal $journal
* @param TransactionJournal $journal
* @param TransactionCurrency $currency
*
* @return int

View File

@@ -31,24 +31,23 @@ use Str;
/**
* Class CreateFirstUser
*
* @package FireflyIII\Console\Commands
*/
class CreateFirstUser extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'firefly-iii:create-first-user {email}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Creates a new user and gives admin rights. Outputs the password on the command line. Strictly for testing.';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'firefly-iii:create-first-user {email}';
private UserRepositoryInterface $repository;
/**
@@ -60,12 +59,14 @@ class CreateFirstUser extends Command
{
if ('testing' !== env('APP_ENV', 'local')) {
$this->error('This command only works in the testing environment.');
return 1;
}
$this->stupidLaravel();
$count = $this->repository->count();
if ($count > 0) {
$this->error('Already have more than zero users in DB.');
return 1;
}
$data = [
@@ -82,6 +83,7 @@ class CreateFirstUser extends Command
$this->info(sprintf('Created new admin user (ID #%d) with email address "%s" and password "%s".', $user->id, $user->email, $password));
$this->error('Change this password.');
return 0;
}

View File

@@ -85,6 +85,26 @@ class DecryptDatabase extends Command
return 0;
}
/**
* @param string $table
* @param array $fields
*/
private function decryptTable(string $table, array $fields): void
{
if ($this->isDecrypted($table)) {
$this->info(sprintf('No decryption required for table "%s".', $table));
return;
}
foreach ($fields as $field) {
$this->decryptField($table, $field);
}
$this->line(sprintf('Decrypted the data in table "%s".', $table));
// mark as decrypted:
$configName = sprintf('is_decrypted_%s', $table);
app('fireflyconfig')->set($configName, true);
}
/**
* @param string $table
*
@@ -100,53 +120,12 @@ class DecryptDatabase extends Command
Log::error($e->getMessage());
}
if (null !== $configVar) {
return (bool) $configVar->data;
return (bool)$configVar->data;
}
return false;
}
/**
* Tries to decrypt data. Will only throw an exception when the MAC is invalid.
*
* @param $value
*
* @return string
* @throws FireflyException
*/
private function tryDecrypt($value)
{
try {
$value = Crypt::decrypt($value);
} catch (DecryptException $e) {
if ('The MAC is invalid.' === $e->getMessage()) {
throw new FireflyException($e->getMessage()); // @codeCoverageIgnore
}
}
return $value;
}
/**
* @param string $table
* @param array $fields
*/
private function decryptTable(string $table, array $fields): void
{
if ($this->isDecrypted($table)) {
$this->info(sprintf('No decryption required for table "%s".', $table));
return;
}
foreach ($fields as $field) {
$this->decryptField($table, $field);
}
$this->line(sprintf('Decrypted the data in table "%s".', $table));
// mark as decrypted:
$configName = sprintf('is_decrypted_%s', $table);
app('fireflyconfig')->set($configName, true);
}
/**
* @param string $table
* @param string $field
@@ -171,7 +150,7 @@ class DecryptDatabase extends Command
if (null === $original) {
return;
}
$id = (int) $row->id;
$id = (int)$row->id;
$value = '';
try {
@@ -186,6 +165,7 @@ class DecryptDatabase extends Command
// A separate routine for preferences table:
if ('preferences' === $table) {
$this->decryptPreferencesRow($id, $value);
return;
}
@@ -194,6 +174,27 @@ class DecryptDatabase extends Command
}
}
/**
* Tries to decrypt data. Will only throw an exception when the MAC is invalid.
*
* @param $value
*
* @return string
* @throws FireflyException
*/
private function tryDecrypt($value)
{
try {
$value = Crypt::decrypt($value);
} catch (DecryptException $e) {
if ('The MAC is invalid.' === $e->getMessage()) {
throw new FireflyException($e->getMessage()); // @codeCoverageIgnore
}
}
return $value;
}
/**
* @param int $id
* @param string $value
@@ -209,11 +210,12 @@ class DecryptDatabase extends Command
Log::warning($message);
Log::warning($value);
Log::warning($e->getTraceAsString());
return;
}
/** @var Preference $object */
$object = Preference::find((int) $id);
$object = Preference::find((int)$id);
if (null !== $object) {
$object->data = $newValue;
$object->save();

View File

@@ -88,6 +88,7 @@ class ScanAttachments extends Command
}
app('telemetry')->feature('system.command.executed', $this->signature);
return 0;
}
}

View File

@@ -63,14 +63,6 @@ class MigrateRecurrenceType extends Command
return false; // @codeCoverageIgnore
}
/**
*
*/
private function getInvalidType(): TransactionType
{
return TransactionType::whereType(TransactionType::INVALID)->firstOrCreate(['type' => TransactionType::INVALID]);
}
/**
*
*/
@@ -85,14 +77,6 @@ class MigrateRecurrenceType extends Command
}
}
/**
*
*/
private function markAsExecuted(): void
{
app('fireflyconfig')->set(self::CONFIG_NAME, true);
}
private function migrateRecurrence(Recurrence $recurrence): void
{
$originalType = (int)$recurrence->transaction_type_id;
@@ -106,4 +90,20 @@ class MigrateRecurrenceType extends Command
}
$this->line(sprintf('Updated recurrence #%d to new transaction type model.', $recurrence->id));
}
/**
*
*/
private function getInvalidType(): TransactionType
{
return TransactionType::whereType(TransactionType::INVALID)->firstOrCreate(['type' => TransactionType::INVALID]);
}
/**
*
*/
private function markAsExecuted(): void
{
app('fireflyconfig')->set(self::CONFIG_NAME, true);
}
}

View File

@@ -80,6 +80,53 @@ class UpgradeFireflyInstructions extends Command
return 0;
}
/**
* Render upgrade instructions.
*/
private function updateInstructions(): void
{
/** @var string $version */
$version = config('firefly.version');
$config = config('upgrade.text.upgrade');
$text = '';
foreach (array_keys($config) as $compare) {
// if string starts with:
if (0 === strpos($version, $compare)) {
$text = $config[$compare];
}
}
$this->showLine();
$this->boxed('');
if (null === $text) {
$this->boxed(sprintf('Thank you for updating to Firefly III, v%s', $version));
$this->boxedInfo('There are no extra upgrade instructions.');
$this->boxed('Firefly III should be ready for use.');
$this->boxed('');
$this->showLine();
return;
}
$this->boxed(sprintf('Thank you for updating to Firefly III, v%s!', $version));
$this->boxedInfo($text);
$this->boxed('');
$this->showLine();
}
/**
* Show a line.
*/
private function showLine(): void
{
$line = '+';
for ($i = 0; $i < 78; ++$i) {
$line .= '-';
}
$line .= '+';
$this->line($line);
}
/**
* Show a nice box.
*
@@ -113,8 +160,8 @@ class UpgradeFireflyInstructions extends Command
{
/** @var string $version */
$version = config('firefly.version');
$config = config('upgrade.text.install');
$text = '';
$config = config('upgrade.text.install');
$text = '';
foreach (array_keys($config) as $compare) {
// if string starts with:
if (0 === strpos($version, $compare)) {
@@ -140,51 +187,4 @@ class UpgradeFireflyInstructions extends Command
$this->boxed('');
$this->showLine();
}
/**
* Show a line.
*/
private function showLine(): void
{
$line = '+';
for ($i = 0; $i < 78; ++$i) {
$line .= '-';
}
$line .= '+';
$this->line($line);
}
/**
* Render upgrade instructions.
*/
private function updateInstructions(): void
{
/** @var string $version */
$version = config('firefly.version');
$config = config('upgrade.text.upgrade');
$text = '';
foreach (array_keys($config) as $compare) {
// if string starts with:
if (0 === strpos($version, $compare)) {
$text = $config[$compare];
}
}
$this->showLine();
$this->boxed('');
if (null === $text) {
$this->boxed(sprintf('Thank you for updating to Firefly III, v%s', $version));
$this->boxedInfo('There are no extra upgrade instructions.');
$this->boxed('Firefly III should be ready for use.');
$this->boxed('');
$this->showLine();
return;
}
$this->boxed(sprintf('Thank you for updating to Firefly III, v%s!', $version));
$this->boxedInfo($text);
$this->boxed('');
$this->showLine();
}
}

View File

@@ -38,12 +38,12 @@ use Log;
trait VerifiesAccessToken
{
/**
* @throws FireflyException
* @return User
* @throws FireflyException
*/
public function getUser(): User
{
$userId = (int) $this->option('user');
$userId = (int)$this->option('user');
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
$user = $repository->findNull($userId);
@@ -70,8 +70,8 @@ trait VerifiesAccessToken
*/
protected function verifyAccessToken(): bool
{
$userId = (int) $this->option('user');
$token = (string) $this->option('token');
$userId = (int)$this->option('user');
$token = (string)$this->option('token');
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
$user = $repository->findNull($userId);