Replace transaction collector.

This commit is contained in:
James Cole
2019-05-29 18:28:28 +02:00
parent 627ef09f11
commit d13317095f
10 changed files with 648 additions and 220 deletions

View File

@@ -62,26 +62,6 @@ 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
*
@@ -127,6 +107,126 @@ class Processor
return false;
}
/**
* Method to check whether the current transaction would be triggered
* by the given list of triggers.
*
* @return bool
*/
private function triggered(): bool
{
Log::debug('start of Processor::triggered()');
$foundTriggers = $this->getFoundTriggers();
$hitTriggers = 0;
Log::debug(sprintf('Found triggers starts at %d', $foundTriggers));
/** @var AbstractTrigger $trigger */
foreach ($this->triggers as $trigger) {
++$foundTriggers;
Log::debug(sprintf('Now checking trigger %s with value %s', \get_class($trigger), $trigger->getTriggerValue()));
/** @var AbstractTrigger $trigger */
if ($trigger->triggered($this->journal)) {
Log::debug('Is a match!');
++$hitTriggers;
// is non-strict? then return true!
if (!$this->strict && UserAction::class !== \get_class($trigger)) {
Log::debug('Rule is set as non-strict, return true!');
return true;
}
if (!$this->strict && UserAction::class === \get_class($trigger)) {
Log::debug('Rule is set as non-strict, but action was "user-action". Will not return true.');
}
}
if ($trigger->stopProcessing) {
Log::debug('Stop processing this trigger and break.');
break;
}
}
$result = ($hitTriggers === $foundTriggers && $foundTriggers > 0);
Log::debug('Result of triggered()', ['hitTriggers' => $hitTriggers, 'foundTriggers' => $foundTriggers, 'result' => $result]);
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
@@ -236,69 +336,4 @@ class Processor
}
}
/**
* 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;
}
}
}
/**
* Method to check whether the current transaction would be triggered
* by the given list of triggers.
*
* @return bool
*/
private function triggered(): bool
{
Log::debug('start of Processor::triggered()');
$foundTriggers = $this->getFoundTriggers();
$hitTriggers = 0;
Log::debug(sprintf('Found triggers starts at %d', $foundTriggers));
/** @var AbstractTrigger $trigger */
foreach ($this->triggers as $trigger) {
++$foundTriggers;
Log::debug(sprintf('Now checking trigger %s with value %s', \get_class($trigger), $trigger->getTriggerValue()));
/** @var AbstractTrigger $trigger */
if ($trigger->triggered($this->journal)) {
Log::debug('Is a match!');
++$hitTriggers;
// is non-strict? then return true!
if (!$this->strict && UserAction::class !== \get_class($trigger)) {
Log::debug('Rule is set as non-strict, return true!');
return true;
}
if (!$this->strict && UserAction::class === \get_class($trigger)) {
Log::debug('Rule is set as non-strict, but action was "user-action". Will not return true.');
}
}
if ($trigger->stopProcessing) {
Log::debug('Stop processing this trigger and break.');
break;
}
}
$result = ($hitTriggers === $foundTriggers && $foundTriggers > 0);
Log::debug('Result of triggered()', ['hitTriggers' => $hitTriggers, 'foundTriggers' => $foundTriggers, 'result' => $result]);
return $result;
}
}