fix: replace console messages with unified command.

This commit is contained in:
James Cole
2023-06-20 07:16:56 +02:00
parent f2b2c2109f
commit 42043de34f
62 changed files with 767 additions and 512 deletions

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Console\Commands\Tools;
use Carbon\Carbon;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use FireflyIII\Console\Commands\VerifiesAccessToken;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\AccountType;
@@ -43,6 +44,7 @@ use Illuminate\Support\Facades\Log;
*/
class ApplyRules extends Command
{
use ShowsFriendlyMessages;
use VerifiesAccessToken;
/**
@@ -88,7 +90,7 @@ class ApplyRules extends Command
$start = microtime(true);
$this->stupidLaravel();
if (!$this->verifyAccessToken()) {
$this->error('Invalid access token.');
$this->friendlyError('Invalid access token.');
return 1;
}
@@ -111,11 +113,11 @@ class ApplyRules extends Command
$rulesToApply = $this->getRulesToApply();
$count = $rulesToApply->count();
if (0 === $count) {
$this->error('No rules or rule groups have been included.');
$this->warn('Make a selection using:');
$this->warn(' --rules=1,2,...');
$this->warn(' --rule_groups=1,2,...');
$this->warn(' --all_rules');
$this->friendlyError('No rules or rule groups have been included.');
$this->friendlyWarning('Make a selection using:');
$this->friendlyWarning(' --rules=1,2,...');
$this->friendlyWarning(' --rule_groups=1,2,...');
$this->friendlyWarning(' --all_rules');
return 1;
}
@@ -139,18 +141,61 @@ class ApplyRules extends Command
$ruleEngine->addOperator(['type' => 'date_before', 'value' => $this->endDate->format('Y-m-d')]);
// start running rules.
$this->line(sprintf('Will apply %d rule(s) to your transaction(s).', $count));
$this->friendlyLine(sprintf('Will apply %d rule(s) to your transaction(s).', $count));
// file the rule(s)
$ruleEngine->fire();
$this->line('');
$this->friendlyLine('');
$end = round(microtime(true) - $start, 2);
$this->line(sprintf('Done in %s seconds!', $end));
$this->friendlyPositive(sprintf('Done in %s seconds!', $end));
return 0;
}
/**
* @return Collection
*/
private function getRulesToApply(): Collection
{
$rulesToApply = new Collection();
/** @var RuleGroup $group */
foreach ($this->groups as $group) {
$rules = $this->ruleGroupRepository->getActiveStoreRules($group);
/** @var Rule $rule */
foreach ($rules as $rule) {
// if in rule selection, or group in selection or all rules, it's included.
$test = $this->includeRule($rule, $group);
if (true === $test) {
Log::debug(sprintf('Will include rule #%d "%s"', $rule->id, $rule->title));
$rulesToApply->push($rule);
}
}
}
return $rulesToApply;
}
/**
*/
private function grabAllRules(): void
{
$this->groups = $this->ruleGroupRepository->getActiveGroups();
}
/**
* @param Rule $rule
* @param RuleGroup $group
*
* @return bool
*/
private function includeRule(Rule $rule, RuleGroup $group): bool
{
return in_array($group->id, $this->ruleGroupSelection, true)
|| in_array($rule->id, $this->ruleSelection, true)
|| $this->allRules;
}
/**
* Laravel will execute ALL __construct() methods for ALL commands whenever a SINGLE command is
* executed. This leads to noticeable slow-downs and class calls. To prevent this, this method should
@@ -201,7 +246,7 @@ class ApplyRules extends Command
{
$accountString = $this->option('accounts');
if (null === $accountString || '' === $accountString) {
$this->error('Please use the --accounts option to indicate the accounts to apply rules to.');
$this->friendlyError('Please use the --accounts option to indicate the accounts to apply rules to.');
return false;
}
@@ -220,7 +265,7 @@ class ApplyRules extends Command
}
if (0 === $finalList->count()) {
$this->error('Please make sure all accounts in --accounts are asset accounts or liabilities.');
$this->friendlyError('Please make sure all accounts in --accounts are asset accounts or liabilities.');
return false;
}
@@ -229,53 +274,6 @@ class ApplyRules extends Command
return true;
}
/**
* @return bool
*/
private function verifyInputRuleGroups(): bool
{
$ruleGroupString = $this->option('rule_groups');
if (null === $ruleGroupString || '' === $ruleGroupString) {
// can be empty.
return true;
}
$ruleGroupList = explode(',', $ruleGroupString);
foreach ($ruleGroupList as $ruleGroupId) {
$ruleGroup = $this->ruleGroupRepository->find((int)$ruleGroupId);
if ($ruleGroup->active) {
$this->ruleGroupSelection[] = $ruleGroup->id;
}
if (false === $ruleGroup->active) {
$this->warn(sprintf('Will ignore inactive rule group #%d ("%s")', $ruleGroup->id, $ruleGroup->title));
}
}
return true;
}
/**
* @return bool
*/
private function verifyInputRules(): bool
{
$ruleString = $this->option('rules');
if (null === $ruleString || '' === $ruleString) {
// can be empty.
return true;
}
$ruleList = explode(',', $ruleString);
foreach ($ruleList as $ruleId) {
$rule = $this->ruleRepository->find((int)$ruleId);
if (null !== $rule && $rule->active) {
$this->ruleSelection[] = $rule->id;
}
}
return true;
}
/**
* @throws FireflyException
*/
@@ -313,45 +311,49 @@ class ApplyRules extends Command
}
/**
* @return bool
*/
private function grabAllRules(): void
private function verifyInputRuleGroups(): bool
{
$this->groups = $this->ruleGroupRepository->getActiveGroups();
}
$ruleGroupString = $this->option('rule_groups');
if (null === $ruleGroupString || '' === $ruleGroupString) {
// can be empty.
return true;
}
$ruleGroupList = explode(',', $ruleGroupString);
/**
* @return Collection
*/
private function getRulesToApply(): Collection
{
$rulesToApply = new Collection();
/** @var RuleGroup $group */
foreach ($this->groups as $group) {
$rules = $this->ruleGroupRepository->getActiveStoreRules($group);
/** @var Rule $rule */
foreach ($rules as $rule) {
// if in rule selection, or group in selection or all rules, it's included.
$test = $this->includeRule($rule, $group);
if (true === $test) {
Log::debug(sprintf('Will include rule #%d "%s"', $rule->id, $rule->title));
$rulesToApply->push($rule);
}
foreach ($ruleGroupList as $ruleGroupId) {
$ruleGroup = $this->ruleGroupRepository->find((int)$ruleGroupId);
if ($ruleGroup->active) {
$this->ruleGroupSelection[] = $ruleGroup->id;
}
if (false === $ruleGroup->active) {
$this->friendlyWarning(sprintf('Will ignore inactive rule group #%d ("%s")', $ruleGroup->id, $ruleGroup->title));
}
}
return $rulesToApply;
return true;
}
/**
* @param Rule $rule
* @param RuleGroup $group
*
* @return bool
*/
private function includeRule(Rule $rule, RuleGroup $group): bool
private function verifyInputRules(): bool
{
return in_array($group->id, $this->ruleGroupSelection, true)
|| in_array($rule->id, $this->ruleSelection, true)
|| $this->allRules;
$ruleString = $this->option('rules');
if (null === $ruleString || '' === $ruleString) {
// can be empty.
return true;
}
$ruleList = explode(',', $ruleString);
foreach ($ruleList as $ruleId) {
$rule = $this->ruleRepository->find((int)$ruleId);
if (null !== $rule && $rule->active) {
$this->ruleSelection[] = $rule->id;
}
}
return true;
}
}

View File

@@ -25,6 +25,7 @@ declare(strict_types=1);
namespace FireflyIII\Console\Commands\Tools;
use Carbon\Carbon;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Support\Cronjobs\AutoBudgetCronjob;
use FireflyIII\Support\Cronjobs\BillWarningCronjob;
@@ -43,6 +44,8 @@ use Psr\Container\NotFoundExceptionInterface;
*/
class Cron extends Command
{
use ShowsFriendlyMessages;
/**
* The console command description.
*
@@ -68,7 +71,7 @@ class Cron extends Command
try {
$date = new Carbon($this->option('date'));
} catch (InvalidArgumentException $e) {
$this->error(sprintf('"%s" is not a valid date', $this->option('date')));
$this->friendlyError(sprintf('"%s" is not a valid date', $this->option('date')));
}
$force = (bool)$this->option('force');
@@ -81,7 +84,7 @@ class Cron extends Command
} catch (FireflyException $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
$this->error($e->getMessage());
$this->friendlyError($e->getMessage());
}
}
@@ -93,7 +96,7 @@ class Cron extends Command
} catch (FireflyException $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
$this->error($e->getMessage());
$this->friendlyError($e->getMessage());
}
/*
@@ -104,7 +107,7 @@ class Cron extends Command
} catch (FireflyException $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
$this->error($e->getMessage());
$this->friendlyError($e->getMessage());
}
/*
@@ -115,10 +118,10 @@ class Cron extends Command
} catch (FireflyException $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
$this->error($e->getMessage());
$this->friendlyError($e->getMessage());
}
$this->info('More feedback on the cron jobs can be found in the log files.');
$this->friendlyInfo('More feedback on the cron jobs can be found in the log files.');
return 0;
}
@@ -140,13 +143,13 @@ class Cron extends Command
$autoBudget->fire();
if ($autoBudget->jobErrored) {
$this->error(sprintf('Error in "create auto budgets" cron: %s', $autoBudget->message));
$this->friendlyError(sprintf('Error in "create auto budgets" cron: %s', $autoBudget->message));
}
if ($autoBudget->jobFired) {
$this->line(sprintf('"Create auto budgets" cron fired: %s', $autoBudget->message));
$this->friendlyInfo(sprintf('"Create auto budgets" cron fired: %s', $autoBudget->message));
}
if ($autoBudget->jobSucceeded) {
$this->info(sprintf('"Create auto budgets" cron ran with success: %s', $autoBudget->message));
$this->friendlyPositive(sprintf('"Create auto budgets" cron ran with success: %s', $autoBudget->message));
}
}
@@ -170,13 +173,13 @@ class Cron extends Command
$autoBudget->fire();
if ($autoBudget->jobErrored) {
$this->error(sprintf('Error in "bill warnings" cron: %s', $autoBudget->message));
$this->friendlyError(sprintf('Error in "bill warnings" cron: %s', $autoBudget->message));
}
if ($autoBudget->jobFired) {
$this->line(sprintf('"Send bill warnings" cron fired: %s', $autoBudget->message));
$this->friendlyInfo(sprintf('"Send bill warnings" cron fired: %s', $autoBudget->message));
}
if ($autoBudget->jobSucceeded) {
$this->info(sprintf('"Send bill warnings" cron ran with success: %s', $autoBudget->message));
$this->friendlyPositive(sprintf('"Send bill warnings" cron ran with success: %s', $autoBudget->message));
}
}
@@ -196,13 +199,13 @@ class Cron extends Command
$exchangeRates->fire();
if ($exchangeRates->jobErrored) {
$this->error(sprintf('Error in "exchange rates" cron: %s', $exchangeRates->message));
$this->friendlyError(sprintf('Error in "exchange rates" cron: %s', $exchangeRates->message));
}
if ($exchangeRates->jobFired) {
$this->line(sprintf('"Exchange rates" cron fired: %s', $exchangeRates->message));
$this->friendlyInfo(sprintf('"Exchange rates" cron fired: %s', $exchangeRates->message));
}
if ($exchangeRates->jobSucceeded) {
$this->info(sprintf('"Exchange rates" cron ran with success: %s', $exchangeRates->message));
$this->friendlyPositive(sprintf('"Exchange rates" cron ran with success: %s', $exchangeRates->message));
}
}
@@ -226,13 +229,13 @@ class Cron extends Command
$recurring->fire();
if ($recurring->jobErrored) {
$this->error(sprintf('Error in "create recurring transactions" cron: %s', $recurring->message));
$this->friendlyError(sprintf('Error in "create recurring transactions" cron: %s', $recurring->message));
}
if ($recurring->jobFired) {
$this->line(sprintf('"Create recurring transactions" cron fired: %s', $recurring->message));
$this->friendlyInfo(sprintf('"Create recurring transactions" cron fired: %s', $recurring->message));
}
if ($recurring->jobSucceeded) {
$this->info(sprintf('"Create recurring transactions" cron ran with success: %s', $recurring->message));
$this->friendlyPositive(sprintf('"Create recurring transactions" cron ran with success: %s', $recurring->message));
}
}
}