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; + } +}