2016-01-13 00:16:29 -06:00
|
|
|
<?php
|
2016-02-05 05:08:25 -06:00
|
|
|
declare(strict_types = 1);
|
2016-01-13 00:16:29 -06:00
|
|
|
/**
|
2016-01-13 01:14:14 -06:00
|
|
|
* Processor.php
|
2016-04-01 09:44:46 -05:00
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
2016-01-13 00:16:29 -06:00
|
|
|
*
|
|
|
|
* This software may be modified and distributed under the terms
|
|
|
|
* of the MIT license. See the LICENSE file for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace FireflyIII\Rules;
|
|
|
|
|
|
|
|
use FireflyIII\Models\Rule;
|
2016-01-13 01:14:14 -06:00
|
|
|
use FireflyIII\Models\RuleAction;
|
2016-01-13 00:16:29 -06:00
|
|
|
use FireflyIII\Models\RuleTrigger;
|
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2016-02-17 11:23:20 -06:00
|
|
|
use FireflyIII\Rules\Actions\ActionInterface;
|
2016-02-23 09:02:44 -06:00
|
|
|
use FireflyIII\Rules\Factory\ActionFactory;
|
2016-02-17 23:34:16 -06:00
|
|
|
use FireflyIII\Rules\Factory\TriggerFactory;
|
2016-02-23 09:02:44 -06:00
|
|
|
use FireflyIII\Rules\Triggers\AbstractTrigger;
|
2016-02-17 11:47:05 -06:00
|
|
|
use Illuminate\Support\Collection;
|
2016-01-13 00:47:26 -06:00
|
|
|
use Log;
|
2016-01-13 00:16:29 -06:00
|
|
|
|
|
|
|
/**
|
2016-01-13 01:14:14 -06:00
|
|
|
* Class Processor
|
2016-01-13 00:16:29 -06:00
|
|
|
*
|
|
|
|
* @package FireflyIII\Rules
|
|
|
|
*/
|
2016-02-17 14:55:00 -06:00
|
|
|
final class Processor
|
2016-01-13 00:16:29 -06:00
|
|
|
{
|
2016-02-17 11:47:05 -06:00
|
|
|
/** @var Collection */
|
2016-02-17 14:59:47 -06:00
|
|
|
public $actions;
|
2016-01-13 00:16:29 -06:00
|
|
|
/** @var TransactionJournal */
|
2016-02-17 14:59:47 -06:00
|
|
|
public $journal;
|
2016-01-20 08:23:36 -06:00
|
|
|
/** @var Rule */
|
2016-02-17 14:59:47 -06:00
|
|
|
public $rule;
|
2016-02-17 11:47:05 -06:00
|
|
|
/** @var Collection */
|
2016-02-17 14:59:47 -06:00
|
|
|
public $triggers;
|
2016-01-13 01:14:14 -06:00
|
|
|
|
2016-01-13 00:16:29 -06:00
|
|
|
/**
|
2016-01-13 01:14:14 -06:00
|
|
|
* Processor constructor.
|
2016-01-15 10:38:09 -06:00
|
|
|
*
|
2016-01-13 00:16:29 -06:00
|
|
|
*/
|
2016-02-17 11:47:05 -06:00
|
|
|
private function __construct()
|
2016-01-13 00:16:29 -06:00
|
|
|
{
|
2016-02-17 14:03:59 -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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-17 23:24:47 -06:00
|
|
|
* This method will make a Processor that will process each transaction journal using the triggers
|
|
|
|
* and actions found in the given Rule.
|
|
|
|
*
|
2016-02-17 11:47:05 -06:00
|
|
|
* @param Rule $rule
|
|
|
|
*
|
|
|
|
* @return Processor
|
2016-01-20 08:23:36 -06:00
|
|
|
*/
|
2016-02-17 11:47:05 -06:00
|
|
|
public static function make(Rule $rule)
|
2016-01-20 08:23:36 -06:00
|
|
|
{
|
2016-02-17 14:03:59 -06:00
|
|
|
$self = new self;
|
|
|
|
$self->rule = $rule;
|
|
|
|
|
|
|
|
$triggerSet = $rule->ruleTriggers()->orderBy('order', 'ASC')->get();
|
|
|
|
/** @var RuleTrigger $trigger */
|
|
|
|
foreach ($triggerSet as $trigger) {
|
|
|
|
$self->triggers->push(TriggerFactory::getTrigger($trigger));
|
|
|
|
}
|
|
|
|
$self->actions = $rule->ruleActions()->orderBy('order', 'ASC')->get();
|
2016-01-20 08:23:36 -06:00
|
|
|
|
2016-02-17 11:47:05 -06:00
|
|
|
return $self;
|
2016-01-20 08:23:36 -06:00
|
|
|
}
|
|
|
|
|
2016-02-17 14:03:59 -06:00
|
|
|
/**
|
2016-02-17 23:24:47 -06:00
|
|
|
* 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.
|
|
|
|
*
|
2016-02-17 14:03:59 -06:00
|
|
|
* @param string $triggerName
|
|
|
|
* @param string $triggerValue
|
|
|
|
*
|
|
|
|
* @return Processor
|
|
|
|
*/
|
|
|
|
public static function makeFromString(string $triggerName, string $triggerValue)
|
|
|
|
{
|
|
|
|
$self = new self;
|
|
|
|
$trigger = TriggerFactory::makeTriggerFromStrings($triggerName, $triggerValue, false);
|
|
|
|
$self->triggers->push($trigger);
|
|
|
|
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-17 23:24:47 -06:00
|
|
|
* 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, stopProcessing => bool], [type => xx, value => yy, stopProcessing => bool],
|
|
|
|
*
|
2016-02-17 14:03:59 -06:00
|
|
|
* @param array $triggers
|
|
|
|
*
|
|
|
|
* @return Processor
|
|
|
|
*/
|
|
|
|
public static function makeFromStringArray(array $triggers)
|
|
|
|
{
|
|
|
|
$self = new self;
|
|
|
|
foreach ($triggers as $entry) {
|
|
|
|
$trigger = TriggerFactory::makeTriggerFromStrings($entry['type'], $entry['value'], $entry['stopProcessing']);
|
|
|
|
$self->triggers->push($trigger);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
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-02-17 14:03:59 -06:00
|
|
|
|
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).
|
|
|
|
*
|
2016-02-17 11:47:05 -06:00
|
|
|
* @param TransactionJournal $journal
|
2016-02-17 14:03:59 -06:00
|
|
|
*
|
|
|
|
* @return bool
|
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-02-17 11:47:05 -06:00
|
|
|
$this->journal = $journal;
|
2016-01-13 00:16:29 -06:00
|
|
|
// get all triggers:
|
|
|
|
$triggered = $this->triggered();
|
|
|
|
if ($triggered) {
|
2016-02-17 14:03:59 -06:00
|
|
|
if ($this->actions->count() > 0) {
|
|
|
|
$this->actions();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-01-13 00:16:29 -06:00
|
|
|
}
|
|
|
|
|
2016-02-17 14:03:59 -06:00
|
|
|
return false;
|
|
|
|
|
2016-01-13 00:16:29 -06:00
|
|
|
}
|
|
|
|
|
2016-01-20 08:23:36 -06:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-02-17 11:47:05 -06:00
|
|
|
private function actions()
|
2016-01-20 08:23:36 -06:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var int $index
|
|
|
|
* @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 */
|
2016-02-17 12:56:05 -06:00
|
|
|
$actionClass = ActionFactory::getAction($action);
|
|
|
|
$actionClass->act($this->journal);
|
2016-01-20 08:23:36 -06:00
|
|
|
if ($action->stop_processing) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-13 00:16:29 -06:00
|
|
|
/**
|
2016-02-17 03:24:40 -06:00
|
|
|
* Method to check whether the current transaction would be triggered
|
|
|
|
* 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
|
|
|
*/
|
2016-02-17 11:47:05 -06:00
|
|
|
private function triggered()
|
2016-02-17 11:23:20 -06:00
|
|
|
{
|
2016-01-13 00:47:26 -06:00
|
|
|
$foundTriggers = 0;
|
|
|
|
$hitTriggers = 0;
|
2016-01-13 00:16:29 -06:00
|
|
|
/** @var RuleTrigger $trigger */
|
2016-02-17 11:47:05 -06:00
|
|
|
foreach ($this->triggers as $trigger) {
|
2016-01-13 00:47:26 -06:00
|
|
|
$foundTriggers++;
|
2016-02-17 11:23:20 -06:00
|
|
|
|
2016-02-17 14:03:59 -06:00
|
|
|
/** @var AbstractTrigger $trigger */
|
|
|
|
if ($trigger->triggered($this->journal)) {
|
2016-01-13 00:47:26 -06:00
|
|
|
$hitTriggers++;
|
2016-01-13 00:16:29 -06:00
|
|
|
}
|
2016-02-17 14:03:59 -06:00
|
|
|
if ($trigger->stopProcessing) {
|
2016-01-14 23:35:31 -06:00
|
|
|
break;
|
|
|
|
}
|
2016-01-13 00:16:29 -06:00
|
|
|
|
2016-01-13 00:47:26 -06:00
|
|
|
}
|
|
|
|
Log::debug('Total: ' . $foundTriggers . ' found triggers. ' . $hitTriggers . ' triggers were hit.');
|
2016-01-13 00:16:29 -06:00
|
|
|
|
2016-02-17 23:24:47 -06:00
|
|
|
return ($hitTriggers == $foundTriggers && $foundTriggers > 0);
|
2016-02-17 11:23:20 -06:00
|
|
|
|
2016-01-13 00:16:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-28 14:50:20 -06:00
|
|
|
}
|