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

136 lines
4.8 KiB
PHP
Raw Normal View History

<?php
/**
* TriggerFactory.php
2017-11-15 12:25:49 +01:00
* Copyright (C) 2016 Robert Horlings.
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02: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/>.
*/
declare(strict_types=1);
2016-02-18 06:34:16 +01:00
2017-09-13 07:49:58 +02:00
namespace FireflyIII\TransactionRules\Factory;
2016-02-17 15:52:46 +01:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\RuleTrigger;
2017-09-14 17:40:02 +02:00
use FireflyIII\Support\Domain;
2017-09-13 07:49:58 +02:00
use FireflyIII\TransactionRules\Triggers\AbstractTrigger;
use FireflyIII\TransactionRules\Triggers\TriggerInterface;
use Log;
/**
2017-11-25 15:20:53 +01:00
* Class TriggerFactory can create triggers.
2017-11-25 20:27:53 +01:00
*
* @codeCoverageIgnore
*/
class TriggerFactory
{
2017-11-25 15:20:53 +01:00
/** @var array array with trigger types */
2016-02-18 06:45:10 +01:00
protected static $triggerTypes = [];
2016-02-17 15:29:26 +01:00
2016-02-17 15:38:21 +01:00
/**
2016-02-18 06:45:10 +01:00
* Returns the trigger for the given type and journal. This method returns the actual implementation
2017-09-13 07:49:58 +02:00
* (TransactionRules/Triggers/[object]) for a given RuleTrigger (database object). If for example the database object
2016-02-18 06:45:10 +01: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 15:38:21 +01:00
*
* @param RuleTrigger $trigger
2016-02-17 15:38:21 +01:00
*
2016-02-17 20:19:44 +01:00
* @return AbstractTrigger
2016-02-17 15:38:21 +01:00
*/
2016-02-17 20:19:44 +01:00
public static function getTrigger(RuleTrigger $trigger)
2016-02-17 15:38:21 +01:00
{
$triggerType = $trigger->trigger_type;
2016-02-17 20:19:44 +01:00
/** @var AbstractTrigger $class */
2017-03-04 15:29:20 +01:00
$class = self::getTriggerClass($triggerType);
$obj = $class::makeFromTriggerValue($trigger->trigger_value);
$obj->stopProcessing = $trigger->stop_processing;
2016-02-17 20:19:44 +01: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 20:19:44 +01:00
return $obj;
2016-02-17 15:38:21 +01:00
}
/**
2016-02-18 06:45:10 +01: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 21:14:32 +01:00
* @param bool $stopProcessing
*
2016-02-18 06:45:10 +01:00
* @see TriggerFactory::getTrigger
2017-11-15 12:25:49 +01:00
*
* @return AbstractTrigger
2017-11-15 12:25:49 +01: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);
Log::debug('Created trigger from string', ['type' => $triggerType, 'value' => $triggerValue, 'stopProcessing' => $stopProcessing, 'class' => $class]);
return $obj;
}
/**
2016-02-18 06:45:10 +01:00
* Returns a map with trigger types, mapped to the class representing that type.
*
* @return array
*/
2016-02-18 06:45:10 +01:00
protected static function getTriggerTypes(): array
{
2017-11-15 12:25:49 +01:00
if (0 === count(self::$triggerTypes)) {
self::$triggerTypes = Domain::getRuleTriggers();
}
return self::$triggerTypes;
}
/**
2016-02-18 06:45:10 +01: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 15:29:26 +01:00
*
* @param string $triggerType
2016-02-17 15:29:26 +01:00
*
* @return TriggerInterface|string
2017-11-15 12:25:49 +01:00
*
* @throws FireflyException
*/
private static function getTriggerClass(string $triggerType): string
2016-02-17 15:29:26 +01:00
{
$triggerTypes = self::getTriggerTypes();
2016-02-17 15:29:26 +01:00
if (!array_key_exists($triggerType, $triggerTypes)) {
2016-02-17 15:52:46 +01:00
throw new FireflyException('No such trigger exists ("' . e($triggerType) . '").');
}
2016-02-17 15:29:26 +01:00
$class = $triggerTypes[$triggerType];
if (!class_exists($class)) {
2016-02-17 15:52:46 +01:00
throw new FireflyException('Could not instantiate class for rule trigger type "' . e($triggerType) . '" (' . e($class) . ').');
}
2016-02-17 15:29:26 +01:00
return $class;
}
}