mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Refactor rule processor so it's testable.
This commit is contained in:
@@ -38,7 +38,7 @@ use Log;
|
||||
/**
|
||||
* Class Processor.
|
||||
*/
|
||||
final class Processor
|
||||
class Processor
|
||||
{
|
||||
/** @var Collection Actions to exectute */
|
||||
public $actions;
|
||||
@@ -56,92 +56,12 @@ final class Processor
|
||||
/**
|
||||
* Processor constructor.
|
||||
*/
|
||||
private function __construct()
|
||||
public function __construct()
|
||||
{
|
||||
$this->triggers = new Collection;
|
||||
$this->actions = new Collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @return Processor
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
*/
|
||||
public static function make(Rule $rule, bool $includeActions = null): Processor
|
||||
{
|
||||
$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)));
|
||||
$self = new self;
|
||||
$self->rule = $rule;
|
||||
$self->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));
|
||||
$self->triggers->push(TriggerFactory::getTrigger($trigger));
|
||||
}
|
||||
if (true === $includeActions) {
|
||||
$self->actions = $rule->ruleActions()->orderBy('order', 'ASC')->get();
|
||||
}
|
||||
|
||||
return $self;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @return Processor
|
||||
*
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
*/
|
||||
public static function makeFromString(string $triggerName, string $triggerValue): Processor
|
||||
{
|
||||
Log::debug(sprintf('Processor::makeFromString("%s", "%s")', $triggerName, $triggerValue));
|
||||
$self = new self;
|
||||
$trigger = TriggerFactory::makeTriggerFromStrings($triggerName, $triggerValue, false);
|
||||
$self->triggers->push($trigger);
|
||||
|
||||
return $self;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @return Processor
|
||||
*
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
*/
|
||||
public static function makeFromStringArray(array $triggers): Processor
|
||||
{
|
||||
$self = new self;
|
||||
foreach ($triggers as $entry) {
|
||||
$entry['value'] = $entry['value'] ?? '';
|
||||
$trigger = TriggerFactory::makeTriggerFromStrings($entry['type'], $entry['value'], $entry['stop_processing']);
|
||||
$self->triggers->push($trigger);
|
||||
}
|
||||
|
||||
return $self;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return found triggers
|
||||
*
|
||||
@@ -234,6 +154,73 @@ final class Processor
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the actions
|
||||
*
|
||||
|
@@ -93,8 +93,10 @@ class TransactionMatcher
|
||||
}
|
||||
|
||||
// Variables used within the loop
|
||||
$processor = Processor::makeFromStringArray($this->triggers);
|
||||
$result = $this->runProcessor($processor);
|
||||
/** @var Processor $processor */
|
||||
$processor = app(Processor::class);
|
||||
$processor->makeFromStringArray($this->triggers);
|
||||
$result = $this->runProcessor($processor);
|
||||
|
||||
// If the list of matchingTransactions is larger than the maximum number of results
|
||||
// (e.g. if a large percentage of the transactions match), truncate the list
|
||||
|
Reference in New Issue
Block a user