2016-02-17 06:13:23 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-02-17 23:34:16 -06:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Rules\Factory;
|
2016-02-17 06:13:23 -06:00
|
|
|
|
2016-02-17 08:52:46 -06:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2016-02-17 06:13:23 -06:00
|
|
|
use FireflyIII\Models\RuleTrigger;
|
2016-02-17 23:34:16 -06:00
|
|
|
use FireflyIII\Rules\Triggers\AbstractTrigger;
|
|
|
|
use FireflyIII\Rules\Triggers\TriggerInterface;
|
2016-02-17 06:13:23 -06:00
|
|
|
use FireflyIII\Support\Domain;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interface TriggerInterface
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Rules\Triggers
|
|
|
|
*/
|
|
|
|
class TriggerFactory
|
|
|
|
{
|
2016-02-17 23:45:10 -06:00
|
|
|
protected static $triggerTypes = [];
|
2016-02-17 08:29:26 -06:00
|
|
|
|
2016-02-17 08:38:21 -06:00
|
|
|
/**
|
2016-02-17 23:45:10 -06:00
|
|
|
* Returns the trigger for the given type and journal. This method returns the actual implementation
|
|
|
|
* (Rules/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".
|
2016-02-17 08:38:21 -06:00
|
|
|
*
|
2016-02-17 12:07:20 -06:00
|
|
|
* @param RuleTrigger $trigger
|
2016-02-17 08:38:21 -06:00
|
|
|
*
|
2016-02-17 13:19:44 -06:00
|
|
|
* @return AbstractTrigger
|
2016-02-17 08:38:21 -06:00
|
|
|
*/
|
2016-02-17 13:19:44 -06:00
|
|
|
public static function getTrigger(RuleTrigger $trigger)
|
2016-02-17 08:38:21 -06:00
|
|
|
{
|
|
|
|
$triggerType = $trigger->trigger_type;
|
|
|
|
|
2016-02-17 13:19:44 -06:00
|
|
|
/** @var AbstractTrigger $class */
|
|
|
|
$class = self::getTriggerClass($triggerType);
|
|
|
|
$obj = $class::makeFromTriggerValue($trigger->trigger_value);
|
|
|
|
|
|
|
|
return $obj;
|
2016-02-17 08:38:21 -06:00
|
|
|
}
|
|
|
|
|
2016-02-17 14:03:59 -06:00
|
|
|
/**
|
2016-02-17 23:45:10 -06:00
|
|
|
* 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).
|
|
|
|
*
|
2016-02-17 14:03:59 -06:00
|
|
|
* @param string $triggerType
|
|
|
|
* @param string $triggerValue
|
2016-02-17 14:14:32 -06:00
|
|
|
* @param bool $stopProcessing
|
|
|
|
*
|
2016-02-17 23:45:10 -06:00
|
|
|
* @see TriggerFactory::getTrigger
|
2016-02-17 14:03:59 -06:00
|
|
|
* @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);
|
|
|
|
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-17 23:45:10 -06:00
|
|
|
* Returns a map with trigger types, mapped to the class representing that type.
|
|
|
|
*
|
|
|
|
* @return array
|
2016-02-17 14:03:59 -06:00
|
|
|
*/
|
2016-02-17 23:45:10 -06:00
|
|
|
protected static function getTriggerTypes(): array
|
2016-02-17 14:03:59 -06:00
|
|
|
{
|
2016-02-17 23:45:10 -06:00
|
|
|
if (count(self::$triggerTypes) === 0) {
|
2016-02-17 14:03:59 -06:00
|
|
|
self::$triggerTypes = Domain::getRuleTriggers();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$triggerTypes;
|
|
|
|
}
|
|
|
|
|
2016-02-17 06:13:23 -06:00
|
|
|
/**
|
2016-02-17 23:45:10 -06:00
|
|
|
* 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).
|
2016-02-17 08:29:26 -06:00
|
|
|
*
|
2016-02-17 06:13:23 -06:00
|
|
|
* @param string $triggerType
|
2016-02-17 08:29:26 -06:00
|
|
|
*
|
2016-02-17 12:07:20 -06:00
|
|
|
* @return TriggerInterface|string
|
|
|
|
* @throws FireflyException
|
2016-02-17 06:13:23 -06:00
|
|
|
*/
|
2016-02-17 14:03:59 -06:00
|
|
|
private static function getTriggerClass(string $triggerType): string
|
2016-02-17 08:29:26 -06:00
|
|
|
{
|
2016-02-17 06:13:23 -06:00
|
|
|
$triggerTypes = self::getTriggerTypes();
|
2016-02-17 08:29:26 -06:00
|
|
|
|
2016-02-17 06:13:23 -06:00
|
|
|
if (!array_key_exists($triggerType, $triggerTypes)) {
|
2016-02-17 08:52:46 -06:00
|
|
|
throw new FireflyException('No such trigger exists ("' . e($triggerType) . '").');
|
2016-02-17 06:13:23 -06:00
|
|
|
}
|
2016-02-17 08:29:26 -06:00
|
|
|
|
2016-02-17 06:13:23 -06:00
|
|
|
$class = $triggerTypes[$triggerType];
|
|
|
|
if (!class_exists($class)) {
|
2016-02-17 08:52:46 -06:00
|
|
|
throw new FireflyException('Could not instantiate class for rule trigger type "' . e($triggerType) . '" (' . e($class) . ').');
|
2016-02-17 06:13:23 -06:00
|
|
|
}
|
2016-02-17 08:29:26 -06:00
|
|
|
|
2016-02-17 06:13:23 -06:00
|
|
|
return $class;
|
|
|
|
}
|
|
|
|
}
|