From bb5f935d7ab8712f2a3301a02b1618ace34b3b0f Mon Sep 17 00:00:00 2001 From: Robert Horlings Date: Wed, 17 Feb 2016 10:13:21 +0100 Subject: [PATCH 01/11] Added closing li tags --- resources/views/rules/index.twig | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/resources/views/rules/index.twig b/resources/views/rules/index.twig index 272ed57928..447cf3d4eb 100644 --- a/resources/views/rules/index.twig +++ b/resources/views/rules/index.twig @@ -46,17 +46,16 @@
  • {{ 'edit'|_ }}
  • {{ 'delete'|_ }} + class="fa fa-fw fa-trash"> {{ 'delete'|_ }}
  • {% if ruleGroup.order > 1 %}
  • {{ 'move_rule_group_up'|_ }} + class="fa fa-fw fa-arrow-up"> {{ 'move_rule_group_up'|_ }}
  • {% endif %} {% if ruleGroup.order < ruleGroups|length %}
  • {{ 'move_rule_group_down'|_ }} - +
  • {% endif %} - From 7c8c82edd7b2aeae016a3376e89f6794ab0302bf Mon Sep 17 00:00:00 2001 From: Robert Horlings Date: Wed, 17 Feb 2016 13:02:27 +0100 Subject: [PATCH 02/11] Added method for triggers to check whether they match all transactions --- app/Rules/Triggers/AmountExactly.php | 7 +++++++ app/Rules/Triggers/AmountLess.php | 7 +++++++ app/Rules/Triggers/AmountMore.php | 9 +++++++++ app/Rules/Triggers/DescriptionContains.php | 9 +++++++++ app/Rules/Triggers/DescriptionEnds.php | 10 ++++++++++ app/Rules/Triggers/DescriptionIs.php | 7 +++++++ app/Rules/Triggers/DescriptionStarts.php | 10 ++++++++++ app/Rules/Triggers/FromAccountContains.php | 10 ++++++++++ app/Rules/Triggers/FromAccountEnds.php | 11 +++++++++++ app/Rules/Triggers/FromAccountIs.php | 7 +++++++ app/Rules/Triggers/FromAccountStarts.php | 11 +++++++++++ app/Rules/Triggers/ToAccountContains.php | 11 +++++++++++ app/Rules/Triggers/ToAccountEnds.php | 10 ++++++++++ app/Rules/Triggers/ToAccountIs.php | 7 +++++++ app/Rules/Triggers/ToAccountStarts.php | 11 +++++++++++ app/Rules/Triggers/TransactionType.php | 7 +++++++ app/Rules/Triggers/TriggerInterface.php | 7 +++++++ app/Rules/Triggers/UserAction.php | 6 ++++++ 18 files changed, 157 insertions(+) diff --git a/app/Rules/Triggers/AmountExactly.php b/app/Rules/Triggers/AmountExactly.php index 02045a42f8..998d851dd5 100644 --- a/app/Rules/Triggers/AmountExactly.php +++ b/app/Rules/Triggers/AmountExactly.php @@ -62,4 +62,11 @@ class AmountExactly implements TriggerInterface return false; } + + /** + * Checks whether this trigger will match all transactions + * For example: amount > 0 or description starts with '' + * @return bool + */ + public function matchesAnything() { return false; } } diff --git a/app/Rules/Triggers/AmountLess.php b/app/Rules/Triggers/AmountLess.php index cfad91f24b..e90428a233 100644 --- a/app/Rules/Triggers/AmountLess.php +++ b/app/Rules/Triggers/AmountLess.php @@ -62,4 +62,11 @@ class AmountLess implements TriggerInterface return false; } + + /** + * Checks whether this trigger will match all transactions + * For example: amount > 0 or description starts with '' + * @return bool + */ + public function matchesAnything() { return false; } } diff --git a/app/Rules/Triggers/AmountMore.php b/app/Rules/Triggers/AmountMore.php index a7d6711f1f..4a1376dfe7 100644 --- a/app/Rules/Triggers/AmountMore.php +++ b/app/Rules/Triggers/AmountMore.php @@ -62,4 +62,13 @@ class AmountMore implements TriggerInterface return false; } + + /** + * Checks whether this trigger will match all transactions + * This happens when the trigger_value is zero + * @return bool + */ + public function matchesAnything() { + return bccomp('0', $this->trigger->trigger_value) === 0; + } } diff --git a/app/Rules/Triggers/DescriptionContains.php b/app/Rules/Triggers/DescriptionContains.php index 4e5fe8428a..a26d8628d0 100644 --- a/app/Rules/Triggers/DescriptionContains.php +++ b/app/Rules/Triggers/DescriptionContains.php @@ -63,4 +63,13 @@ class DescriptionContains implements TriggerInterface return false; } + + /** + * Checks whether this trigger will match all transactions + * This happens when the trigger_value is empty + * @return bool + */ + public function matchesAnything() { + return $this->trigger->trigger_value === ""; + } } diff --git a/app/Rules/Triggers/DescriptionEnds.php b/app/Rules/Triggers/DescriptionEnds.php index b71009799a..fde345df80 100644 --- a/app/Rules/Triggers/DescriptionEnds.php +++ b/app/Rules/Triggers/DescriptionEnds.php @@ -72,4 +72,14 @@ class DescriptionEnds implements TriggerInterface return false; } + + /** + * Checks whether this trigger will match all transactions + * This happens when the trigger_value is empty + * @return bool + */ + public function matchesAnything() { + return $this->trigger->trigger_value === ""; + } + } diff --git a/app/Rules/Triggers/DescriptionIs.php b/app/Rules/Triggers/DescriptionIs.php index 12bc48b697..2d38bc46f6 100644 --- a/app/Rules/Triggers/DescriptionIs.php +++ b/app/Rules/Triggers/DescriptionIs.php @@ -58,4 +58,11 @@ class DescriptionIs implements TriggerInterface return false; } + + /** + * Checks whether this trigger will match all transactions + * @return bool + */ + public function matchesAnything() { return false; } + } diff --git a/app/Rules/Triggers/DescriptionStarts.php b/app/Rules/Triggers/DescriptionStarts.php index 9b23e1253c..2b41bad3e3 100644 --- a/app/Rules/Triggers/DescriptionStarts.php +++ b/app/Rules/Triggers/DescriptionStarts.php @@ -60,4 +60,14 @@ class DescriptionStarts implements TriggerInterface return false; } + + /** + * Checks whether this trigger will match all transactions + * This happens when the trigger_value is empty + * @return bool + */ + public function matchesAnything() { + return $this->trigger->trigger_value === ""; + } + } diff --git a/app/Rules/Triggers/FromAccountContains.php b/app/Rules/Triggers/FromAccountContains.php index 05badb6f7b..9da8cbd290 100644 --- a/app/Rules/Triggers/FromAccountContains.php +++ b/app/Rules/Triggers/FromAccountContains.php @@ -62,4 +62,14 @@ class FromAccountContains implements TriggerInterface return false; } + + /** + * Checks whether this trigger will match all transactions + * This happens when the trigger_value is empty + * @return bool + */ + public function matchesAnything() { + return $this->trigger->trigger_value === ""; + } + } diff --git a/app/Rules/Triggers/FromAccountEnds.php b/app/Rules/Triggers/FromAccountEnds.php index e4b144bb87..26927c6370 100644 --- a/app/Rules/Triggers/FromAccountEnds.php +++ b/app/Rules/Triggers/FromAccountEnds.php @@ -72,4 +72,15 @@ class FromAccountEnds implements TriggerInterface return false; } + + + /** + * Checks whether this trigger will match all transactions + * This happens when the trigger_value is empty + * @return bool + */ + public function matchesAnything() { + return $this->trigger->trigger_value === ""; + } + } diff --git a/app/Rules/Triggers/FromAccountIs.php b/app/Rules/Triggers/FromAccountIs.php index 9fb084de93..1cdbc30521 100644 --- a/app/Rules/Triggers/FromAccountIs.php +++ b/app/Rules/Triggers/FromAccountIs.php @@ -58,4 +58,11 @@ class FromAccountIs implements TriggerInterface return false; } + + /** + * Checks whether this trigger will match all transactions + * @return bool + */ + public function matchesAnything() { return false; } + } diff --git a/app/Rules/Triggers/FromAccountStarts.php b/app/Rules/Triggers/FromAccountStarts.php index 6eea3179f4..43e8f915b9 100644 --- a/app/Rules/Triggers/FromAccountStarts.php +++ b/app/Rules/Triggers/FromAccountStarts.php @@ -60,4 +60,15 @@ class FromAccountStarts implements TriggerInterface return false; } + + + /** + * Checks whether this trigger will match all transactions + * This happens when the trigger_value is empty + * @return bool + */ + public function matchesAnything() { + return $this->trigger->trigger_value === ""; + } + } diff --git a/app/Rules/Triggers/ToAccountContains.php b/app/Rules/Triggers/ToAccountContains.php index 06d081bfac..0e291a904a 100644 --- a/app/Rules/Triggers/ToAccountContains.php +++ b/app/Rules/Triggers/ToAccountContains.php @@ -62,4 +62,15 @@ class ToAccountContains implements TriggerInterface return false; } + + + /** + * Checks whether this trigger will match all transactions + * This happens when the trigger_value is empty + * @return bool + */ + public function matchesAnything() { + return $this->trigger->trigger_value === ""; + } + } diff --git a/app/Rules/Triggers/ToAccountEnds.php b/app/Rules/Triggers/ToAccountEnds.php index 5637df0cb6..60784e4b6f 100644 --- a/app/Rules/Triggers/ToAccountEnds.php +++ b/app/Rules/Triggers/ToAccountEnds.php @@ -72,4 +72,14 @@ class ToAccountEnds implements TriggerInterface return false; } + + + /** + * Checks whether this trigger will match all transactions + * This happens when the trigger_value is empty + * @return bool + */ + public function matchesAnything() { + return $this->trigger->trigger_value === ""; + } } diff --git a/app/Rules/Triggers/ToAccountIs.php b/app/Rules/Triggers/ToAccountIs.php index 1eb9f5623e..f11fdf272f 100644 --- a/app/Rules/Triggers/ToAccountIs.php +++ b/app/Rules/Triggers/ToAccountIs.php @@ -58,4 +58,11 @@ class ToAccountIs implements TriggerInterface return false; } + + /** + * Checks whether this trigger will match all transactions + * @return bool + */ + public function matchesAnything() { return false; } + } diff --git a/app/Rules/Triggers/ToAccountStarts.php b/app/Rules/Triggers/ToAccountStarts.php index 446a592d29..bf0ca5f5d7 100644 --- a/app/Rules/Triggers/ToAccountStarts.php +++ b/app/Rules/Triggers/ToAccountStarts.php @@ -60,4 +60,15 @@ class ToAccountStarts implements TriggerInterface return false; } + + + /** + * Checks whether this trigger will match all transactions + * This happens when the trigger_value is empty + * @return bool + */ + public function matchesAnything() { + return $this->trigger->trigger_value === ""; + } + } diff --git a/app/Rules/Triggers/TransactionType.php b/app/Rules/Triggers/TransactionType.php index 1a96720fc8..4d46520d50 100644 --- a/app/Rules/Triggers/TransactionType.php +++ b/app/Rules/Triggers/TransactionType.php @@ -57,4 +57,11 @@ class TransactionType implements TriggerInterface return false; } + + /** + * Checks whether this trigger will match all transactions + * @return bool + */ + public function matchesAnything() { return false; } + } diff --git a/app/Rules/Triggers/TriggerInterface.php b/app/Rules/Triggers/TriggerInterface.php index 074cacdd40..5a653a9d9f 100644 --- a/app/Rules/Triggers/TriggerInterface.php +++ b/app/Rules/Triggers/TriggerInterface.php @@ -32,4 +32,11 @@ interface TriggerInterface * @return bool */ public function triggered(); + + /** + * Checks whether this trigger will match all transactions + * For example: amount > 0 or description starts with '' + * @return bool + */ + public function matchesAnything(); } diff --git a/app/Rules/Triggers/UserAction.php b/app/Rules/Triggers/UserAction.php index 917f2d8822..4e576bd573 100644 --- a/app/Rules/Triggers/UserAction.php +++ b/app/Rules/Triggers/UserAction.php @@ -53,4 +53,10 @@ class UserAction implements TriggerInterface return true; } + /** + * Checks whether this trigger will match all transactions + * @return bool + */ + public function matchesAnything() { return true; } + } From 70e72c246d5a9eb93a9a4f03bf65c71bee9335f5 Mon Sep 17 00:00:00 2001 From: Robert Horlings Date: Wed, 17 Feb 2016 13:03:02 +0100 Subject: [PATCH 03/11] Added method to RuleTrigger object as well --- app/Models/RuleTrigger.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/Models/RuleTrigger.php b/app/Models/RuleTrigger.php index dc92048977..cf13c918d7 100644 --- a/app/Models/RuleTrigger.php +++ b/app/Models/RuleTrigger.php @@ -36,4 +36,12 @@ class RuleTrigger extends Model { return $this->belongsTo('FireflyIII\Models\Rule'); } + + /** + * Checks whether this trigger will match all transactions + * For example: amount > 0 or description starts with '' + */ + public function matchesAnything() { + return TriggerFactory::getTrigger($this, new TransactionJournal)->matchesAnything(); + } } From d8bb83e8c43ba3838f7402f06825d9baa7d8404f Mon Sep 17 00:00:00 2001 From: Robert Horlings Date: Wed, 17 Feb 2016 13:13:23 +0100 Subject: [PATCH 04/11] 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 05/11] 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 06/11] 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(); } /** From 52481a6e8b1aa19afdd516a77aa5939c41bb9194 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 17 Feb 2016 15:29:26 +0100 Subject: [PATCH 07/11] Code formatting. --- app/Rules/Triggers/AmountExactly.php | 8 +++-- app/Rules/Triggers/AmountLess.php | 8 +++-- app/Rules/Triggers/AmountMore.php | 8 +++-- app/Rules/Triggers/DescriptionContains.php | 8 +++-- app/Rules/Triggers/DescriptionEnds.php | 8 +++-- app/Rules/Triggers/DescriptionIs.php | 10 +++++-- app/Rules/Triggers/DescriptionStarts.php | 8 +++-- app/Rules/Triggers/FromAccountContains.php | 8 +++-- app/Rules/Triggers/FromAccountEnds.php | 8 +++-- app/Rules/Triggers/FromAccountIs.php | 8 +++-- app/Rules/Triggers/FromAccountStarts.php | 8 +++-- app/Rules/Triggers/ToAccountContains.php | 8 +++-- app/Rules/Triggers/ToAccountEnds.php | 6 ++-- app/Rules/Triggers/ToAccountIs.php | 10 +++++-- app/Rules/Triggers/ToAccountStarts.php | 8 +++-- app/Rules/Triggers/TransactionType.php | 10 +++++-- app/Rules/Triggers/TriggerFactory.php | 35 +++++++++++++--------- app/Rules/Triggers/TriggerInterface.php | 11 +++---- app/Rules/Triggers/UserAction.php | 8 +++-- 19 files changed, 121 insertions(+), 65 deletions(-) diff --git a/app/Rules/Triggers/AmountExactly.php b/app/Rules/Triggers/AmountExactly.php index 998d851dd5..dba3d90867 100644 --- a/app/Rules/Triggers/AmountExactly.php +++ b/app/Rules/Triggers/AmountExactly.php @@ -62,11 +62,15 @@ class AmountExactly implements TriggerInterface return false; } - + /** * Checks whether this trigger will match all transactions * For example: amount > 0 or description starts with '' + * * @return bool */ - public function matchesAnything() { return false; } + public function matchesAnything() + { + return false; + } } diff --git a/app/Rules/Triggers/AmountLess.php b/app/Rules/Triggers/AmountLess.php index e90428a233..bf7aaca8cb 100644 --- a/app/Rules/Triggers/AmountLess.php +++ b/app/Rules/Triggers/AmountLess.php @@ -62,11 +62,15 @@ class AmountLess implements TriggerInterface return false; } - + /** * Checks whether this trigger will match all transactions * For example: amount > 0 or description starts with '' + * * @return bool */ - public function matchesAnything() { return false; } + public function matchesAnything() + { + return false; + } } diff --git a/app/Rules/Triggers/AmountMore.php b/app/Rules/Triggers/AmountMore.php index 4a1376dfe7..e48df74f3b 100644 --- a/app/Rules/Triggers/AmountMore.php +++ b/app/Rules/Triggers/AmountMore.php @@ -62,13 +62,15 @@ class AmountMore implements TriggerInterface return false; } - + /** * Checks whether this trigger will match all transactions * This happens when the trigger_value is zero + * * @return bool */ - public function matchesAnything() { - return bccomp('0', $this->trigger->trigger_value) === 0; + public function matchesAnything() + { + return bccomp('0', $this->trigger->trigger_value) === 0; } } diff --git a/app/Rules/Triggers/DescriptionContains.php b/app/Rules/Triggers/DescriptionContains.php index a26d8628d0..3582f6768e 100644 --- a/app/Rules/Triggers/DescriptionContains.php +++ b/app/Rules/Triggers/DescriptionContains.php @@ -63,13 +63,15 @@ class DescriptionContains implements TriggerInterface return false; } - + /** * Checks whether this trigger will match all transactions * This happens when the trigger_value is empty + * * @return bool */ - public function matchesAnything() { + public function matchesAnything() + { return $this->trigger->trigger_value === ""; - } + } } diff --git a/app/Rules/Triggers/DescriptionEnds.php b/app/Rules/Triggers/DescriptionEnds.php index fde345df80..d1b214b37c 100644 --- a/app/Rules/Triggers/DescriptionEnds.php +++ b/app/Rules/Triggers/DescriptionEnds.php @@ -72,14 +72,16 @@ class DescriptionEnds implements TriggerInterface return false; } - + /** * Checks whether this trigger will match all transactions * This happens when the trigger_value is empty + * * @return bool */ - public function matchesAnything() { + public function matchesAnything() + { return $this->trigger->trigger_value === ""; } - + } diff --git a/app/Rules/Triggers/DescriptionIs.php b/app/Rules/Triggers/DescriptionIs.php index 2d38bc46f6..bf1e115371 100644 --- a/app/Rules/Triggers/DescriptionIs.php +++ b/app/Rules/Triggers/DescriptionIs.php @@ -58,11 +58,15 @@ class DescriptionIs implements TriggerInterface return false; } - + /** * Checks whether this trigger will match all transactions + * * @return bool */ - public function matchesAnything() { return false; } - + public function matchesAnything() + { + return false; + } + } diff --git a/app/Rules/Triggers/DescriptionStarts.php b/app/Rules/Triggers/DescriptionStarts.php index 2b41bad3e3..f24fa5cebf 100644 --- a/app/Rules/Triggers/DescriptionStarts.php +++ b/app/Rules/Triggers/DescriptionStarts.php @@ -60,14 +60,16 @@ class DescriptionStarts implements TriggerInterface return false; } - + /** * Checks whether this trigger will match all transactions * This happens when the trigger_value is empty + * * @return bool */ - public function matchesAnything() { + public function matchesAnything() + { return $this->trigger->trigger_value === ""; } - + } diff --git a/app/Rules/Triggers/FromAccountContains.php b/app/Rules/Triggers/FromAccountContains.php index 9da8cbd290..417f69284b 100644 --- a/app/Rules/Triggers/FromAccountContains.php +++ b/app/Rules/Triggers/FromAccountContains.php @@ -62,14 +62,16 @@ class FromAccountContains implements TriggerInterface return false; } - + /** * Checks whether this trigger will match all transactions * This happens when the trigger_value is empty + * * @return bool */ - public function matchesAnything() { + public function matchesAnything() + { return $this->trigger->trigger_value === ""; } - + } diff --git a/app/Rules/Triggers/FromAccountEnds.php b/app/Rules/Triggers/FromAccountEnds.php index 26927c6370..798fee6bc4 100644 --- a/app/Rules/Triggers/FromAccountEnds.php +++ b/app/Rules/Triggers/FromAccountEnds.php @@ -72,15 +72,17 @@ class FromAccountEnds implements TriggerInterface return false; } - + /** * Checks whether this trigger will match all transactions * This happens when the trigger_value is empty + * * @return bool */ - public function matchesAnything() { + public function matchesAnything() + { return $this->trigger->trigger_value === ""; } - + } diff --git a/app/Rules/Triggers/FromAccountIs.php b/app/Rules/Triggers/FromAccountIs.php index 1cdbc30521..98b294e7ad 100644 --- a/app/Rules/Triggers/FromAccountIs.php +++ b/app/Rules/Triggers/FromAccountIs.php @@ -61,8 +61,12 @@ class FromAccountIs implements TriggerInterface /** * Checks whether this trigger will match all transactions + * * @return bool */ - public function matchesAnything() { return false; } - + public function matchesAnything() + { + return false; + } + } diff --git a/app/Rules/Triggers/FromAccountStarts.php b/app/Rules/Triggers/FromAccountStarts.php index 43e8f915b9..b88fb2739b 100644 --- a/app/Rules/Triggers/FromAccountStarts.php +++ b/app/Rules/Triggers/FromAccountStarts.php @@ -60,15 +60,17 @@ class FromAccountStarts implements TriggerInterface return false; } - + /** * Checks whether this trigger will match all transactions * This happens when the trigger_value is empty + * * @return bool */ - public function matchesAnything() { + public function matchesAnything() + { return $this->trigger->trigger_value === ""; } - + } diff --git a/app/Rules/Triggers/ToAccountContains.php b/app/Rules/Triggers/ToAccountContains.php index 0e291a904a..1431f8c8bb 100644 --- a/app/Rules/Triggers/ToAccountContains.php +++ b/app/Rules/Triggers/ToAccountContains.php @@ -62,15 +62,17 @@ class ToAccountContains implements TriggerInterface return false; } - + /** * Checks whether this trigger will match all transactions * This happens when the trigger_value is empty + * * @return bool */ - public function matchesAnything() { + public function matchesAnything() + { return $this->trigger->trigger_value === ""; } - + } diff --git a/app/Rules/Triggers/ToAccountEnds.php b/app/Rules/Triggers/ToAccountEnds.php index 60784e4b6f..5ddccd7a96 100644 --- a/app/Rules/Triggers/ToAccountEnds.php +++ b/app/Rules/Triggers/ToAccountEnds.php @@ -72,14 +72,16 @@ class ToAccountEnds implements TriggerInterface return false; } - + /** * Checks whether this trigger will match all transactions * This happens when the trigger_value is empty + * * @return bool */ - public function matchesAnything() { + public function matchesAnything() + { return $this->trigger->trigger_value === ""; } } diff --git a/app/Rules/Triggers/ToAccountIs.php b/app/Rules/Triggers/ToAccountIs.php index f11fdf272f..409d29ba2c 100644 --- a/app/Rules/Triggers/ToAccountIs.php +++ b/app/Rules/Triggers/ToAccountIs.php @@ -58,11 +58,15 @@ class ToAccountIs implements TriggerInterface return false; } - + /** * Checks whether this trigger will match all transactions + * * @return bool */ - public function matchesAnything() { return false; } - + public function matchesAnything() + { + return false; + } + } diff --git a/app/Rules/Triggers/ToAccountStarts.php b/app/Rules/Triggers/ToAccountStarts.php index bf0ca5f5d7..6eeab8e06d 100644 --- a/app/Rules/Triggers/ToAccountStarts.php +++ b/app/Rules/Triggers/ToAccountStarts.php @@ -60,15 +60,17 @@ class ToAccountStarts implements TriggerInterface return false; } - + /** * Checks whether this trigger will match all transactions * This happens when the trigger_value is empty + * * @return bool */ - public function matchesAnything() { + public function matchesAnything() + { return $this->trigger->trigger_value === ""; } - + } diff --git a/app/Rules/Triggers/TransactionType.php b/app/Rules/Triggers/TransactionType.php index 4d46520d50..dbd17c1b47 100644 --- a/app/Rules/Triggers/TransactionType.php +++ b/app/Rules/Triggers/TransactionType.php @@ -57,11 +57,15 @@ class TransactionType implements TriggerInterface return false; } - + /** * Checks whether this trigger will match all transactions + * * @return bool */ - public function matchesAnything() { return false; } - + public function matchesAnything() + { + return false; + } + } diff --git a/app/Rules/Triggers/TriggerFactory.php b/app/Rules/Triggers/TriggerFactory.php index cb7c68efd2..8c2a38d8f4 100644 --- a/app/Rules/Triggers/TriggerFactory.php +++ b/app/Rules/Triggers/TriggerFactory.php @@ -22,48 +22,55 @@ use FireflyIII\Support\Domain; 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 { + 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 RuleTrigger $trigger * @param TransactionJournal $journal + * * @return TriggerInterface */ - public static function getTrigger(RuleTrigger $trigger, TransactionJournal $journal): TriggerInterface { + public static function getTrigger(RuleTrigger $trigger, TransactionJournal $journal): TriggerInterface + { $triggerType = $trigger->trigger_type; - $class = self::getTriggerClass($triggerType); - + $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 ) { + protected static function getTriggerTypes() + { + if (!self::$triggerTypes) { self::$triggerTypes = Domain::getRuleTriggers(); } - + return self::$triggerTypes; } } diff --git a/app/Rules/Triggers/TriggerInterface.php b/app/Rules/Triggers/TriggerInterface.php index 5a653a9d9f..76ae6052bb 100644 --- a/app/Rules/Triggers/TriggerInterface.php +++ b/app/Rules/Triggers/TriggerInterface.php @@ -28,15 +28,16 @@ interface TriggerInterface */ public function __construct(RuleTrigger $trigger, TransactionJournal $journal); - /** - * @return bool - */ - public function triggered(); - /** * Checks whether this trigger will match all transactions * For example: amount > 0 or description starts with '' + * * @return bool */ public function matchesAnything(); + + /** + * @return bool + */ + public function triggered(); } diff --git a/app/Rules/Triggers/UserAction.php b/app/Rules/Triggers/UserAction.php index 4e576bd573..475ec8a82e 100644 --- a/app/Rules/Triggers/UserAction.php +++ b/app/Rules/Triggers/UserAction.php @@ -55,8 +55,12 @@ class UserAction implements TriggerInterface /** * Checks whether this trigger will match all transactions + * * @return bool */ - public function matchesAnything() { return true; } - + public function matchesAnything() + { + return true; + } + } From 9cac61dc3321bc99d0a00810eaf52ce9494fd657 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 17 Feb 2016 15:37:05 +0100 Subject: [PATCH 08/11] Better description for matchesAnything --- app/Rules/Triggers/AmountExactly.php | 5 ++-- app/Rules/Triggers/AmountLess.php | 5 ++-- app/Rules/Triggers/AmountMore.php | 5 ++-- app/Rules/Triggers/DescriptionContains.php | 5 ++-- app/Rules/Triggers/DescriptionEnds.php | 5 ++-- app/Rules/Triggers/DescriptionIs.php | 4 ++- app/Rules/Triggers/DescriptionStarts.php | 29 +++++++++++----------- app/Rules/Triggers/FromAccountContains.php | 5 ++-- app/Rules/Triggers/FromAccountEnds.php | 5 ++-- app/Rules/Triggers/FromAccountIs.php | 4 ++- app/Rules/Triggers/FromAccountStarts.php | 5 ++-- app/Rules/Triggers/ToAccountContains.php | 5 ++-- app/Rules/Triggers/ToAccountEnds.php | 5 ++-- app/Rules/Triggers/ToAccountIs.php | 4 ++- app/Rules/Triggers/ToAccountStarts.php | 5 ++-- app/Rules/Triggers/TransactionType.php | 4 ++- app/Rules/Triggers/TriggerFactory.php | 1 + app/Rules/Triggers/TriggerInterface.php | 12 +++++++-- app/Rules/Triggers/UserAction.php | 4 ++- 19 files changed, 73 insertions(+), 44 deletions(-) diff --git a/app/Rules/Triggers/AmountExactly.php b/app/Rules/Triggers/AmountExactly.php index dba3d90867..a9e13cf388 100644 --- a/app/Rules/Triggers/AmountExactly.php +++ b/app/Rules/Triggers/AmountExactly.php @@ -64,8 +64,9 @@ class AmountExactly implements TriggerInterface } /** - * Checks whether this trigger will match all transactions - * For example: amount > 0 or description starts with '' + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything * * @return bool */ diff --git a/app/Rules/Triggers/AmountLess.php b/app/Rules/Triggers/AmountLess.php index bf7aaca8cb..29e38e181b 100644 --- a/app/Rules/Triggers/AmountLess.php +++ b/app/Rules/Triggers/AmountLess.php @@ -64,8 +64,9 @@ class AmountLess implements TriggerInterface } /** - * Checks whether this trigger will match all transactions - * For example: amount > 0 or description starts with '' + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything * * @return bool */ diff --git a/app/Rules/Triggers/AmountMore.php b/app/Rules/Triggers/AmountMore.php index e48df74f3b..583d893ddc 100644 --- a/app/Rules/Triggers/AmountMore.php +++ b/app/Rules/Triggers/AmountMore.php @@ -64,8 +64,9 @@ class AmountMore implements TriggerInterface } /** - * Checks whether this trigger will match all transactions - * This happens when the trigger_value is zero + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything * * @return bool */ diff --git a/app/Rules/Triggers/DescriptionContains.php b/app/Rules/Triggers/DescriptionContains.php index 3582f6768e..cd639078c0 100644 --- a/app/Rules/Triggers/DescriptionContains.php +++ b/app/Rules/Triggers/DescriptionContains.php @@ -65,8 +65,9 @@ class DescriptionContains implements TriggerInterface } /** - * Checks whether this trigger will match all transactions - * This happens when the trigger_value is empty + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything * * @return bool */ diff --git a/app/Rules/Triggers/DescriptionEnds.php b/app/Rules/Triggers/DescriptionEnds.php index d1b214b37c..0eb3c99f83 100644 --- a/app/Rules/Triggers/DescriptionEnds.php +++ b/app/Rules/Triggers/DescriptionEnds.php @@ -74,8 +74,9 @@ class DescriptionEnds implements TriggerInterface } /** - * Checks whether this trigger will match all transactions - * This happens when the trigger_value is empty + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything * * @return bool */ diff --git a/app/Rules/Triggers/DescriptionIs.php b/app/Rules/Triggers/DescriptionIs.php index bf1e115371..5d1011bf6a 100644 --- a/app/Rules/Triggers/DescriptionIs.php +++ b/app/Rules/Triggers/DescriptionIs.php @@ -60,7 +60,9 @@ class DescriptionIs implements TriggerInterface } /** - * Checks whether this trigger will match all transactions + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything * * @return bool */ diff --git a/app/Rules/Triggers/DescriptionStarts.php b/app/Rules/Triggers/DescriptionStarts.php index f24fa5cebf..f5ad96953d 100644 --- a/app/Rules/Triggers/DescriptionStarts.php +++ b/app/Rules/Triggers/DescriptionStarts.php @@ -21,12 +21,10 @@ use Log; */ class DescriptionStarts implements TriggerInterface { - /** @var RuleTrigger */ - protected $trigger; - /** @var TransactionJournal */ protected $journal; - + /** @var RuleTrigger */ + protected $trigger; /** * TriggerInterface constructor. @@ -40,6 +38,18 @@ class DescriptionStarts implements TriggerInterface $this->journal = $journal; } + /** + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything + * + * @return bool + */ + public function matchesAnything() + { + return $this->trigger->trigger_value === ""; + } + /** * @return bool */ @@ -61,15 +71,4 @@ class DescriptionStarts implements TriggerInterface } - /** - * Checks whether this trigger will match all transactions - * This happens when the trigger_value is empty - * - * @return bool - */ - public function matchesAnything() - { - return $this->trigger->trigger_value === ""; - } - } diff --git a/app/Rules/Triggers/FromAccountContains.php b/app/Rules/Triggers/FromAccountContains.php index 417f69284b..e7bd31ed5d 100644 --- a/app/Rules/Triggers/FromAccountContains.php +++ b/app/Rules/Triggers/FromAccountContains.php @@ -64,8 +64,9 @@ class FromAccountContains implements TriggerInterface } /** - * Checks whether this trigger will match all transactions - * This happens when the trigger_value is empty + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything * * @return bool */ diff --git a/app/Rules/Triggers/FromAccountEnds.php b/app/Rules/Triggers/FromAccountEnds.php index 798fee6bc4..bba52c85c5 100644 --- a/app/Rules/Triggers/FromAccountEnds.php +++ b/app/Rules/Triggers/FromAccountEnds.php @@ -75,8 +75,9 @@ class FromAccountEnds implements TriggerInterface /** - * Checks whether this trigger will match all transactions - * This happens when the trigger_value is empty + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything * * @return bool */ diff --git a/app/Rules/Triggers/FromAccountIs.php b/app/Rules/Triggers/FromAccountIs.php index 98b294e7ad..90d38ca6d9 100644 --- a/app/Rules/Triggers/FromAccountIs.php +++ b/app/Rules/Triggers/FromAccountIs.php @@ -60,7 +60,9 @@ class FromAccountIs implements TriggerInterface } /** - * Checks whether this trigger will match all transactions + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything * * @return bool */ diff --git a/app/Rules/Triggers/FromAccountStarts.php b/app/Rules/Triggers/FromAccountStarts.php index b88fb2739b..43a8f5b7a0 100644 --- a/app/Rules/Triggers/FromAccountStarts.php +++ b/app/Rules/Triggers/FromAccountStarts.php @@ -63,8 +63,9 @@ class FromAccountStarts implements TriggerInterface /** - * Checks whether this trigger will match all transactions - * This happens when the trigger_value is empty + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything * * @return bool */ diff --git a/app/Rules/Triggers/ToAccountContains.php b/app/Rules/Triggers/ToAccountContains.php index 1431f8c8bb..c219437e8b 100644 --- a/app/Rules/Triggers/ToAccountContains.php +++ b/app/Rules/Triggers/ToAccountContains.php @@ -65,8 +65,9 @@ class ToAccountContains implements TriggerInterface /** - * Checks whether this trigger will match all transactions - * This happens when the trigger_value is empty + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything * * @return bool */ diff --git a/app/Rules/Triggers/ToAccountEnds.php b/app/Rules/Triggers/ToAccountEnds.php index 5ddccd7a96..5591ffcca8 100644 --- a/app/Rules/Triggers/ToAccountEnds.php +++ b/app/Rules/Triggers/ToAccountEnds.php @@ -75,8 +75,9 @@ class ToAccountEnds implements TriggerInterface /** - * Checks whether this trigger will match all transactions - * This happens when the trigger_value is empty + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything * * @return bool */ diff --git a/app/Rules/Triggers/ToAccountIs.php b/app/Rules/Triggers/ToAccountIs.php index 409d29ba2c..4596ef9fde 100644 --- a/app/Rules/Triggers/ToAccountIs.php +++ b/app/Rules/Triggers/ToAccountIs.php @@ -60,7 +60,9 @@ class ToAccountIs implements TriggerInterface } /** - * Checks whether this trigger will match all transactions + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything * * @return bool */ diff --git a/app/Rules/Triggers/ToAccountStarts.php b/app/Rules/Triggers/ToAccountStarts.php index 6eeab8e06d..d4423e931a 100644 --- a/app/Rules/Triggers/ToAccountStarts.php +++ b/app/Rules/Triggers/ToAccountStarts.php @@ -63,8 +63,9 @@ class ToAccountStarts implements TriggerInterface /** - * Checks whether this trigger will match all transactions - * This happens when the trigger_value is empty + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything * * @return bool */ diff --git a/app/Rules/Triggers/TransactionType.php b/app/Rules/Triggers/TransactionType.php index dbd17c1b47..3a06c13f30 100644 --- a/app/Rules/Triggers/TransactionType.php +++ b/app/Rules/Triggers/TransactionType.php @@ -59,7 +59,9 @@ class TransactionType implements TriggerInterface } /** - * Checks whether this trigger will match all transactions + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything * * @return bool */ diff --git a/app/Rules/Triggers/TriggerFactory.php b/app/Rules/Triggers/TriggerFactory.php index 8c2a38d8f4..157861889c 100644 --- a/app/Rules/Triggers/TriggerFactory.php +++ b/app/Rules/Triggers/TriggerFactory.php @@ -38,6 +38,7 @@ class TriggerFactory abort(500, 'No such trigger exists ("' . $triggerType . '").'); } + /** @var TriggerInterface $class */ $class = $triggerTypes[$triggerType]; if (!class_exists($class)) { abort(500, 'Could not instantiate class for rule trigger type "' . $triggerType . '" (' . $class . ').'); diff --git a/app/Rules/Triggers/TriggerInterface.php b/app/Rules/Triggers/TriggerInterface.php index 76ae6052bb..2c8f61f4f8 100644 --- a/app/Rules/Triggers/TriggerInterface.php +++ b/app/Rules/Triggers/TriggerInterface.php @@ -29,8 +29,16 @@ interface TriggerInterface public function __construct(RuleTrigger $trigger, TransactionJournal $journal); /** - * Checks whether this trigger will match all transactions - * For example: amount > 0 or description starts with '' + * A trigger is said to "match anything", or match any given transaction, + * when the trigger value is very vague or has no restrictions. Easy examples + * are the "AmountMore"-trigger combined with an amount of 0: any given transaction + * has an amount of more than zero! Other examples are all the "Description"-triggers + * which have hard time handling empty trigger values such as "" or "*" (wild cards). + * + * If the user tries to create such a trigger, this method MUST return true so Firefly III + * can stop the storing / updating the trigger. If the trigger is in any way restrictive + * (even if it will still include 99.9% of the users transactions), this method MUST return + * false. * * @return bool */ diff --git a/app/Rules/Triggers/UserAction.php b/app/Rules/Triggers/UserAction.php index 475ec8a82e..55ab4dbfda 100644 --- a/app/Rules/Triggers/UserAction.php +++ b/app/Rules/Triggers/UserAction.php @@ -54,7 +54,9 @@ class UserAction implements TriggerInterface } /** - * Checks whether this trigger will match all transactions + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything * * @return bool */ From 0d44f82c864cdf267f27ce5bd648c80374cc5ff1 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 17 Feb 2016 15:38:21 +0100 Subject: [PATCH 09/11] Code cleanup. [skip ci] --- app/Rules/Triggers/AmountExactly.php | 30 ++++++++++---------- app/Rules/Triggers/AmountLess.php | 30 ++++++++++---------- app/Rules/Triggers/AmountMore.php | 30 ++++++++++---------- app/Rules/Triggers/DescriptionContains.php | 30 ++++++++++---------- app/Rules/Triggers/DescriptionEnds.php | 30 ++++++++++---------- app/Rules/Triggers/DescriptionIs.php | 30 ++++++++++---------- app/Rules/Triggers/FromAccountContains.php | 30 ++++++++++---------- app/Rules/Triggers/FromAccountEnds.php | 31 ++++++++++----------- app/Rules/Triggers/FromAccountIs.php | 30 ++++++++++---------- app/Rules/Triggers/FromAccountStarts.php | 31 ++++++++++----------- app/Rules/Triggers/ToAccountContains.php | 31 ++++++++++----------- app/Rules/Triggers/ToAccountEnds.php | 31 ++++++++++----------- app/Rules/Triggers/ToAccountIs.php | 30 ++++++++++---------- app/Rules/Triggers/ToAccountStarts.php | 31 ++++++++++----------- app/Rules/Triggers/TransactionType.php | 30 ++++++++++---------- app/Rules/Triggers/TriggerFactory.php | 32 +++++++++++----------- app/Rules/Triggers/UserAction.php | 30 ++++++++++---------- 17 files changed, 240 insertions(+), 277 deletions(-) diff --git a/app/Rules/Triggers/AmountExactly.php b/app/Rules/Triggers/AmountExactly.php index a9e13cf388..03af0bec25 100644 --- a/app/Rules/Triggers/AmountExactly.php +++ b/app/Rules/Triggers/AmountExactly.php @@ -22,12 +22,10 @@ use Log; */ class AmountExactly implements TriggerInterface { - /** @var RuleTrigger */ - protected $trigger; - /** @var TransactionJournal */ protected $journal; - + /** @var RuleTrigger */ + protected $trigger; /** * TriggerInterface constructor. @@ -41,6 +39,18 @@ class AmountExactly implements TriggerInterface $this->journal = $journal; } + /** + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything + * + * @return bool + */ + public function matchesAnything() + { + return false; + } + /** * @return bool */ @@ -62,16 +72,4 @@ class AmountExactly implements TriggerInterface return false; } - - /** - * @{inheritdoc} - * - * @see TriggerInterface::matchesAnything - * - * @return bool - */ - public function matchesAnything() - { - return false; - } } diff --git a/app/Rules/Triggers/AmountLess.php b/app/Rules/Triggers/AmountLess.php index 29e38e181b..5a9c72976b 100644 --- a/app/Rules/Triggers/AmountLess.php +++ b/app/Rules/Triggers/AmountLess.php @@ -22,12 +22,10 @@ use Log; */ class AmountLess implements TriggerInterface { - /** @var RuleTrigger */ - protected $trigger; - /** @var TransactionJournal */ protected $journal; - + /** @var RuleTrigger */ + protected $trigger; /** * TriggerInterface constructor. @@ -41,6 +39,18 @@ class AmountLess implements TriggerInterface $this->journal = $journal; } + /** + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything + * + * @return bool + */ + public function matchesAnything() + { + return false; + } + /** * @return bool */ @@ -62,16 +72,4 @@ class AmountLess implements TriggerInterface return false; } - - /** - * @{inheritdoc} - * - * @see TriggerInterface::matchesAnything - * - * @return bool - */ - public function matchesAnything() - { - return false; - } } diff --git a/app/Rules/Triggers/AmountMore.php b/app/Rules/Triggers/AmountMore.php index 583d893ddc..27ea6e02c3 100644 --- a/app/Rules/Triggers/AmountMore.php +++ b/app/Rules/Triggers/AmountMore.php @@ -22,12 +22,10 @@ use Log; */ class AmountMore implements TriggerInterface { - /** @var RuleTrigger */ - protected $trigger; - /** @var TransactionJournal */ protected $journal; - + /** @var RuleTrigger */ + protected $trigger; /** * TriggerInterface constructor. @@ -41,6 +39,18 @@ class AmountMore implements TriggerInterface $this->journal = $journal; } + /** + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything + * + * @return bool + */ + public function matchesAnything() + { + return bccomp('0', $this->trigger->trigger_value) === 0; + } + /** * @return bool */ @@ -62,16 +72,4 @@ class AmountMore implements TriggerInterface return false; } - - /** - * @{inheritdoc} - * - * @see TriggerInterface::matchesAnything - * - * @return bool - */ - public function matchesAnything() - { - return bccomp('0', $this->trigger->trigger_value) === 0; - } } diff --git a/app/Rules/Triggers/DescriptionContains.php b/app/Rules/Triggers/DescriptionContains.php index cd639078c0..e0949a60ee 100644 --- a/app/Rules/Triggers/DescriptionContains.php +++ b/app/Rules/Triggers/DescriptionContains.php @@ -22,12 +22,10 @@ use Log; */ class DescriptionContains implements TriggerInterface { - /** @var RuleTrigger */ - protected $trigger; - /** @var TransactionJournal */ protected $journal; - + /** @var RuleTrigger */ + protected $trigger; /** * TriggerInterface constructor. @@ -41,6 +39,18 @@ class DescriptionContains implements TriggerInterface $this->journal = $journal; } + /** + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything + * + * @return bool + */ + public function matchesAnything() + { + return $this->trigger->trigger_value === ""; + } + /** * @return bool */ @@ -63,16 +73,4 @@ class DescriptionContains implements TriggerInterface return false; } - - /** - * @{inheritdoc} - * - * @see TriggerInterface::matchesAnything - * - * @return bool - */ - public function matchesAnything() - { - return $this->trigger->trigger_value === ""; - } } diff --git a/app/Rules/Triggers/DescriptionEnds.php b/app/Rules/Triggers/DescriptionEnds.php index 0eb3c99f83..406fb385b9 100644 --- a/app/Rules/Triggers/DescriptionEnds.php +++ b/app/Rules/Triggers/DescriptionEnds.php @@ -21,12 +21,10 @@ use Log; */ class DescriptionEnds implements TriggerInterface { - /** @var RuleTrigger */ - protected $trigger; - /** @var TransactionJournal */ protected $journal; - + /** @var RuleTrigger */ + protected $trigger; /** * TriggerInterface constructor. @@ -40,6 +38,18 @@ class DescriptionEnds implements TriggerInterface $this->journal = $journal; } + /** + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything + * + * @return bool + */ + public function matchesAnything() + { + return $this->trigger->trigger_value === ""; + } + /** * @return bool */ @@ -73,16 +83,4 @@ class DescriptionEnds implements TriggerInterface } - /** - * @{inheritdoc} - * - * @see TriggerInterface::matchesAnything - * - * @return bool - */ - public function matchesAnything() - { - return $this->trigger->trigger_value === ""; - } - } diff --git a/app/Rules/Triggers/DescriptionIs.php b/app/Rules/Triggers/DescriptionIs.php index 5d1011bf6a..f6cedfd335 100644 --- a/app/Rules/Triggers/DescriptionIs.php +++ b/app/Rules/Triggers/DescriptionIs.php @@ -21,12 +21,10 @@ use Log; */ class DescriptionIs implements TriggerInterface { - /** @var RuleTrigger */ - protected $trigger; - /** @var TransactionJournal */ protected $journal; - + /** @var RuleTrigger */ + protected $trigger; /** * TriggerInterface constructor. @@ -40,6 +38,18 @@ class DescriptionIs implements TriggerInterface $this->journal = $journal; } + /** + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything + * + * @return bool + */ + public function matchesAnything() + { + return false; + } + /** * @return bool */ @@ -59,16 +69,4 @@ class DescriptionIs implements TriggerInterface } - /** - * @{inheritdoc} - * - * @see TriggerInterface::matchesAnything - * - * @return bool - */ - public function matchesAnything() - { - return false; - } - } diff --git a/app/Rules/Triggers/FromAccountContains.php b/app/Rules/Triggers/FromAccountContains.php index e7bd31ed5d..c7e28041ba 100644 --- a/app/Rules/Triggers/FromAccountContains.php +++ b/app/Rules/Triggers/FromAccountContains.php @@ -21,12 +21,10 @@ use Log; */ class FromAccountContains implements TriggerInterface { - /** @var RuleTrigger */ - protected $trigger; - /** @var TransactionJournal */ protected $journal; - + /** @var RuleTrigger */ + protected $trigger; /** * TriggerInterface constructor. @@ -40,6 +38,18 @@ class FromAccountContains implements TriggerInterface $this->journal = $journal; } + /** + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything + * + * @return bool + */ + public function matchesAnything() + { + return $this->trigger->trigger_value === ""; + } + /** * @return bool */ @@ -63,16 +73,4 @@ class FromAccountContains implements TriggerInterface } - /** - * @{inheritdoc} - * - * @see TriggerInterface::matchesAnything - * - * @return bool - */ - public function matchesAnything() - { - return $this->trigger->trigger_value === ""; - } - } diff --git a/app/Rules/Triggers/FromAccountEnds.php b/app/Rules/Triggers/FromAccountEnds.php index bba52c85c5..79659fad98 100644 --- a/app/Rules/Triggers/FromAccountEnds.php +++ b/app/Rules/Triggers/FromAccountEnds.php @@ -21,12 +21,10 @@ use Log; */ class FromAccountEnds implements TriggerInterface { - /** @var RuleTrigger */ - protected $trigger; - /** @var TransactionJournal */ protected $journal; - + /** @var RuleTrigger */ + protected $trigger; /** * TriggerInterface constructor. @@ -40,6 +38,18 @@ class FromAccountEnds implements TriggerInterface $this->journal = $journal; } + /** + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything + * + * @return bool + */ + public function matchesAnything() + { + return $this->trigger->trigger_value === ""; + } + /** * @return bool */ @@ -73,17 +83,4 @@ class FromAccountEnds implements TriggerInterface } - - /** - * @{inheritdoc} - * - * @see TriggerInterface::matchesAnything - * - * @return bool - */ - public function matchesAnything() - { - return $this->trigger->trigger_value === ""; - } - } diff --git a/app/Rules/Triggers/FromAccountIs.php b/app/Rules/Triggers/FromAccountIs.php index 90d38ca6d9..07265484f8 100644 --- a/app/Rules/Triggers/FromAccountIs.php +++ b/app/Rules/Triggers/FromAccountIs.php @@ -21,12 +21,10 @@ use Log; */ class FromAccountIs implements TriggerInterface { - /** @var RuleTrigger */ - protected $trigger; - /** @var TransactionJournal */ protected $journal; - + /** @var RuleTrigger */ + protected $trigger; /** * TriggerInterface constructor. @@ -40,6 +38,18 @@ class FromAccountIs implements TriggerInterface $this->journal = $journal; } + /** + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything + * + * @return bool + */ + public function matchesAnything() + { + return false; + } + /** * @return bool */ @@ -59,16 +69,4 @@ class FromAccountIs implements TriggerInterface } - /** - * @{inheritdoc} - * - * @see TriggerInterface::matchesAnything - * - * @return bool - */ - public function matchesAnything() - { - return false; - } - } diff --git a/app/Rules/Triggers/FromAccountStarts.php b/app/Rules/Triggers/FromAccountStarts.php index 43a8f5b7a0..7588769ed4 100644 --- a/app/Rules/Triggers/FromAccountStarts.php +++ b/app/Rules/Triggers/FromAccountStarts.php @@ -21,12 +21,10 @@ use Log; */ class FromAccountStarts implements TriggerInterface { - /** @var RuleTrigger */ - protected $trigger; - /** @var TransactionJournal */ protected $journal; - + /** @var RuleTrigger */ + protected $trigger; /** * TriggerInterface constructor. @@ -40,6 +38,18 @@ class FromAccountStarts implements TriggerInterface $this->journal = $journal; } + /** + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything + * + * @return bool + */ + public function matchesAnything() + { + return $this->trigger->trigger_value === ""; + } + /** * @return bool */ @@ -61,17 +71,4 @@ class FromAccountStarts implements TriggerInterface } - - /** - * @{inheritdoc} - * - * @see TriggerInterface::matchesAnything - * - * @return bool - */ - public function matchesAnything() - { - return $this->trigger->trigger_value === ""; - } - } diff --git a/app/Rules/Triggers/ToAccountContains.php b/app/Rules/Triggers/ToAccountContains.php index c219437e8b..b55d592bec 100644 --- a/app/Rules/Triggers/ToAccountContains.php +++ b/app/Rules/Triggers/ToAccountContains.php @@ -21,12 +21,10 @@ use Log; */ class ToAccountContains implements TriggerInterface { - /** @var RuleTrigger */ - protected $trigger; - /** @var TransactionJournal */ protected $journal; - + /** @var RuleTrigger */ + protected $trigger; /** * TriggerInterface constructor. @@ -40,6 +38,18 @@ class ToAccountContains implements TriggerInterface $this->journal = $journal; } + /** + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything + * + * @return bool + */ + public function matchesAnything() + { + return $this->trigger->trigger_value === ""; + } + /** * @return bool */ @@ -63,17 +73,4 @@ class ToAccountContains implements TriggerInterface } - - /** - * @{inheritdoc} - * - * @see TriggerInterface::matchesAnything - * - * @return bool - */ - public function matchesAnything() - { - return $this->trigger->trigger_value === ""; - } - } diff --git a/app/Rules/Triggers/ToAccountEnds.php b/app/Rules/Triggers/ToAccountEnds.php index 5591ffcca8..0dc9d757c8 100644 --- a/app/Rules/Triggers/ToAccountEnds.php +++ b/app/Rules/Triggers/ToAccountEnds.php @@ -21,12 +21,10 @@ use Log; */ class ToAccountEnds implements TriggerInterface { - /** @var RuleTrigger */ - protected $trigger; - /** @var TransactionJournal */ protected $journal; - + /** @var RuleTrigger */ + protected $trigger; /** * TriggerInterface constructor. @@ -40,6 +38,18 @@ class ToAccountEnds implements TriggerInterface $this->journal = $journal; } + /** + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything + * + * @return bool + */ + public function matchesAnything() + { + return $this->trigger->trigger_value === ""; + } + /** * @return bool */ @@ -72,17 +82,4 @@ class ToAccountEnds implements TriggerInterface return false; } - - - /** - * @{inheritdoc} - * - * @see TriggerInterface::matchesAnything - * - * @return bool - */ - public function matchesAnything() - { - return $this->trigger->trigger_value === ""; - } } diff --git a/app/Rules/Triggers/ToAccountIs.php b/app/Rules/Triggers/ToAccountIs.php index 4596ef9fde..1d389ca5dd 100644 --- a/app/Rules/Triggers/ToAccountIs.php +++ b/app/Rules/Triggers/ToAccountIs.php @@ -21,12 +21,10 @@ use Log; */ class ToAccountIs implements TriggerInterface { - /** @var RuleTrigger */ - protected $trigger; - /** @var TransactionJournal */ protected $journal; - + /** @var RuleTrigger */ + protected $trigger; /** * TriggerInterface constructor. @@ -40,6 +38,18 @@ class ToAccountIs implements TriggerInterface $this->journal = $journal; } + /** + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything + * + * @return bool + */ + public function matchesAnything() + { + return false; + } + /** * @return bool */ @@ -59,16 +69,4 @@ class ToAccountIs implements TriggerInterface } - /** - * @{inheritdoc} - * - * @see TriggerInterface::matchesAnything - * - * @return bool - */ - public function matchesAnything() - { - return false; - } - } diff --git a/app/Rules/Triggers/ToAccountStarts.php b/app/Rules/Triggers/ToAccountStarts.php index d4423e931a..e9d5b8caa7 100644 --- a/app/Rules/Triggers/ToAccountStarts.php +++ b/app/Rules/Triggers/ToAccountStarts.php @@ -21,12 +21,10 @@ use Log; */ class ToAccountStarts implements TriggerInterface { - /** @var RuleTrigger */ - protected $trigger; - /** @var TransactionJournal */ protected $journal; - + /** @var RuleTrigger */ + protected $trigger; /** * TriggerInterface constructor. @@ -40,6 +38,18 @@ class ToAccountStarts implements TriggerInterface $this->journal = $journal; } + /** + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything + * + * @return bool + */ + public function matchesAnything() + { + return $this->trigger->trigger_value === ""; + } + /** * @return bool */ @@ -61,17 +71,4 @@ class ToAccountStarts implements TriggerInterface } - - /** - * @{inheritdoc} - * - * @see TriggerInterface::matchesAnything - * - * @return bool - */ - public function matchesAnything() - { - return $this->trigger->trigger_value === ""; - } - } diff --git a/app/Rules/Triggers/TransactionType.php b/app/Rules/Triggers/TransactionType.php index 3a06c13f30..4a47604a66 100644 --- a/app/Rules/Triggers/TransactionType.php +++ b/app/Rules/Triggers/TransactionType.php @@ -21,12 +21,10 @@ use Log; */ class TransactionType implements TriggerInterface { - /** @var RuleTrigger */ - protected $trigger; - /** @var TransactionJournal */ protected $journal; - + /** @var RuleTrigger */ + protected $trigger; /** * TriggerInterface constructor. @@ -40,6 +38,18 @@ class TransactionType implements TriggerInterface $this->journal = $journal; } + /** + * @{inheritdoc} + * + * @see TriggerInterface::matchesAnything + * + * @return bool + */ + public function matchesAnything() + { + return false; + } + /** * @return bool */ @@ -58,16 +68,4 @@ class TransactionType implements TriggerInterface return false; } - /** - * @{inheritdoc} - * - * @see TriggerInterface::matchesAnything - * - * @return bool - */ - public function matchesAnything() - { - return false; - } - } diff --git a/app/Rules/Triggers/TriggerFactory.php b/app/Rules/Triggers/TriggerFactory.php index 157861889c..116670e9ff 100644 --- a/app/Rules/Triggers/TriggerFactory.php +++ b/app/Rules/Triggers/TriggerFactory.php @@ -23,6 +23,22 @@ class TriggerFactory { protected static $triggerTypes = null; + /** + * 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 the class name to be used for triggers with the given name * @@ -47,22 +63,6 @@ class TriggerFactory 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 */ diff --git a/app/Rules/Triggers/UserAction.php b/app/Rules/Triggers/UserAction.php index 55ab4dbfda..c9fdcc78a2 100644 --- a/app/Rules/Triggers/UserAction.php +++ b/app/Rules/Triggers/UserAction.php @@ -21,12 +21,10 @@ use Log; */ class UserAction implements TriggerInterface { - /** @var RuleTrigger */ - protected $trigger; - /** @var TransactionJournal */ protected $journal; - + /** @var RuleTrigger */ + protected $trigger; /** * TriggerInterface constructor. @@ -41,18 +39,6 @@ class UserAction implements TriggerInterface } - /** - * This trigger is always triggered, because the rule that it is a part of has been pre-selected on this condition. - * - * @return bool - */ - public function triggered() - { - Log::debug('user_action always returns true.'); - - return true; - } - /** * @{inheritdoc} * @@ -65,4 +51,16 @@ class UserAction implements TriggerInterface return true; } + /** + * This trigger is always triggered, because the rule that it is a part of has been pre-selected on this condition. + * + * @return bool + */ + public function triggered() + { + Log::debug('user_action always returns true.'); + + return true; + } + } From 317aa591c39cd1bd180953ce1dca5ee6463e319e Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 17 Feb 2016 15:46:58 +0100 Subject: [PATCH 10/11] Need to "use" the TriggerFactory! --- app/Models/RuleTrigger.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/app/Models/RuleTrigger.php b/app/Models/RuleTrigger.php index cf13c918d7..365e873722 100644 --- a/app/Models/RuleTrigger.php +++ b/app/Models/RuleTrigger.php @@ -10,6 +10,7 @@ declare(strict_types = 1); namespace FireflyIII\Models; +use FireflyIII\Rules\Triggers\TriggerFactory; use Illuminate\Database\Eloquent\Model; /** @@ -29,6 +30,15 @@ use Illuminate\Database\Eloquent\Model; */ class RuleTrigger extends Model { + /** + * Checks whether this trigger will match all transactions + * For example: amount > 0 or description starts with '' + */ + public function matchesAnything() + { + return TriggerFactory::getTrigger($this, new TransactionJournal)->matchesAnything(); + } + /** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ @@ -36,12 +46,4 @@ class RuleTrigger extends Model { return $this->belongsTo('FireflyIII\Models\Rule'); } - - /** - * Checks whether this trigger will match all transactions - * For example: amount > 0 or description starts with '' - */ - public function matchesAnything() { - return TriggerFactory::getTrigger($this, new TransactionJournal)->matchesAnything(); - } } From 1a110de5979c47f59036f3f065d41d4f010c7064 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 17 Feb 2016 15:52:46 +0100 Subject: [PATCH 11/11] Throw errors instead of abort() --- app/Http/Controllers/Auth/AuthController.php | 3 +- app/Http/Controllers/JsonController.php | 3 +- app/Http/Requests/JournalFormRequest.php | 4 +- .../Account/AccountRepository.php | 5 +- .../Journal/JournalRepository.php | 5 +- app/Rules/Actions/ActionFactory.php | 67 +++++++++++-------- app/Rules/Triggers/TriggerFactory.php | 6 +- 7 files changed, 55 insertions(+), 38 deletions(-) diff --git a/app/Http/Controllers/Auth/AuthController.php b/app/Http/Controllers/Auth/AuthController.php index 7bf7a47d82..93376784cd 100644 --- a/app/Http/Controllers/Auth/AuthController.php +++ b/app/Http/Controllers/Auth/AuthController.php @@ -4,6 +4,7 @@ declare(strict_types = 1); namespace FireflyIII\Http\Controllers\Auth; use Auth; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Models\Role; use FireflyIII\User; @@ -151,7 +152,7 @@ class AuthController extends Controller return redirect($this->redirectPath()); } - abort(500, 'Not a user!'); + throw new FireflyException('The authenticated user object is invalid.'); return redirect($this->redirectPath()); diff --git a/app/Http/Controllers/JsonController.php b/app/Http/Controllers/JsonController.php index 78fbc0ec4f..8e11142d10 100644 --- a/app/Http/Controllers/JsonController.php +++ b/app/Http/Controllers/JsonController.php @@ -3,6 +3,7 @@ use Amount; use Carbon\Carbon; use Config; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Helpers\Report\ReportQueryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI; use FireflyIII\Repositories\Bill\BillRepositoryInterface; @@ -247,7 +248,7 @@ class JsonController extends Controller { $pref = Preferences::get('tour', true); if (!$pref) { - abort(404); + throw new FireflyException('Cannot find preference for tour. Exit.'); } $headers = ['main-content', 'sidebar-toggle', 'account-menu', 'budget-menu', 'report-menu', 'transaction-menu', 'option-menu', 'main-content-end']; $steps = []; diff --git a/app/Http/Requests/JournalFormRequest.php b/app/Http/Requests/JournalFormRequest.php index ee9fdf0788..060a41dbb8 100644 --- a/app/Http/Requests/JournalFormRequest.php +++ b/app/Http/Requests/JournalFormRequest.php @@ -6,6 +6,7 @@ namespace FireflyIII\Http\Requests; use Auth; use Carbon\Carbon; use Exception; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\TransactionType; use Input; @@ -32,6 +33,7 @@ class JournalFormRequest extends Request public function getJournalData() { $tags = $this->get('tags') ?? ''; + return [ 'what' => $this->get('what'), 'description' => $this->get('description'), @@ -86,7 +88,7 @@ class JournalFormRequest extends Request $rules['category'] = 'between:1,255'; break; default: - abort(500, 'Cannot handle ' . $what); + throw new FireflyException('Cannot handle transaction type of type ' . e($what) . '.'); break; } diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index 99cf93bff8..7a90bb7386 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -7,6 +7,7 @@ use Auth; use Carbon\Carbon; use Config; use DB; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Account; use FireflyIII\Models\AccountMeta; use FireflyIII\Models\AccountType; @@ -482,7 +483,9 @@ class AccountRepository implements AccountRepositoryInterface $existingAccount = Account::firstOrNullEncrypted($searchData); if (!$existingAccount) { Log::error('Account create error: ' . $newAccount->getErrors()->toJson()); - abort(500); + throw new FireflyException( + 'Cannot create a new account. See also the log files. First error is: ' . e($newAccount->getErrors()->first()) . '.' + ); } $newAccount = $existingAccount; diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php index 6184ff9d47..c11f3a4a97 100644 --- a/app/Repositories/Journal/JournalRepository.php +++ b/app/Repositories/Journal/JournalRepository.php @@ -6,6 +6,7 @@ namespace FireflyIII\Repositories\Journal; use Auth; use Carbon\Carbon; use DB; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; use FireflyIII\Models\Budget; @@ -349,14 +350,14 @@ class JournalRepository implements JournalRepositoryInterface if (is_null($toAccount)) { Log::error('"to"-account is null, so we cannot continue!'); - abort(500, '"to"-account is null, so we cannot continue!'); + throw new FireflyException('"to"-account is null, so we cannot continue!'); // @codeCoverageIgnoreStart } // @codeCoverageIgnoreEnd if (is_null($fromAccount)) { Log::error('"from"-account is null, so we cannot continue!'); - abort(500, '"from"-account is null, so we cannot continue!'); + throw new FireflyException('"from"-account is null, so we cannot continue!'); // @codeCoverageIgnoreStart } diff --git a/app/Rules/Actions/ActionFactory.php b/app/Rules/Actions/ActionFactory.php index 43161b56a1..5c525e79af 100644 --- a/app/Rules/Actions/ActionFactory.php +++ b/app/Rules/Actions/ActionFactory.php @@ -10,6 +10,7 @@ declare(strict_types = 1); namespace FireflyIII\Rules\Actions; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\RuleAction; use FireflyIII\Models\TransactionJournal; use FireflyIII\Support\Domain; @@ -22,48 +23,56 @@ use FireflyIII\Support\Domain; 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 RuleAction $action * @param TransactionJournal $journal + * * @return ActionInterface */ - public static function getAction(RuleAction $action, TransactionJournal $journal): ActionInterface { + public static function getAction(RuleAction $action, TransactionJournal $journal): ActionInterface + { $actionType = $action->action_type; - $class = self::getActionClass($actionType); - + $class = self::getActionClass($actionType); + return new $class($action, $journal); } - + + /** + * Returns the class name to be used for actions with the given name + * + * @param string $actionType + * + * @return ActionInterface|string + * @throws FireflyException + */ + public static function getActionClass(string $actionType): string + { + $actionTypes = self::getActionTypes(); + + if (!array_key_exists($actionType, $actionTypes)) { + throw new FireflyException('No such action exists ("' . e($actionType) . '").'); + } + + $class = $actionTypes[$actionType]; + if (!class_exists($class)) { + throw new FireflyException('Could not instantiate class for rule action type "' . e($actionType) . '" (' . e($class) . ').'); + } + + return $class; + } + /** * Returns a map with actiontypes, mapped to the class representing that type */ - protected static function getActionTypes() { - if( !self::$actionTypes ) { + protected static function getActionTypes() + { + if (!self::$actionTypes) { self::$actionTypes = Domain::getRuleActions(); } - + return self::$actionTypes; } } diff --git a/app/Rules/Triggers/TriggerFactory.php b/app/Rules/Triggers/TriggerFactory.php index 116670e9ff..2eaf47e354 100644 --- a/app/Rules/Triggers/TriggerFactory.php +++ b/app/Rules/Triggers/TriggerFactory.php @@ -10,6 +10,7 @@ declare(strict_types = 1); namespace FireflyIII\Rules\Triggers; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\RuleTrigger; use FireflyIII\Models\TransactionJournal; use FireflyIII\Support\Domain; @@ -51,13 +52,12 @@ class TriggerFactory $triggerTypes = self::getTriggerTypes(); if (!array_key_exists($triggerType, $triggerTypes)) { - abort(500, 'No such trigger exists ("' . $triggerType . '").'); + throw new FireflyException('No such trigger exists ("' . e($triggerType) . '").'); } - /** @var TriggerInterface $class */ $class = $triggerTypes[$triggerType]; if (!class_exists($class)) { - abort(500, 'Could not instantiate class for rule trigger type "' . $triggerType . '" (' . $class . ').'); + throw new FireflyException('Could not instantiate class for rule trigger type "' . e($triggerType) . '" (' . e($class) . ').'); } return $class;