mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-12-26 08:51:12 -06:00
Merge pull request #190 from roberthorlings/feature/trigger-action-factories
Introduction of factories to create rule actions and rule triggers
This commit is contained in:
commit
aa25ac774e
69
app/Rules/Actions/ActionFactory.php
Normal file
69
app/Rules/Actions/ActionFactory.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
declare(strict_types = 1);
|
||||
/**
|
||||
* ActionFactory.php
|
||||
* Copyright (C) 2016 Robert Horlings
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace FireflyIII\Rules\Actions;
|
||||
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Support\Domain;
|
||||
|
||||
/**
|
||||
* Interface ActionInterface
|
||||
*
|
||||
* @package FireflyIII\Rules\Actions
|
||||
*/
|
||||
class ActionFactory
|
||||
{
|
||||
protected static $actionTypes = null;
|
||||
|
||||
/**
|
||||
* Returns the class name to be used for actions with the given name
|
||||
* @param string $actionType
|
||||
* @return ActionInterface
|
||||
*/
|
||||
public static function getActionClass(string $actionType): string {
|
||||
$actionTypes = self::getActionTypes();
|
||||
|
||||
if (!array_key_exists($actionType, $actionTypes)) {
|
||||
abort(500, 'No such action exists ("' . $actionType . '").');
|
||||
}
|
||||
|
||||
$class = $actionTypes[$actionType];
|
||||
if (!class_exists($class)) {
|
||||
abort(500, 'Could not instantiate class for rule action type "' . $actionType . '" (' . $class . ').');
|
||||
}
|
||||
|
||||
return $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the action for the given type and journal
|
||||
* @param RuleAction $action
|
||||
* @param TransactionJournal $journal
|
||||
* @return ActionInterface
|
||||
*/
|
||||
public static function getAction(RuleAction $action, TransactionJournal $journal): ActionInterface {
|
||||
$actionType = $action->action_type;
|
||||
$class = self::getActionClass($actionType);
|
||||
|
||||
return new $class($action, $journal);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a map with actiontypes, mapped to the class representing that type
|
||||
*/
|
||||
protected static function getActionTypes() {
|
||||
if( !self::$actionTypes ) {
|
||||
self::$actionTypes = Domain::getRuleActions();
|
||||
}
|
||||
|
||||
return self::$actionTypes;
|
||||
}
|
||||
}
|
@ -15,8 +15,9 @@ use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\RuleTrigger;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Rules\Actions\ActionInterface;
|
||||
use FireflyIII\Rules\Actions\ActionFactory;
|
||||
use FireflyIII\Rules\Triggers\TriggerInterface;
|
||||
use FireflyIII\Support\Domain;
|
||||
use FireflyIII\Rules\Triggers\TriggerFactory;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
@ -30,10 +31,6 @@ class Processor
|
||||
protected $journal;
|
||||
/** @var Rule */
|
||||
protected $rule;
|
||||
/** @var array */
|
||||
private $actionTypes = [];
|
||||
/** @var array */
|
||||
private $triggerTypes = [];
|
||||
|
||||
/**
|
||||
* Processor constructor.
|
||||
@ -45,8 +42,6 @@ class Processor
|
||||
{
|
||||
$this->rule = $rule;
|
||||
$this->journal = $journal;
|
||||
$this->triggerTypes = Domain::getRuleTriggers();
|
||||
$this->actionTypes = Domain::getRuleActions();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -102,14 +97,8 @@ class Processor
|
||||
* @var RuleAction $action
|
||||
*/
|
||||
foreach ($this->rule->ruleActions()->orderBy('order', 'ASC')->get() as $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 = ActionFactory::getAction($action, $this->journal);
|
||||
$actionClass->act();
|
||||
if ($action->stop_processing) {
|
||||
break;
|
||||
@ -130,19 +119,9 @@ class Processor
|
||||
/** @var RuleTrigger $trigger */
|
||||
foreach ($this->rule->ruleTriggers()->orderBy('order', 'ASC')->get() as $trigger) {
|
||||
$foundTriggers++;
|
||||
$type = $trigger->trigger_type;
|
||||
|
||||
if (!isset($this->triggerTypes[$type])) {
|
||||
abort(500, 'No such trigger exists ("' . $type . '").');
|
||||
}
|
||||
|
||||
$class = $this->triggerTypes[$type];
|
||||
Log::debug('Trigger #' . $trigger->id . ' for rule #' . $trigger->rule_id . ' (' . $type . ')');
|
||||
if (!class_exists($class)) {
|
||||
abort(500, 'Could not instantiate class for rule trigger type "' . $type . '" (' . $class . ').');
|
||||
}
|
||||
|
||||
/** @var TriggerInterface $triggerClass */
|
||||
$triggerClass = new $class($trigger, $this->journal);
|
||||
$triggerClass = TriggerFactory::getTrigger($trigger, $this->journal);
|
||||
if ($triggerClass->triggered()) {
|
||||
$hitTriggers++;
|
||||
}
|
||||
|
69
app/Rules/Triggers/TriggerFactory.php
Normal file
69
app/Rules/Triggers/TriggerFactory.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
declare(strict_types = 1);
|
||||
/**
|
||||
* TriggerFactory.php
|
||||
* Copyright (C) 2016 Robert Horlings
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace FireflyIII\Rules\Triggers;
|
||||
|
||||
use FireflyIII\Models\RuleTrigger;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Support\Domain;
|
||||
|
||||
/**
|
||||
* Interface TriggerInterface
|
||||
*
|
||||
* @package FireflyIII\Rules\Triggers
|
||||
*/
|
||||
class TriggerFactory
|
||||
{
|
||||
protected static $triggerTypes = null;
|
||||
|
||||
/**
|
||||
* Returns the class name to be used for triggers with the given name
|
||||
* @param string $triggerType
|
||||
* @return TriggerInterface
|
||||
*/
|
||||
public static function getTriggerClass(string $triggerType): string {
|
||||
$triggerTypes = self::getTriggerTypes();
|
||||
|
||||
if (!array_key_exists($triggerType, $triggerTypes)) {
|
||||
abort(500, 'No such trigger exists ("' . $triggerType . '").');
|
||||
}
|
||||
|
||||
$class = $triggerTypes[$triggerType];
|
||||
if (!class_exists($class)) {
|
||||
abort(500, 'Could not instantiate class for rule trigger type "' . $triggerType . '" (' . $class . ').');
|
||||
}
|
||||
|
||||
return $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the trigger for the given type and journal
|
||||
* @param RuleTrigger $trigger
|
||||
* @param TransactionJournal $journal
|
||||
* @return TriggerInterface
|
||||
*/
|
||||
public static function getTrigger(RuleTrigger $trigger, TransactionJournal $journal): TriggerInterface {
|
||||
$triggerType = $trigger->trigger_type;
|
||||
$class = self::getTriggerClass($triggerType);
|
||||
|
||||
return new $class($trigger, $journal);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a map with triggertypes, mapped to the class representing that type
|
||||
*/
|
||||
protected static function getTriggerTypes() {
|
||||
if( !self::$triggerTypes ) {
|
||||
self::$triggerTypes = Domain::getRuleTriggers();
|
||||
}
|
||||
|
||||
return self::$triggerTypes;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user