2016-02-17 13:24:56 +01:00
|
|
|
<?php
|
2016-10-05 06:52:15 +02:00
|
|
|
/**
|
|
|
|
* ActionFactory.php
|
2019-10-05 10:10:11 +02:00
|
|
|
* Copyright (c) 2019 Robert Horlings
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 08:40:00 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2016-10-05 06:52:15 +02:00
|
|
|
*/
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
2016-02-17 13:24:56 +01:00
|
|
|
|
2017-09-13 07:49:58 +02:00
|
|
|
namespace FireflyIII\TransactionRules\Factory;
|
2016-02-17 13:24:56 +01:00
|
|
|
|
2016-02-17 15:52:46 +01:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2016-02-17 13:24:56 +01:00
|
|
|
use FireflyIII\Models\RuleAction;
|
|
|
|
use FireflyIII\Support\Domain;
|
2017-09-14 17:40:02 +02:00
|
|
|
use FireflyIII\TransactionRules\Actions\ActionInterface;
|
2016-09-21 20:30:09 +02:00
|
|
|
use Log;
|
2016-02-17 13:24:56 +01:00
|
|
|
|
|
|
|
/**
|
2017-11-25 15:20:53 +01:00
|
|
|
* Class ActionFactory can create actions.
|
2017-11-25 20:27:53 +01:00
|
|
|
*
|
|
|
|
* @codeCoverageIgnore
|
2016-02-17 13:24:56 +01:00
|
|
|
*/
|
|
|
|
class ActionFactory
|
|
|
|
{
|
2017-11-25 15:20:53 +01:00
|
|
|
/** @var array array of action types */
|
2016-02-18 06:45:10 +01:00
|
|
|
protected static $actionTypes = [];
|
2016-02-17 15:52:46 +01:00
|
|
|
|
|
|
|
/**
|
2017-09-13 07:49:58 +02:00
|
|
|
* This method returns the actual implementation (TransactionRules/Actions/[object]) for a given
|
2016-02-18 06:38:37 +01:00
|
|
|
* RuleAction (database object). If for example the database object contains action_type "change_category"
|
|
|
|
* with value "Groceries" this method will return a corresponding SetCategory object preset
|
|
|
|
* to "Groceries". Any transaction journal then fed to this object will have its category changed.
|
2016-02-17 15:52:46 +01:00
|
|
|
*
|
2016-02-17 21:59:47 +01:00
|
|
|
* @param RuleAction $action
|
2016-02-17 15:52:46 +01:00
|
|
|
*
|
|
|
|
* @return ActionInterface
|
2017-12-22 18:32:43 +01:00
|
|
|
*
|
2017-12-17 14:30:53 +01:00
|
|
|
* @throws FireflyException
|
2016-02-17 15:52:46 +01:00
|
|
|
*/
|
2016-02-17 19:56:05 +01:00
|
|
|
public static function getAction(RuleAction $action): ActionInterface
|
2016-02-17 15:52:46 +01:00
|
|
|
{
|
2016-02-18 06:38:37 +01:00
|
|
|
$class = self::getActionClass($action->action_type);
|
2016-09-21 20:30:09 +02:00
|
|
|
Log::debug(sprintf('self::getActionClass("%s") = "%s"', $action->action_type, $class));
|
2016-02-17 15:52:46 +01:00
|
|
|
|
2016-02-17 19:56:05 +01:00
|
|
|
return new $class($action);
|
2016-02-17 15:52:46 +01:00
|
|
|
}
|
|
|
|
|
2016-02-17 13:24:56 +01:00
|
|
|
/**
|
2016-02-18 06:38:37 +01:00
|
|
|
* Returns the class name to be used for actions with the given name. This is a lookup function
|
|
|
|
* that will match the given action type (ie. "change_category") to the matching class name
|
|
|
|
* (SetCategory) using the configuration (firefly.php).
|
2016-02-17 15:52:46 +01:00
|
|
|
*
|
2016-02-17 13:24:56 +01:00
|
|
|
* @param string $actionType
|
2016-02-17 15:52:46 +01:00
|
|
|
*
|
2016-02-18 06:38:37 +01:00
|
|
|
* @return string
|
2017-11-15 12:25:49 +01:00
|
|
|
*
|
2016-02-17 15:52:46 +01:00
|
|
|
* @throws FireflyException
|
2016-02-17 13:24:56 +01:00
|
|
|
*/
|
2016-02-17 15:52:46 +01:00
|
|
|
public static function getActionClass(string $actionType): string
|
|
|
|
{
|
2016-02-17 13:24:56 +01:00
|
|
|
$actionTypes = self::getActionTypes();
|
2016-02-17 15:52:46 +01:00
|
|
|
|
2016-02-17 13:24:56 +01:00
|
|
|
if (!array_key_exists($actionType, $actionTypes)) {
|
2016-02-17 15:52:46 +01:00
|
|
|
throw new FireflyException('No such action exists ("' . e($actionType) . '").');
|
2016-02-17 13:24:56 +01:00
|
|
|
}
|
2016-02-17 15:52:46 +01:00
|
|
|
|
2016-02-17 13:24:56 +01:00
|
|
|
$class = $actionTypes[$actionType];
|
|
|
|
if (!class_exists($class)) {
|
2016-02-17 15:52:46 +01:00
|
|
|
throw new FireflyException('Could not instantiate class for rule action type "' . e($actionType) . '" (' . e($class) . ').');
|
2016-02-17 13:24:56 +01:00
|
|
|
}
|
2016-02-17 15:52:46 +01:00
|
|
|
|
2016-02-17 13:24:56 +01:00
|
|
|
return $class;
|
|
|
|
}
|
2016-02-17 15:52:46 +01:00
|
|
|
|
2016-02-17 13:24:56 +01:00
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Returns a map with actiontypes, mapped to the class representing that type.
|
2016-02-18 06:45:10 +01:00
|
|
|
*
|
|
|
|
* @return array
|
2016-02-17 13:24:56 +01:00
|
|
|
*/
|
2016-02-18 06:45:10 +01:00
|
|
|
protected static function getActionTypes(): array
|
2016-02-17 15:52:46 +01:00
|
|
|
{
|
2019-05-30 12:31:19 +02:00
|
|
|
if (0 === count(self::$actionTypes)) {
|
2016-02-17 13:24:56 +01:00
|
|
|
self::$actionTypes = Domain::getRuleActions();
|
|
|
|
}
|
2016-02-17 15:52:46 +01:00
|
|
|
|
2016-02-17 13:24:56 +01:00
|
|
|
return self::$actionTypes;
|
|
|
|
}
|
|
|
|
}
|