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-01-13 00:16:29 -06:00
|
|
|
* Copyright (C) 2016 Sander Dorigo
|
|
|
|
*
|
|
|
|
* 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-01-13 01:14:14 -06:00
|
|
|
use FireflyIII\Rules\Actions\ActionInterface;
|
2016-02-17 09:30:54 -06:00
|
|
|
use FireflyIII\Rules\Actions\ActionFactory;
|
2016-01-13 00:47:26 -06:00
|
|
|
use FireflyIII\Rules\Triggers\TriggerInterface;
|
2016-02-17 06:13:23 -06:00
|
|
|
use FireflyIII\Rules\Triggers\TriggerFactory;
|
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-01-13 01:14:14 -06:00
|
|
|
class Processor
|
2016-01-13 00:16:29 -06:00
|
|
|
{
|
|
|
|
/** @var TransactionJournal */
|
|
|
|
protected $journal;
|
2016-01-20 08:23:36 -06:00
|
|
|
/** @var Rule */
|
|
|
|
protected $rule;
|
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
|
|
|
*
|
|
|
|
* @param Rule $rule
|
|
|
|
* @param TransactionJournal $journal
|
2016-01-13 00:16:29 -06:00
|
|
|
*/
|
|
|
|
public function __construct(Rule $rule, TransactionJournal $journal)
|
|
|
|
{
|
|
|
|
$this->rule = $rule;
|
|
|
|
$this->journal = $journal;
|
|
|
|
}
|
|
|
|
|
2016-01-20 08:23:36 -06:00
|
|
|
/**
|
|
|
|
* @return TransactionJournal
|
|
|
|
*/
|
|
|
|
public function getJournal()
|
|
|
|
{
|
|
|
|
return $this->journal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*/
|
|
|
|
public function setJournal($journal)
|
|
|
|
{
|
|
|
|
$this->journal = $journal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Rule
|
|
|
|
*/
|
|
|
|
public function getRule()
|
|
|
|
{
|
|
|
|
return $this->rule;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Rule $rule
|
|
|
|
*/
|
|
|
|
public function setRule($rule)
|
|
|
|
{
|
|
|
|
$this->rule = $rule;
|
|
|
|
}
|
|
|
|
|
2016-01-13 00:16:29 -06:00
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
// get all triggers:
|
|
|
|
$triggered = $this->triggered();
|
|
|
|
if ($triggered) {
|
2016-01-13 00:47:26 -06:00
|
|
|
Log::debug('Rule #' . $this->rule->id . ' was triggered. Now process each action.');
|
2016-01-13 01:14:14 -06:00
|
|
|
$this->actions();
|
2016-01-13 00:16:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-17 03:24:40 -06:00
|
|
|
/**
|
|
|
|
* Checks whether the current transaction is triggered by the current rule
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isTriggered() {
|
|
|
|
return $this->triggered();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the current transaction is triggered by the list of given triggers
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isTriggeredBy(array $triggers) {
|
|
|
|
return $this->triggeredBy($triggers);
|
|
|
|
}
|
|
|
|
|
2016-01-20 08:23:36 -06:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function actions()
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var int $index
|
|
|
|
* @var RuleAction $action
|
|
|
|
*/
|
|
|
|
foreach ($this->rule->ruleActions()->orderBy('order', 'ASC')->get() as $action) {
|
|
|
|
/** @var ActionInterface $actionClass */
|
2016-02-17 06:24:56 -06:00
|
|
|
$actionClass = ActionFactory::getAction($action, $this->journal);
|
2016-01-20 08:23:36 -06:00
|
|
|
$actionClass->act();
|
|
|
|
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-01-13 00:16:29 -06:00
|
|
|
* @return bool
|
2016-02-17 03:24:40 -06:00
|
|
|
*/
|
|
|
|
protected function triggeredBy($triggers) {
|
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 03:24:40 -06:00
|
|
|
foreach ($triggers as $trigger) {
|
2016-01-13 00:47:26 -06:00
|
|
|
$foundTriggers++;
|
2016-02-17 06:13:23 -06:00
|
|
|
|
2016-01-13 00:47:26 -06:00
|
|
|
/** @var TriggerInterface $triggerClass */
|
2016-02-17 06:13:23 -06:00
|
|
|
$triggerClass = TriggerFactory::getTrigger($trigger, $this->journal);
|
2016-01-13 00:47:26 -06:00
|
|
|
if ($triggerClass->triggered()) {
|
|
|
|
$hitTriggers++;
|
2016-01-13 00:16:29 -06:00
|
|
|
}
|
2016-01-14 23:35:31 -06:00
|
|
|
if ($trigger->stop_processing) {
|
|
|
|
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-01-13 00:47:26 -06:00
|
|
|
return ($hitTriggers == $foundTriggers);
|
2016-02-17 03:24:40 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Checks whether the current transaction is triggered by the current rule
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function triggered()
|
|
|
|
{
|
|
|
|
return $this->triggeredBy($this->rule->ruleTriggers()->orderBy('order', 'ASC')->get());
|
2016-01-13 00:16:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-28 14:50:20 -06:00
|
|
|
}
|