firefly-iii/app/Rules/Processor.php

151 lines
3.7 KiB
PHP
Raw Normal View History

2016-01-13 00:16:29 -06:00
<?php
/**
* 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;
use FireflyIII\Models\RuleAction;
2016-01-13 00:16:29 -06:00
use FireflyIII\Models\RuleTrigger;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Rules\Actions\ActionInterface;
2016-01-13 00:47:26 -06:00
use FireflyIII\Rules\Triggers\TriggerInterface;
2016-01-13 00:16:29 -06:00
use FireflyIII\Support\Domain;
2016-01-13 00:47:26 -06:00
use Log;
2016-01-13 00:16:29 -06:00
/**
* Class Processor
2016-01-13 00:16:29 -06:00
*
* @package FireflyIII\Rules
*/
class Processor
2016-01-13 00:16:29 -06:00
{
/** @var Rule */
protected $rule;
/** @var TransactionJournal */
protected $journal;
/** @var array */
2016-01-13 00:16:29 -06:00
private $triggerTypes = [];
/** @var array */
private $actionTypes = [];
2016-01-13 00:16:29 -06:00
/**
* Processor constructor.
2016-01-13 00:16:29 -06:00
*/
public function __construct(Rule $rule, TransactionJournal $journal)
{
$this->rule = $rule;
$this->journal = $journal;
$this->triggerTypes = Domain::getRuleTriggers();
$this->actionTypes = Domain::getRuleActions();
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.');
$this->actions();
2016-01-13 00:16:29 -06:00
}
}
/**
2016-01-13 00:47:26 -06:00
* TODO stop when stop_processing is present.
*
2016-01-13 00:16:29 -06:00
* @return bool
*/
protected function triggered()
{
2016-01-13 00:47:26 -06:00
$foundTriggers = 0;
$hitTriggers = 0;
2016-01-13 00:16:29 -06:00
/** @var RuleTrigger $trigger */
2016-01-13 00:47:26 -06:00
foreach ($this->rule->ruleTriggers()->orderBy('order', 'ASC')->get() as $index => $trigger) {
$foundTriggers++;
2016-01-13 00:16:29 -06:00
$type = $trigger->trigger_type;
$class = $this->triggerTypes[$type];
2016-01-13 00:47:26 -06:00
Log::debug('Trigger #' . $trigger->id . ' for rule #' . $trigger->rule_id . ' (' . $type . ')');
2016-01-13 00:16:29 -06:00
if (!class_exists($class)) {
2016-01-13 00:47:26 -06:00
abort(500, 'Could not instantiate class for rule trigger type "' . $type . '" (' . $class . ').');
}
/** @var TriggerInterface $triggerClass */
$triggerClass = new $class($trigger, $this->journal);
if ($triggerClass->triggered()) {
$hitTriggers++;
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-01-13 00:16:29 -06:00
}
/**
* @return bool
*/
protected function actions()
{
/**
* @var int $index
* @var RuleAction $action
*/
foreach ($this->rule->ruleActions()->orderBy('order', 'ASC')->get() as $index => $action) {
$type = $action->action_type;
$class = $this->actionTypes[$type];
Log::debug('Action #' . $action->id . ' for rule #' . $action->rule_id . ' (' . $type . ')');
if (!class_exists($class)) {
abort(500, 'Could not instantiate class for rule action type "' . $type . '" (' . $class . ').');
}
/** @var ActionInterface $actionClass */
$actionClass = new $class($action, $this->journal);
$actionClass->act();
}
return true;
}
2016-01-13 00:16:29 -06:00
/**
* @return Rule
*/
public function getRule()
{
return $this->rule;
}
/**
* @param Rule $rule
*/
public function setRule($rule)
{
$this->rule = $rule;
}
/**
* @return TransactionJournal
*/
public function getJournal()
{
return $this->journal;
}
/**
* @param TransactionJournal $journal
*/
public function setJournal($journal)
{
$this->journal = $journal;
}
}