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:
parent
af5648d928
commit
b7b9378484
@ -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']);
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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 */
|
||||
|
Loading…
x
Reference in New Issue
Block a user