From d8bb83e8c43ba3838f7402f06825d9baa7d8404f Mon Sep 17 00:00:00 2001 From: Robert Horlings Date: Wed, 17 Feb 2016 13:13:23 +0100 Subject: [PATCH 1/3] Moved creation of Trigger objects to factory for reuse --- app/Rules/Processor.php | 15 ++---- app/Rules/Triggers/TriggerFactory.php | 69 +++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 app/Rules/Triggers/TriggerFactory.php diff --git a/app/Rules/Processor.php b/app/Rules/Processor.php index c8c3a3b3cf..5c88a8e23a 100644 --- a/app/Rules/Processor.php +++ b/app/Rules/Processor.php @@ -16,6 +16,7 @@ use FireflyIII\Models\RuleTrigger; use FireflyIII\Models\TransactionJournal; use FireflyIII\Rules\Actions\ActionInterface; use FireflyIII\Rules\Triggers\TriggerInterface; +use FireflyIII\Rules\Triggers\TriggerFactory; use FireflyIII\Support\Domain; use Log; @@ -130,19 +131,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++; } diff --git a/app/Rules/Triggers/TriggerFactory.php b/app/Rules/Triggers/TriggerFactory.php new file mode 100644 index 0000000000..cb7c68efd2 --- /dev/null +++ b/app/Rules/Triggers/TriggerFactory.php @@ -0,0 +1,69 @@ +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; + } +} From b671da900a72d32828acacad6b627fd36c7d9507 Mon Sep 17 00:00:00 2001 From: Robert Horlings Date: Wed, 17 Feb 2016 13:24:56 +0100 Subject: [PATCH 2/3] Implemented action factory analogue to trigger factory --- app/Rules/Actions/ActionFactory.php | 69 +++++++++++++++++++++++++++++ app/Rules/Processor.php | 9 +--- 2 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 app/Rules/Actions/ActionFactory.php diff --git a/app/Rules/Actions/ActionFactory.php b/app/Rules/Actions/ActionFactory.php new file mode 100644 index 0000000000..43161b56a1 --- /dev/null +++ b/app/Rules/Actions/ActionFactory.php @@ -0,0 +1,69 @@ +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; + } +} diff --git a/app/Rules/Processor.php b/app/Rules/Processor.php index 5c88a8e23a..4bde8b13c2 100644 --- a/app/Rules/Processor.php +++ b/app/Rules/Processor.php @@ -15,6 +15,7 @@ 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\Rules\Triggers\TriggerFactory; use FireflyIII\Support\Domain; @@ -103,14 +104,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; From af7da586aa3a37975c2f9614709062f9c2259749 Mon Sep 17 00:00:00 2001 From: Robert Horlings Date: Wed, 17 Feb 2016 13:26:38 +0100 Subject: [PATCH 3/3] Cleanup of processor after introduction of factories --- app/Rules/Processor.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/app/Rules/Processor.php b/app/Rules/Processor.php index 4bde8b13c2..bcd81a4d86 100644 --- a/app/Rules/Processor.php +++ b/app/Rules/Processor.php @@ -18,7 +18,6 @@ use FireflyIII\Rules\Actions\ActionInterface; use FireflyIII\Rules\Actions\ActionFactory; use FireflyIII\Rules\Triggers\TriggerInterface; use FireflyIII\Rules\Triggers\TriggerFactory; -use FireflyIII\Support\Domain; use Log; /** @@ -32,10 +31,6 @@ class Processor protected $journal; /** @var Rule */ protected $rule; - /** @var array */ - private $actionTypes = []; - /** @var array */ - private $triggerTypes = []; /** * Processor constructor. @@ -47,8 +42,6 @@ class Processor { $this->rule = $rule; $this->journal = $journal; - $this->triggerTypes = Domain::getRuleTriggers(); - $this->actionTypes = Domain::getRuleActions(); } /**