2016-02-17 06:13:23 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* TriggerFactory.php
|
2017-11-15 05:25:49 -06:00
|
|
|
* Copyright (C) 2016 Robert Horlings.
|
2016-02-17 06:13:23 -06:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* This file is part of Firefly III.
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-02-17 06:13:23 -06:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
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 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;
|
2017-09-14 10:40:02 -05:00
|
|
|
use FireflyIII\Support\Domain;
|
2017-09-13 00:49:58 -05:00
|
|
|
use FireflyIII\TransactionRules\Triggers\AbstractTrigger;
|
|
|
|
use FireflyIII\TransactionRules\Triggers\TriggerInterface;
|
2016-09-12 11:43:10 -05:00
|
|
|
use Log;
|
2016-02-17 06:13:23 -06:00
|
|
|
|
|
|
|
/**
|
2017-10-06 08:41:21 -05:00
|
|
|
* @codeCoverageIgnore
|
2017-11-25 08:20:53 -06:00
|
|
|
*
|
|
|
|
* Class TriggerFactory can create triggers.
|
2016-02-17 06:13:23 -06:00
|
|
|
*/
|
|
|
|
class TriggerFactory
|
|
|
|
{
|
2017-11-25 08:20:53 -06:00
|
|
|
/** @var array array with trigger types */
|
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
|
|
|
*
|
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 */
|
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
|
|
|
|
2016-09-21 13:30:09 -05: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 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
|
2017-11-15 05:25:49 -06:00
|
|
|
*
|
2016-02-17 14:03:59 -06:00
|
|
|
* @return AbstractTrigger
|
2017-11-15 05:25:49 -06:00
|
|
|
*
|
2016-02-17 14:03:59 -06:00
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public static function makeTriggerFromStrings(string $triggerType, string $triggerValue, bool $stopProcessing)
|
|
|
|
{
|
|
|
|
/** @var AbstractTrigger $class */
|
|
|
|
$class = self::getTriggerClass($triggerType);
|
|
|
|
$obj = $class::makeFromStrings($triggerValue, $stopProcessing);
|
2016-09-12 11:43:10 -05:00
|
|
|
Log::debug('Created trigger from string', ['type' => $triggerType, 'value' => $triggerValue, 'stopProcessing' => $stopProcessing, 'class' => $class]);
|
2016-02-17 14:03:59 -06:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2017-11-15 05:25:49 -06:00
|
|
|
if (0 === count(self::$triggerTypes)) {
|
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
|
2017-11-15 05:25:49 -06:00
|
|
|
*
|
2016-02-17 12:07:20 -06:00
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
}
|