2016-02-17 06:24:56 -06:00
|
|
|
<?php
|
2016-10-04 23:52:15 -05:00
|
|
|
/**
|
|
|
|
* ActionFactory.php
|
2017-12-17 07:44:05 -06:00
|
|
|
* Copyright (C) 2017 Robert Horlings.
|
2016-10-04 23:52:15 -05: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
|
2017-12-17 07:44:05 -06:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-10-04 23:52:15 -05:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2016-02-17 06:24:56 -06:00
|
|
|
|
2017-09-13 00:49:58 -05:00
|
|
|
namespace FireflyIII\TransactionRules\Factory;
|
2016-02-17 06:24:56 -06:00
|
|
|
|
2016-02-17 08:52:46 -06:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2016-02-17 06:24:56 -06:00
|
|
|
use FireflyIII\Models\RuleAction;
|
|
|
|
use FireflyIII\Support\Domain;
|
2017-09-14 10:40:02 -05:00
|
|
|
use FireflyIII\TransactionRules\Actions\ActionInterface;
|
2016-09-21 13:30:09 -05:00
|
|
|
use Log;
|
2016-02-17 06:24:56 -06:00
|
|
|
|
|
|
|
/**
|
2017-11-25 08:20:53 -06:00
|
|
|
* Class ActionFactory can create actions.
|
2017-11-25 13:27:53 -06:00
|
|
|
*
|
|
|
|
* @codeCoverageIgnore
|
2016-02-17 06:24:56 -06:00
|
|
|
*/
|
|
|
|
class ActionFactory
|
|
|
|
{
|
2017-11-25 08:20:53 -06:00
|
|
|
/** @var array array of action types */
|
2016-02-17 23:45:10 -06:00
|
|
|
protected static $actionTypes = [];
|
2016-02-17 08:52:46 -06:00
|
|
|
|
|
|
|
/**
|
2017-09-13 00:49:58 -05:00
|
|
|
* This method returns the actual implementation (TransactionRules/Actions/[object]) for a given
|
2016-02-17 23:38:37 -06: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 08:52:46 -06:00
|
|
|
*
|
2016-02-17 14:59:47 -06:00
|
|
|
* @param RuleAction $action
|
2016-02-17 08:52:46 -06:00
|
|
|
*
|
|
|
|
* @return ActionInterface
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2017-12-17 07:30:53 -06:00
|
|
|
* @throws FireflyException
|
2016-02-17 08:52:46 -06:00
|
|
|
*/
|
2016-02-17 12:56:05 -06:00
|
|
|
public static function getAction(RuleAction $action): ActionInterface
|
2016-02-17 08:52:46 -06:00
|
|
|
{
|
2016-02-17 23:38:37 -06:00
|
|
|
$class = self::getActionClass($action->action_type);
|
2016-09-21 13:30:09 -05:00
|
|
|
Log::debug(sprintf('self::getActionClass("%s") = "%s"', $action->action_type, $class));
|
2016-02-17 08:52:46 -06:00
|
|
|
|
2016-02-17 12:56:05 -06:00
|
|
|
return new $class($action);
|
2016-02-17 08:52:46 -06:00
|
|
|
}
|
|
|
|
|
2016-02-17 06:24:56 -06:00
|
|
|
/**
|
2016-02-17 23:38:37 -06: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 08:52:46 -06:00
|
|
|
*
|
2016-02-17 06:24:56 -06:00
|
|
|
* @param string $actionType
|
2016-02-17 08:52:46 -06:00
|
|
|
*
|
2016-02-17 23:38:37 -06:00
|
|
|
* @return string
|
2017-11-15 05:25:49 -06:00
|
|
|
*
|
2016-02-17 08:52:46 -06:00
|
|
|
* @throws FireflyException
|
2016-02-17 06:24:56 -06:00
|
|
|
*/
|
2016-02-17 08:52:46 -06:00
|
|
|
public static function getActionClass(string $actionType): string
|
|
|
|
{
|
2016-02-17 06:24:56 -06:00
|
|
|
$actionTypes = self::getActionTypes();
|
2016-02-17 08:52:46 -06:00
|
|
|
|
2016-02-17 06:24:56 -06:00
|
|
|
if (!array_key_exists($actionType, $actionTypes)) {
|
2016-02-17 08:52:46 -06:00
|
|
|
throw new FireflyException('No such action exists ("' . e($actionType) . '").');
|
2016-02-17 06:24:56 -06:00
|
|
|
}
|
2016-02-17 08:52:46 -06:00
|
|
|
|
2016-02-17 06:24:56 -06:00
|
|
|
$class = $actionTypes[$actionType];
|
|
|
|
if (!class_exists($class)) {
|
2016-02-17 08:52:46 -06:00
|
|
|
throw new FireflyException('Could not instantiate class for rule action type "' . e($actionType) . '" (' . e($class) . ').');
|
2016-02-17 06:24:56 -06:00
|
|
|
}
|
2016-02-17 08:52:46 -06:00
|
|
|
|
2016-02-17 06:24:56 -06:00
|
|
|
return $class;
|
|
|
|
}
|
2016-02-17 08:52:46 -06:00
|
|
|
|
2016-02-17 06:24:56 -06:00
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Returns a map with actiontypes, mapped to the class representing that type.
|
2016-02-17 23:45:10 -06:00
|
|
|
*
|
|
|
|
* @return array
|
2016-02-17 06:24:56 -06:00
|
|
|
*/
|
2016-02-17 23:45:10 -06:00
|
|
|
protected static function getActionTypes(): array
|
2016-02-17 08:52:46 -06:00
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
if (0 === \count(self::$actionTypes)) {
|
2016-02-17 06:24:56 -06:00
|
|
|
self::$actionTypes = Domain::getRuleActions();
|
|
|
|
}
|
2016-02-17 08:52:46 -06:00
|
|
|
|
2016-02-17 06:24:56 -06:00
|
|
|
return self::$actionTypes;
|
|
|
|
}
|
|
|
|
}
|