firefly-iii/app/TransactionRules/Processor.php

289 lines
9.7 KiB
PHP
Raw Normal View History

2016-01-13 00:16:29 -06:00
<?php
/**
* Processor.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
2016-01-13 00:16:29 -06:00
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2016-01-13 00:16:29 -06:00
*/
declare(strict_types=1);
2017-09-13 00:49:58 -05:00
namespace FireflyIII\TransactionRules;
2016-01-13 00:16:29 -06:00
use FireflyIII\Models\Rule;
use FireflyIII\Models\RuleAction;
2016-01-13 00:16:29 -06:00
use FireflyIII\Models\RuleTrigger;
2016-11-06 09:17:22 -06:00
use FireflyIII\Models\Transaction;
2016-01-13 00:16:29 -06:00
use FireflyIII\Models\TransactionJournal;
2017-09-13 00:49:58 -05:00
use FireflyIII\TransactionRules\Actions\ActionInterface;
use FireflyIII\TransactionRules\Factory\ActionFactory;
use FireflyIII\TransactionRules\Factory\TriggerFactory;
use FireflyIII\TransactionRules\Triggers\AbstractTrigger;
use FireflyIII\TransactionRules\Triggers\UserAction;
use Illuminate\Support\Collection;
use Log;
2016-01-13 00:16:29 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class Processor.
2016-01-13 00:16:29 -06:00
*/
class Processor
2016-01-13 00:16:29 -06:00
{
2017-11-25 08:20:53 -06:00
/** @var Collection Actions to exectute */
2016-02-17 14:59:47 -06:00
public $actions;
2017-11-25 08:20:53 -06:00
/** @var TransactionJournal Journal to run them on */
2016-02-17 14:59:47 -06:00
public $journal;
2017-11-25 08:20:53 -06:00
/** @var Rule Rule that applies */
2016-02-17 14:59:47 -06:00
public $rule;
2017-12-22 11:32:43 -06:00
/** @var Collection All triggers */
2016-02-17 14:59:47 -06:00
public $triggers;
2017-11-25 08:20:53 -06:00
/** @var int Found triggers */
2017-11-15 05:25:49 -06:00
private $foundTriggers = 0;
2018-04-14 13:31:31 -05:00
/** @var bool */
private $strict = true;
2016-01-13 00:16:29 -06:00
/**
* Processor constructor.
2016-01-13 00:16:29 -06:00
*/
public function __construct()
2016-01-13 00:16:29 -06:00
{
$this->triggers = new Collection;
2016-02-17 14:59:47 -06:00
$this->actions = new Collection;
2016-01-20 08:23:36 -06:00
}
/**
2017-11-25 08:20:53 -06:00
* Return found triggers
*
* @return int
*/
public function getFoundTriggers(): int
{
return $this->foundTriggers;
}
/**
2017-11-25 08:20:53 -06:00
* Set found triggers
*
* @param int $foundTriggers
*/
2018-07-28 03:45:16 -05:00
public function setFoundTriggers(int $foundTriggers): void
{
$this->foundTriggers = $foundTriggers;
}
2016-02-23 09:02:44 -06:00
/**
2017-11-25 08:20:53 -06:00
* Returns the rule
*
2016-02-23 09:02:44 -06:00
* @return \FireflyIII\Models\Rule
*/
2016-04-05 15:00:03 -05:00
public function getRule(): Rule
2016-02-23 09:02:44 -06:00
{
return $this->rule;
}
2016-11-06 09:17:22 -06:00
/**
* 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 Transaction $transaction
*
* @return bool
2018-03-25 02:01:43 -05:00
* @throws \FireflyIII\Exceptions\FireflyException
2016-11-06 09:17:22 -06:00
*/
public function handleTransaction(Transaction $transaction): bool
{
Log::debug(sprintf('handleTransactionJournal for journal #%d (transaction #%d)', $transaction->transaction_journal_id, $transaction->id));
// grab the actual journal.
$journal = $transaction->transactionJournal()->first();
$this->journal = $journal;
// get all triggers:
$triggered = $this->triggered();
if ($triggered) {
2017-09-25 02:28:16 -05:00
Log::debug('Rule is triggered, go to actions.');
2016-11-06 09:17:22 -06:00
if ($this->actions->count() > 0) {
2017-09-25 02:28:16 -05:00
Log::debug('Has more than zero actions.');
2016-11-06 09:17:22 -06:00
$this->actions();
}
2017-11-15 05:25:49 -06:00
if (0 === $this->actions->count()) {
2017-09-25 02:28:16 -05:00
Log::info('Rule has no actions!');
}
2016-11-06 09:17:22 -06:00
return true;
}
return false;
}
2016-01-20 08:23:36 -06:00
/**
2016-02-17 23:24:47 -06:00
* 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
2018-03-25 02:01:43 -05:00
* @throws \FireflyIII\Exceptions\FireflyException
2016-01-20 08:23:36 -06:00
*/
2016-04-05 15:00:03 -05:00
public function handleTransactionJournal(TransactionJournal $journal): bool
2016-01-13 00:16:29 -06:00
{
2016-09-21 14:01:10 -05:00
Log::debug(sprintf('handleTransactionJournal for journal %d', $journal->id));
$this->journal = $journal;
2016-01-13 00:16:29 -06:00
// get all triggers:
$triggered = $this->triggered();
if ($triggered) {
if ($this->actions->count() > 0) {
$this->actions();
}
return true;
2016-01-13 00:16:29 -06:00
}
return false;
2016-01-13 00:16:29 -06:00
}
/**
* 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);
}
}
2016-01-20 08:23:36 -06:00
/**
2017-11-25 08:20:53 -06:00
* Run the actions
*
2018-07-28 03:45:16 -05:00
* @return void
2018-03-11 10:24:07 -05:00
* @throws \FireflyIII\Exceptions\FireflyException
2016-01-20 08:23:36 -06:00
*/
2018-07-28 03:45:16 -05:00
private function actions(): void
2016-01-20 08:23:36 -06:00
{
/**
2017-11-15 05:25:49 -06:00
* @var int
2016-01-20 08:23:36 -06:00
* @var RuleAction $action
*/
2016-02-17 12:08:27 -06:00
foreach ($this->actions as $action) {
2016-01-20 08:23:36 -06:00
/** @var ActionInterface $actionClass */
$actionClass = ActionFactory::getAction($action);
2018-04-27 23:23:13 -05:00
Log::debug(sprintf('Fire action %s on journal #%d', \get_class($actionClass), $this->journal->id));
$actionClass->act($this->journal);
2016-01-20 08:23:36 -06:00
if ($action->stop_processing) {
2016-09-21 14:01:10 -05:00
Log::debug('Stop processing now and break.');
2016-01-20 08:23:36 -06:00
break;
}
}
}
2016-01-13 00:16:29 -06:00
/**
* Method to check whether the current transaction would be triggered
2017-11-15 05:25:49 -06:00
* by the given list of triggers.
2016-02-17 11:23:20 -06:00
*
2016-01-13 00:16:29 -06:00
* @return bool
2016-02-17 11:23:20 -06:00
*/
private function triggered(): bool
2016-02-17 11:23:20 -06:00
{
Log::debug('start of Processor::triggered()');
$foundTriggers = $this->getFoundTriggers();
2016-01-13 00:47:26 -06:00
$hitTriggers = 0;
2016-09-24 02:12:17 -05:00
Log::debug(sprintf('Found triggers starts at %d', $foundTriggers));
/** @var AbstractTrigger $trigger */
foreach ($this->triggers as $trigger) {
2017-11-15 05:25:49 -06:00
++$foundTriggers;
2018-04-27 23:23:13 -05:00
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!');
2017-11-15 05:25:49 -06:00
++$hitTriggers;
2018-04-14 13:31:31 -05:00
// is non-strict? then return true!
if (!$this->strict && UserAction::class !== \get_class($trigger)) {
2018-04-14 13:31:31 -05:00
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.');
}
2016-01-13 00:16:29 -06:00
}
if ($trigger->stopProcessing) {
Log::debug('Stop processing this trigger and break.');
2016-01-14 23:35:31 -06:00
break;
}
2016-01-13 00:47:26 -06:00
}
2017-07-15 09:41:07 -05:00
$result = ($hitTriggers === $foundTriggers && $foundTriggers > 0);
Log::debug('Result of triggered()', ['hitTriggers' => $hitTriggers, 'foundTriggers' => $foundTriggers, 'result' => $result]);
2016-01-13 00:16:29 -06:00
return $result;
2016-01-13 00:16:29 -06:00
}
}