Change namespace of rules.

This commit is contained in:
James Cole
2017-09-13 07:49:58 +02:00
parent 0583aa53a9
commit 233305ecb4
55 changed files with 108 additions and 148 deletions

View File

@@ -0,0 +1,88 @@
<?php
/**
* ActionFactory.php
* Copyright (C) 2016 Robert Horlings
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Factory;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\RuleAction;
use FireflyIII\TransactionRules\Actions\ActionInterface;
use FireflyIII\Support\Domain;
use Log;
/**
* Interface ActionInterface
*
* @package FireflyIII\TransactionRules\Actions
*/
class ActionFactory
{
protected static $actionTypes = [];
/**
* This method returns the actual implementation (TransactionRules/Actions/[object]) for a given
* RuleAction (database object). If for example the database object contains action_type "change_category"
* with value "Groceries" this method will return a corresponding SetCategory object preset
* to "Groceries". Any transaction journal then fed to this object will have its category changed.
*
* @param RuleAction $action
*
* @return ActionInterface
*/
public static function getAction(RuleAction $action): ActionInterface
{
$class = self::getActionClass($action->action_type);
Log::debug(sprintf('self::getActionClass("%s") = "%s"', $action->action_type, $class));
return new $class($action);
}
/**
* Returns the class name to be used for actions with the given name. This is a lookup function
* that will match the given action type (ie. "change_category") to the matching class name
* (SetCategory) using the configuration (firefly.php).
*
* @param string $actionType
*
* @return 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
*
* @return array
*/
protected static function getActionTypes(): array
{
if (count(self::$actionTypes) === 0) {
self::$actionTypes = Domain::getRuleActions();
}
return self::$actionTypes;
}
}

View File

@@ -0,0 +1,122 @@
<?php
/**
* TriggerFactory.php
* Copyright (C) 2016 Robert Horlings
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Factory;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\RuleTrigger;
use FireflyIII\TransactionRules\Triggers\AbstractTrigger;
use FireflyIII\TransactionRules\Triggers\TriggerInterface;
use FireflyIII\Support\Domain;
use Log;
/**
* Interface TriggerInterface
*
* @package FireflyIII\TransactionRules\Triggers
*/
class TriggerFactory
{
protected static $triggerTypes = [];
/**
* Returns the trigger for the given type and journal. This method returns the actual implementation
* (TransactionRules/Triggers/[object]) for a given RuleTrigger (database object). If for example the database object
* contains trigger_type "description_is" with value "Rent" this method will return a corresponding
* DescriptionIs object preset to "Rent". Any transaction journal then fed to this object will
* be triggered if its description actually is "Rent".
*
* @param RuleTrigger $trigger
*
* @return AbstractTrigger
*/
public static function getTrigger(RuleTrigger $trigger)
{
$triggerType = $trigger->trigger_type;
/** @var AbstractTrigger $class */
$class = self::getTriggerClass($triggerType);
$obj = $class::makeFromTriggerValue($trigger->trigger_value);
$obj->stopProcessing = $trigger->stop_processing;
Log::debug(sprintf('self::getTriggerClass("%s") = "%s"', $triggerType, $class));
Log::debug(sprintf('%s::makeFromTriggerValue(%s) = object of class "%s"', $class, $trigger->trigger_value, get_class($obj)));
return $obj;
}
/**
* This method is equal to TriggerFactory::getTrigger but accepts a textual representation of the trigger type
* (for example "description_is"), the trigger value ("Rent") and whether or not Firefly III should stop processing
* other triggers (if present) after this trigger.
*
* This method is used when the RuleTriggers from TriggerFactory::getTrigger do not exist (yet).
*
* @param string $triggerType
* @param string $triggerValue
* @param bool $stopProcessing
*
* @see TriggerFactory::getTrigger
* @return AbstractTrigger
* @throws FireflyException
*/
public static function makeTriggerFromStrings(string $triggerType, string $triggerValue, bool $stopProcessing)
{
/** @var AbstractTrigger $class */
$class = self::getTriggerClass($triggerType);
$obj = $class::makeFromStrings($triggerValue, $stopProcessing);
Log::debug('Created trigger from string', ['type' => $triggerType, 'value' => $triggerValue, 'stopProcessing' => $stopProcessing, 'class' => $class]);
return $obj;
}
/**
* Returns a map with trigger types, mapped to the class representing that type.
*
* @return array
*/
protected static function getTriggerTypes(): array
{
if (count(self::$triggerTypes) === 0) {
self::$triggerTypes = Domain::getRuleTriggers();
}
return self::$triggerTypes;
}
/**
* Returns the class name to be used for triggers with the given name. This is a lookup function
* that will match the given trigger type (ie. "from_account_ends") to the matching class name
* (FromAccountEnds) using the configuration (firefly.php).
*
* @param string $triggerType
*
* @return TriggerInterface|string
* @throws FireflyException
*/
private 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;
}
}