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

102 lines
3.3 KiB
PHP
Raw Normal View History

<?php
/**
* ActionFactory.php
2019-10-05 10:10:11 +02:00
* Copyright (c) 2019 Robert Horlings
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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
*
* 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
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +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/>.
*/
declare(strict_types=1);
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\RuleAction;
use FireflyIII\Support\Domain;
2017-09-14 17:40:02 +02:00
use FireflyIII\TransactionRules\Actions\ActionInterface;
use Log;
/**
2017-11-25 15:20:53 +01:00
* Class ActionFactory can create actions.
2017-11-25 20:27:53 +01:00
*
* @codeCoverageIgnore
*/
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
*/
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);
Log::debug(sprintf('self::getActionClass("%s") = "%s"', $action->action_type, $class));
2016-02-17 15:52:46 +01:00
return new $class($action);
2016-02-17 15:52:46 +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
*
* @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 15:52:46 +01:00
public static function getActionClass(string $actionType): string
{
$actionTypes = self::getActionTypes();
2016-02-17 15:52:46 +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 15:52:46 +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 15:52:46 +01:00
return $class;
}
2016-02-17 15:52:46 +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-18 06:45:10 +01:00
protected static function getActionTypes(): array
2016-02-17 15:52:46 +01:00
{
if (0 === count(self::$actionTypes)) {
self::$actionTypes = Domain::getRuleActions();
}
2016-02-17 15:52:46 +01:00
return self::$actionTypes;
}
}