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

138 lines
4.8 KiB
PHP
Raw Normal View History

<?php
/**
* TriggerFactory.php
2017-12-17 07:44:05 -06:00
* Copyright (C) 2017 Robert Horlings.
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
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
2017-12-17 07:44:05 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
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-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;
use Log;
/**
2017-11-25 08:20:53 -06:00
* Class TriggerFactory can create triggers.
2017-11-25 13:27:53 -06:00
*
* @codeCoverageIgnore
*/
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
*
* @param RuleTrigger $trigger
2016-02-17 08:38:21 -06:00
*
2016-02-17 13:19:44 -06:00
* @return AbstractTrigger
2017-12-22 11:32:43 -06:00
*
2017-12-17 07:30:53 -06:00
* @throws FireflyException
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
2017-11-15 05:25:49 -06:00
*
* @return AbstractTrigger
2017-11-15 05:25:49 -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);
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
{
2017-11-15 05:25:49 -06:00
if (0 === count(self::$triggerTypes)) {
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
2017-11-15 05:25:49 -06:00
*
* @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;
}
}