mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-05-04 21:23:36 +00:00
Code cleanup
This commit is contained in:
@@ -22,6 +22,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules;
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\Rule;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\RuleTrigger;
|
||||
@@ -62,16 +63,71 @@ class Processor
|
||||
$this->actions = new Collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return found triggers
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getFoundTriggers(): int
|
||||
{
|
||||
return $this->foundTriggers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set found triggers
|
||||
*
|
||||
* @param int $foundTriggers
|
||||
*/
|
||||
public function setFoundTriggers(int $foundTriggers): void
|
||||
{
|
||||
$this->foundTriggers = $foundTriggers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the rule
|
||||
*
|
||||
* @return \FireflyIII\Models\Rule
|
||||
* @return Rule
|
||||
*/
|
||||
public function getRule(): Rule
|
||||
{
|
||||
return $this->rule;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will scan the given transaction journal and check if it matches the triggers found in the Processor
|
||||
* If so, it will also attempt to run the given actions on the journal. It returns a bool indicating if the transaction journal
|
||||
* matches all of the triggers (regardless of whether the Processor could act on it).
|
||||
*
|
||||
* @param array $journal
|
||||
*
|
||||
* @return bool
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function handleJournalArray(array $journal): bool
|
||||
{
|
||||
|
||||
Log::debug(sprintf('handleJournalArray for journal #%d (group #%d)', $journal['transaction_journal_id'], $journal['transaction_group_id']));
|
||||
|
||||
// grab the actual journal.
|
||||
$this->journal = TransactionJournal::find($journal['transaction_journal_id']);
|
||||
// get all triggers:
|
||||
$triggered = $this->triggered();
|
||||
if ($triggered) {
|
||||
Log::debug('Rule is triggered, go to actions.');
|
||||
if ($this->actions->count() > 0) {
|
||||
Log::debug('Has more than zero actions.');
|
||||
$this->actions();
|
||||
}
|
||||
if (0 === $this->actions->count()) {
|
||||
Log::info('Rule has no actions!');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will scan the given transaction journal and check if it matches the triggers found in the Processor
|
||||
* If so, it will also attempt to run the given actions on the journal. It returns a bool indicating if the transaction journal
|
||||
@@ -80,7 +136,7 @@ class Processor
|
||||
* @param Transaction $transaction
|
||||
*
|
||||
* @return bool
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function handleTransaction(Transaction $transaction): bool
|
||||
{
|
||||
@@ -107,6 +163,140 @@ class Processor
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will scan the given transaction journal and check if it matches the triggers found in the Processor
|
||||
* If so, it will also attempt to run the given actions on the journal. It returns a bool indicating if the transaction journal
|
||||
* matches all of the triggers (regardless of whether the Processor could act on it).
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return bool
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function handleTransactionJournal(TransactionJournal $journal): bool
|
||||
{
|
||||
Log::debug(sprintf('handleTransactionJournal for journal %d', $journal->id));
|
||||
$this->journal = $journal;
|
||||
// get all triggers:
|
||||
$triggered = $this->triggered();
|
||||
if ($triggered) {
|
||||
if ($this->actions->count() > 0) {
|
||||
$this->actions();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isStrict(): bool
|
||||
{
|
||||
return $this->strict;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $strict
|
||||
*/
|
||||
public function setStrict(bool $strict): void
|
||||
{
|
||||
$this->strict = $strict;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will make a Processor that will process each transaction journal using the triggers
|
||||
* and actions found in the given Rule.
|
||||
*
|
||||
* @param Rule $rule
|
||||
* @param bool $includeActions
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function make(Rule $rule, bool $includeActions = null): void
|
||||
{
|
||||
$includeActions = $includeActions ?? true;
|
||||
Log::debug(sprintf('Making new rule from Rule %d', $rule->id));
|
||||
Log::debug(sprintf('Rule is strict: %s', var_export($rule->strict, true)));
|
||||
$this->rule = $rule;
|
||||
$this->strict = $rule->strict;
|
||||
$triggerSet = $rule->ruleTriggers()->orderBy('order', 'ASC')->get();
|
||||
/** @var RuleTrigger $trigger */
|
||||
foreach ($triggerSet as $trigger) {
|
||||
Log::debug(sprintf('Push trigger %d', $trigger->id));
|
||||
$this->triggers->push(TriggerFactory::getTrigger($trigger));
|
||||
}
|
||||
if (true === $includeActions) {
|
||||
$this->actions = $rule->ruleActions()->orderBy('order', 'ASC')->get();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will make a Processor that will process each transaction journal using the given
|
||||
* trigger (singular!). It can only report if the transaction journal was hit by the given trigger
|
||||
* and will not be able to act on it using actions.
|
||||
*
|
||||
* @param string $triggerName
|
||||
* @param string $triggerValue
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function makeFromString(string $triggerName, string $triggerValue): void
|
||||
{
|
||||
Log::debug(sprintf('Processor::makeFromString("%s", "%s")', $triggerName, $triggerValue));
|
||||
$trigger = TriggerFactory::makeTriggerFromStrings($triggerName, $triggerValue, false);
|
||||
$this->triggers->push($trigger);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will make a Processor that will process each transaction journal using the given
|
||||
* triggers. It can only report if the transaction journal was hit by the given triggers
|
||||
* and will not be able to act on it using actions.
|
||||
*
|
||||
* The given triggers must be in the following format:
|
||||
*
|
||||
* [type => xx, value => yy, stop_processing => bool], [type => xx, value => yy, stop_processing => bool],
|
||||
*
|
||||
* @param array $triggers
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function makeFromStringArray(array $triggers): void
|
||||
{
|
||||
foreach ($triggers as $entry) {
|
||||
$entry['value'] = $entry['value'] ?? '';
|
||||
$trigger = TriggerFactory::makeTriggerFromStrings($entry['type'], $entry['value'], $entry['stop_processing']);
|
||||
$this->triggers->push($trigger);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the actions
|
||||
*
|
||||
* @return void
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function actions(): void
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
* @var RuleAction $action
|
||||
*/
|
||||
foreach ($this->actions as $action) {
|
||||
/** @var ActionInterface $actionClass */
|
||||
$actionClass = ActionFactory::getAction($action);
|
||||
Log::debug(sprintf('Fire action %s on journal #%d', get_class($actionClass), $this->journal->id));
|
||||
$actionClass->act($this->journal);
|
||||
if ($action->stop_processing) {
|
||||
Log::debug('Stop processing now and break.');
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to check whether the current transaction would be triggered
|
||||
* by the given list of triggers.
|
||||
@@ -147,193 +337,4 @@ class Processor
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return found triggers
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getFoundTriggers(): int
|
||||
{
|
||||
return $this->foundTriggers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set found triggers
|
||||
*
|
||||
* @param int $foundTriggers
|
||||
*/
|
||||
public function setFoundTriggers(int $foundTriggers): void
|
||||
{
|
||||
$this->foundTriggers = $foundTriggers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the actions
|
||||
*
|
||||
* @return void
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
*/
|
||||
private function actions(): void
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
* @var RuleAction $action
|
||||
*/
|
||||
foreach ($this->actions as $action) {
|
||||
/** @var ActionInterface $actionClass */
|
||||
$actionClass = ActionFactory::getAction($action);
|
||||
Log::debug(sprintf('Fire action %s on journal #%d', get_class($actionClass), $this->journal->id));
|
||||
$actionClass->act($this->journal);
|
||||
if ($action->stop_processing) {
|
||||
Log::debug('Stop processing now and break.');
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will scan the given transaction journal and check if it matches the triggers found in the Processor
|
||||
* If so, it will also attempt to run the given actions on the journal. It returns a bool indicating if the transaction journal
|
||||
* matches all of the triggers (regardless of whether the Processor could act on it).
|
||||
*
|
||||
* @param array $journal
|
||||
*
|
||||
* @return bool
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
*/
|
||||
public function handleJournalArray(array $journal): bool
|
||||
{
|
||||
|
||||
Log::debug(sprintf('handleJournalArray for journal #%d (group #%d)', $journal['transaction_journal_id'], $journal['transaction_group_id']));
|
||||
|
||||
// grab the actual journal.
|
||||
$this->journal = TransactionJournal::find($journal['transaction_journal_id']);
|
||||
// get all triggers:
|
||||
$triggered = $this->triggered();
|
||||
if ($triggered) {
|
||||
Log::debug('Rule is triggered, go to actions.');
|
||||
if ($this->actions->count() > 0) {
|
||||
Log::debug('Has more than zero actions.');
|
||||
$this->actions();
|
||||
}
|
||||
if (0 === $this->actions->count()) {
|
||||
Log::info('Rule has no actions!');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will scan the given transaction journal and check if it matches the triggers found in the Processor
|
||||
* If so, it will also attempt to run the given actions on the journal. It returns a bool indicating if the transaction journal
|
||||
* matches all of the triggers (regardless of whether the Processor could act on it).
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return bool
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
*/
|
||||
public function handleTransactionJournal(TransactionJournal $journal): bool
|
||||
{
|
||||
Log::debug(sprintf('handleTransactionJournal for journal %d', $journal->id));
|
||||
$this->journal = $journal;
|
||||
// get all triggers:
|
||||
$triggered = $this->triggered();
|
||||
if ($triggered) {
|
||||
if ($this->actions->count() > 0) {
|
||||
$this->actions();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isStrict(): bool
|
||||
{
|
||||
return $this->strict;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $strict
|
||||
*/
|
||||
public function setStrict(bool $strict): void
|
||||
{
|
||||
$this->strict = $strict;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will make a Processor that will process each transaction journal using the triggers
|
||||
* and actions found in the given Rule.
|
||||
*
|
||||
* @param Rule $rule
|
||||
* @param bool $includeActions
|
||||
*
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
*/
|
||||
public function make(Rule $rule, bool $includeActions = null): void
|
||||
{
|
||||
$includeActions = $includeActions ?? true;
|
||||
Log::debug(sprintf('Making new rule from Rule %d', $rule->id));
|
||||
Log::debug(sprintf('Rule is strict: %s', var_export($rule->strict, true)));
|
||||
$this->rule = $rule;
|
||||
$this->strict = $rule->strict;
|
||||
$triggerSet = $rule->ruleTriggers()->orderBy('order', 'ASC')->get();
|
||||
/** @var RuleTrigger $trigger */
|
||||
foreach ($triggerSet as $trigger) {
|
||||
Log::debug(sprintf('Push trigger %d', $trigger->id));
|
||||
$this->triggers->push(TriggerFactory::getTrigger($trigger));
|
||||
}
|
||||
if (true === $includeActions) {
|
||||
$this->actions = $rule->ruleActions()->orderBy('order', 'ASC')->get();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will make a Processor that will process each transaction journal using the given
|
||||
* trigger (singular!). It can only report if the transaction journal was hit by the given trigger
|
||||
* and will not be able to act on it using actions.
|
||||
*
|
||||
* @param string $triggerName
|
||||
* @param string $triggerValue
|
||||
*
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
*/
|
||||
public function makeFromString(string $triggerName, string $triggerValue): void
|
||||
{
|
||||
Log::debug(sprintf('Processor::makeFromString("%s", "%s")', $triggerName, $triggerValue));
|
||||
$trigger = TriggerFactory::makeTriggerFromStrings($triggerName, $triggerValue, false);
|
||||
$this->triggers->push($trigger);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will make a Processor that will process each transaction journal using the given
|
||||
* triggers. It can only report if the transaction journal was hit by the given triggers
|
||||
* and will not be able to act on it using actions.
|
||||
*
|
||||
* The given triggers must be in the following format:
|
||||
*
|
||||
* [type => xx, value => yy, stop_processing => bool], [type => xx, value => yy, stop_processing => bool],
|
||||
*
|
||||
* @param array $triggers
|
||||
*
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
*/
|
||||
public function makeFromStringArray(array $triggers): void
|
||||
{
|
||||
foreach ($triggers as $entry) {
|
||||
$entry['value'] = $entry['value'] ?? '';
|
||||
$trigger = TriggerFactory::makeTriggerFromStrings($entry['type'], $entry['value'], $entry['stop_processing']);
|
||||
$this->triggers->push($trigger);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user