1
0
mirror of https://github.com/RSS-Bridge/rss-bridge.git synced 2025-01-16 21:58:21 +01:00

refactor: ActionFactory (#2833)

This commit is contained in:
Dag 2022-06-22 18:28:07 +02:00 committed by GitHub
parent af5648d928
commit b7b9378484
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 47 deletions

View File

@ -16,7 +16,6 @@ if (isset($argv)) {
try {
$actionFac = new \ActionFactory();
$actionFac->setWorkingDir(PATH_LIB_ACTIONS);
if(array_key_exists('action', $params)) {
$action = $actionFac->create($params['action']);

View File

@ -11,53 +11,26 @@
* @link https://github.com/rss-bridge/rss-bridge
*/
/**
* Factory for action objects.
*/
class ActionFactory extends FactoryAbstract {
/**
* {@inheritdoc}
*
* @param string $name {@inheritdoc}
*/
public function create($name) {
$filePath = $this->buildFilePath($name);
class ActionFactory
{
private $folder;
public function __construct(string $folder = PATH_LIB_ACTIONS)
{
$this->folder = $folder;
}
/**
* @param string $name The name of the action e.g. "Display", "List", or "Connectivity"
*/
public function create(string $name): ActionInterface
{
$name = ucfirst(strtolower($name)) . 'Action';
$filePath = $this->folder . $name . '.php';
if(!file_exists($filePath)) {
throw new \Exception('Action ' . $name . ' does not exist!');
throw new \Exception('Invalid action');
}
$class = $this->buildClassName($name);
if((new \ReflectionClass($class))->isInstantiable()) {
return new $class();
}
return false;
}
/**
* Build class name from action name
*
* The class name consists of the action name with prefix "Action". The first
* character of the class name must be uppercase.
*
* Example: 'display' => 'DisplayAction'
*
* @param string $name The action name.
* @return string The class name.
*/
protected function buildClassName($name) {
return ucfirst(strtolower($name)) . 'Action';
}
/**
* Build file path to the action class.
*
* @param string $name The action name.
* @return string Path to the action class.
*/
protected function buildFilePath($name) {
return $this->getWorkingDir() . $this->buildClassName($name) . '.php';
$className = '\\' . $name;
return new $className();
}
}

View File

@ -78,7 +78,6 @@ class ListActionTest extends TestCase {
private function initAction() {
$actionFac = new ActionFactory();
$actionFac->setWorkingDir(PATH_LIB_ACTIONS);
$action = $actionFac->create('list');
$action->setUserData(array()); /* no user data required */