Skip to content

Firefly III: a personal finances manager

Implemented action factory analogue to trigger factory

Robert Horlingsb671da900a72
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 @@
+<?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;
+ }
+}
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;