Skip to content

Firefly III: a personal finances manager

Moved creation of Trigger objects to factory for reuse

Robert Horlingsbc3d64a2fff0
diff --git a/app/Rules/Processor.php b/app/Rules/Processor.php
index 1b1d78341b..b8a53664fd 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;
@@ -147,19 +148,9 @@ class Processor
/** @var RuleTrigger $trigger */
foreach ($triggers 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 @@
+<?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;
+ }
+}