Merge branch 'develop' into feature/test-triggers

This commit is contained in:
Robert Horlings
2016-02-17 16:14:07 +01:00
28 changed files with 454 additions and 108 deletions

View File

@@ -0,0 +1,78 @@
<?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\Exceptions\FireflyException;
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 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 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) {
self::$actionTypes = Domain::getRuleActions();
}
return self::$actionTypes;
}
}

View File

@@ -15,8 +15,9 @@ 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\Support\Domain;
use FireflyIII\Rules\Triggers\TriggerFactory;
use Log;
/**
@@ -30,10 +31,6 @@ class Processor
protected $journal;
/** @var Rule */
protected $rule;
/** @var array */
private $actionTypes = [];
/** @var array */
private $triggerTypes = [];
/**
* Processor constructor.
@@ -45,8 +42,6 @@ class Processor
{
$this->rule = $rule;
$this->journal = $journal;
$this->triggerTypes = Domain::getRuleTriggers();
$this->actionTypes = Domain::getRuleActions();
}
/**
@@ -118,14 +113,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;
@@ -137,8 +126,6 @@ class Processor
}
/**
* Method to check whether the current transaction would be triggered
* by the given list of triggers
* @return bool
*/
protected function triggeredBy($triggers) {
@@ -147,29 +134,19 @@ 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++;
}
if ($trigger->stop_processing) {
break;
}
}
Log::debug('Total: ' . $foundTriggers . ' found triggers. ' . $hitTriggers . ' triggers were hit.');
return ($hitTriggers == $foundTriggers);
}

View File

@@ -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
*/

View File

@@ -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
*/

View File

@@ -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
*/

View File

@@ -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
*/

View File

@@ -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
*/
@@ -72,4 +82,5 @@ class DescriptionEnds implements TriggerInterface
return false;
}
}

View File

@@ -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
*/
@@ -58,4 +68,5 @@ class DescriptionIs implements TriggerInterface
return false;
}
}

View File

@@ -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
*/
@@ -60,4 +70,5 @@ class DescriptionStarts implements TriggerInterface
return false;
}
}

View File

@@ -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
*/
@@ -62,4 +72,5 @@ class FromAccountContains implements TriggerInterface
return false;
}
}

View File

@@ -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
*/
@@ -72,4 +82,5 @@ class FromAccountEnds implements TriggerInterface
return false;
}
}

View File

@@ -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
*/
@@ -58,4 +68,5 @@ class FromAccountIs implements TriggerInterface
return false;
}
}

View File

@@ -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
*/
@@ -60,4 +70,5 @@ class FromAccountStarts implements TriggerInterface
return false;
}
}

View File

@@ -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
*/
@@ -62,4 +72,5 @@ class ToAccountContains implements TriggerInterface
return false;
}
}

View File

@@ -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
*/

View File

@@ -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
*/
@@ -58,4 +68,5 @@ class ToAccountIs implements TriggerInterface
return false;
}
}

View File

@@ -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
*/
@@ -60,4 +70,5 @@ class ToAccountStarts implements TriggerInterface
return false;
}
}

View File

@@ -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
*/
@@ -57,4 +67,5 @@ class TransactionType implements TriggerInterface
return false;
}
}

View File

@@ -0,0 +1,77 @@
<?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\Exceptions\FireflyException;
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 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
*
* @param string $triggerType
*
* @return TriggerInterface
*/
public static function getTriggerClass(string $triggerType): string
{
$triggerTypes = self::getTriggerTypes();
if (!array_key_exists($triggerType, $triggerTypes)) {
throw new FireflyException('No such trigger exists ("' . e($triggerType) . '").');
}
$class = $triggerTypes[$triggerType];
if (!class_exists($class)) {
throw new FireflyException('Could not instantiate class for rule trigger type "' . e($triggerType) . '" (' . e($class) . ').');
}
return $class;
}
/**
* 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;
}
}

View File

@@ -28,6 +28,22 @@ interface TriggerInterface
*/
public function __construct(RuleTrigger $trigger, TransactionJournal $journal);
/**
* 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
*/
public function matchesAnything();
/**
* @return bool
*/

View File

@@ -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,6 +39,18 @@ class UserAction implements TriggerInterface
}
/**
* @{inheritdoc}
*
* @see TriggerInterface::matchesAnything
*
* @return bool
*/
public function matchesAnything()
{
return true;
}
/**
* This trigger is always triggered, because the rule that it is a part of has been pre-selected on this condition.
*