firefly-iii/app/Rules/Factory/ActionFactory.php

86 lines
2.5 KiB
PHP
Raw Normal View History

<?php
2016-04-01 09:44:46 -05:00
declare(strict_types = 1);
/**
* ActionFactory.php
* Copyright (C) 2016 Robert Horlings
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
2016-02-17 23:34:16 -06:00
declare(strict_types = 1);
namespace FireflyIII\Rules\Factory;
2016-02-17 08:52:46 -06:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\RuleAction;
2016-02-17 23:34:16 -06:00
use FireflyIII\Rules\Actions\ActionInterface;
use FireflyIII\Support\Domain;
/**
* Interface ActionInterface
*
* @package FireflyIII\Rules\Actions
*/
class ActionFactory
{
2016-02-17 23:45:10 -06:00
protected static $actionTypes = [];
2016-02-17 08:52:46 -06:00
/**
2016-02-17 23:45:10 -06:00
* This method returns the actual implementation (Rules/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
*/
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-02-17 08:52:46 -06:00
return new $class($action);
2016-02-17 08:52:46 -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
*
* @param string $actionType
2016-02-17 08:52:46 -06:00
*
2016-02-17 23:38:37 -06:00
* @return string
2016-02-17 08:52:46 -06:00
* @throws FireflyException
*/
2016-02-17 08:52:46 -06:00
public static function getActionClass(string $actionType): string
{
$actionTypes = self::getActionTypes();
2016-02-17 08:52:46 -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 08:52:46 -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 08:52:46 -06:00
return $class;
}
2016-02-17 08:52:46 -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 23:45:10 -06:00
protected static function getActionTypes(): array
2016-02-17 08:52:46 -06:00
{
2016-02-17 23:45:10 -06:00
if (count(self::$actionTypes) === 0) {
self::$actionTypes = Domain::getRuleActions();
}
2016-02-17 08:52:46 -06:00
return self::$actionTypes;
}
}