firefly-iii/app/TransactionRules/Factory/TriggerFactory.php

123 lines
4.3 KiB
PHP
Raw Normal View History

<?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);
2016-02-17 23:34:16 -06:00
2017-09-13 00:49:58 -05:00
namespace FireflyIII\TransactionRules\Factory;
2016-02-17 08:52:46 -06:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\RuleTrigger;
2017-09-13 00:49:58 -05:00
use FireflyIII\TransactionRules\Triggers\AbstractTrigger;
use FireflyIII\TransactionRules\Triggers\TriggerInterface;
use FireflyIII\Support\Domain;
use Log;
/**
* Interface TriggerInterface
*
2017-09-13 00:49:58 -05:00
* @package FireflyIII\TransactionRules\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
2017-09-13 00:49:58 -05:00
* (TransactionRules/Triggers/[object]) for a given RuleTrigger (database object). If for example the database object
2016-02-17 23:45:10 -06:00
* 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
*
* @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 */
2017-03-04 08:29:20 -06:00
$class = self::getTriggerClass($triggerType);
$obj = $class::makeFromTriggerValue($trigger->trigger_value);
$obj->stopProcessing = $trigger->stop_processing;
2016-02-17 13:19:44 -06:00
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)));
2016-02-17 13:19:44 -06:00
return $obj;
2016-02-17 08:38:21 -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).
*
* @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
* @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;
}
/**
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 23:45:10 -06:00
protected static function getTriggerTypes(): array
{
2016-02-17 23:45:10 -06:00
if (count(self::$triggerTypes) === 0) {
self::$triggerTypes = Domain::getRuleTriggers();
}
return self::$triggerTypes;
}
/**
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
*
* @param string $triggerType
2016-02-17 08:29:26 -06:00
*
* @return TriggerInterface|string
* @throws FireflyException
*/
private static function getTriggerClass(string $triggerType): string
2016-02-17 08:29:26 -06:00
{
$triggerTypes = self::getTriggerTypes();
2016-02-17 08:29:26 -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 08:29:26 -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 08:29:26 -06:00
return $class;
}
}