refactor: inject the action params via its execute method (#2907)

This commit is contained in:
Dag 2022-07-08 21:06:14 +02:00 committed by GitHub
parent 22c10941dc
commit a966213cd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 32 additions and 35 deletions

View File

@ -24,8 +24,6 @@
*/ */
class ConnectivityAction implements ActionInterface class ConnectivityAction implements ActionInterface
{ {
public $userData = [];
private BridgeFactory $bridgeFactory; private BridgeFactory $bridgeFactory;
public function __construct() public function __construct()
@ -33,18 +31,18 @@ class ConnectivityAction implements ActionInterface
$this->bridgeFactory = new \BridgeFactory(); $this->bridgeFactory = new \BridgeFactory();
} }
public function execute() public function execute(array $request)
{ {
if (!Debug::isEnabled()) { if (!Debug::isEnabled()) {
returnError('This action is only available in debug mode!', 400); returnError('This action is only available in debug mode!', 400);
} }
if (!isset($this->userData['bridge'])) { if (!isset($request['bridge'])) {
$this->returnEntryPage(); $this->returnEntryPage();
return; return;
} }
$bridgeName = $this->userData['bridge']; $bridgeName = $request['bridge'];
$bridgeClassName = $this->bridgeFactory->sanitizeBridgeName($bridgeName); $bridgeClassName = $this->bridgeFactory->sanitizeBridgeName($bridgeName);

View File

@ -14,14 +14,12 @@
class DetectAction implements ActionInterface class DetectAction implements ActionInterface
{ {
public $userData = []; public function execute(array $request)
public function execute()
{ {
$targetURL = $this->userData['url'] $targetURL = $request['url']
or returnClientError('You must specify a url!'); or returnClientError('You must specify a url!');
$format = $this->userData['format'] $format = $request['format']
or returnClientError('You must specify a format!'); or returnClientError('You must specify a format!');
$bridgeFactory = new \BridgeFactory(); $bridgeFactory = new \BridgeFactory();

View File

@ -14,19 +14,20 @@
class DisplayAction implements ActionInterface class DisplayAction implements ActionInterface
{ {
public $userData = []; public function execute(array $request)
public function execute()
{ {
$bridgeFactory = new \BridgeFactory(); $bridgeFactory = new \BridgeFactory();
$bridgeClassName = isset($this->userData['bridge']) ? $bridgeFactory->sanitizeBridgeName($this->userData['bridge']) : null; $bridgeClassName = null;
if (isset($request['bridge'])) {
$bridgeClassName = $bridgeFactory->sanitizeBridgeName($request['bridge']);
}
if ($bridgeClassName === null) { if ($bridgeClassName === null) {
throw new \InvalidArgumentException('Bridge name invalid!'); throw new \InvalidArgumentException('Bridge name invalid!');
} }
$format = $this->userData['format'] $format = $request['format']
or returnClientError('You must specify a format!'); or returnClientError('You must specify a format!');
// whitelist control // whitelist control
@ -39,8 +40,8 @@ class DisplayAction implements ActionInterface
$bridge = $bridgeFactory->create($bridgeClassName); $bridge = $bridgeFactory->create($bridgeClassName);
$bridge->loadConfiguration(); $bridge->loadConfiguration();
$noproxy = array_key_exists('_noproxy', $this->userData) $noproxy = array_key_exists('_noproxy', $request)
&& filter_var($this->userData['_noproxy'], FILTER_VALIDATE_BOOLEAN); && filter_var($request['_noproxy'], FILTER_VALIDATE_BOOLEAN);
if (defined('PROXY_URL') && PROXY_BYBRIDGE && $noproxy) { if (defined('PROXY_URL') && PROXY_BYBRIDGE && $noproxy) {
define('NOPROXY', true); define('NOPROXY', true);
@ -48,22 +49,22 @@ class DisplayAction implements ActionInterface
// Cache timeout // Cache timeout
$cache_timeout = -1; $cache_timeout = -1;
if (array_key_exists('_cache_timeout', $this->userData)) { if (array_key_exists('_cache_timeout', $request)) {
if (!CUSTOM_CACHE_TIMEOUT) { if (!CUSTOM_CACHE_TIMEOUT) {
unset($this->userData['_cache_timeout']); unset($request['_cache_timeout']);
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) . '?' . http_build_query($this->userData); $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) . '?' . http_build_query($request);
header('Location: ' . $uri, true, 301); header('Location: ' . $uri, true, 301);
exit; exit;
} }
$cache_timeout = filter_var($this->userData['_cache_timeout'], FILTER_VALIDATE_INT); $cache_timeout = filter_var($request['_cache_timeout'], FILTER_VALIDATE_INT);
} else { } else {
$cache_timeout = $bridge->getCacheTimeout(); $cache_timeout = $bridge->getCacheTimeout();
} }
// Remove parameters that don't concern bridges // Remove parameters that don't concern bridges
$bridge_params = array_diff_key( $bridge_params = array_diff_key(
$this->userData, $request,
array_fill_keys( array_fill_keys(
[ [
'action', 'action',
@ -79,7 +80,7 @@ class DisplayAction implements ActionInterface
// Remove parameters that don't concern caches // Remove parameters that don't concern caches
$cache_params = array_diff_key( $cache_params = array_diff_key(
$this->userData, $request,
array_fill_keys( array_fill_keys(
[ [
'action', 'action',
@ -162,19 +163,19 @@ class DisplayAction implements ActionInterface
$item = new \FeedItem(); $item = new \FeedItem();
// Create "new" error message every 24 hours // Create "new" error message every 24 hours
$this->userData['_error_time'] = urlencode((int)(time() / 86400)); $request['_error_time'] = urlencode((int)(time() / 86400));
$message = sprintf( $message = sprintf(
'Bridge returned error %s! (%s)', 'Bridge returned error %s! (%s)',
$e->getCode(), $e->getCode(),
$this->userData['_error_time'] $request['_error_time']
); );
$item->setTitle($message); $item->setTitle($message);
$item->setURI( $item->setURI(
(isset($_SERVER['REQUEST_URI']) ? parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) : '') (isset($_SERVER['REQUEST_URI']) ? parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) : '')
. '?' . '?'
. http_build_query($this->userData) . http_build_query($request)
); );
$item->setTimestamp(time()); $item->setTimestamp(time());

View File

@ -14,7 +14,7 @@
class ListAction implements ActionInterface class ListAction implements ActionInterface
{ {
public function execute() public function execute(array $request)
{ {
$list = new StdClass(); $list = new StdClass();
$list->bridges = []; $list->bridges = [];

View File

@ -8,18 +8,18 @@ rss-bridge from the command line
*/ */
if (isset($argv)) { if (isset($argv)) {
parse_str(implode('&', array_slice($argv, 1)), $cliArgs); parse_str(implode('&', array_slice($argv, 1)), $cliArgs);
$params = array_merge($_GET, $cliArgs); $request = array_merge($_GET, $cliArgs);
} else { } else {
$params = $_GET; $request = $_GET;
} }
try { try {
$actionFactory = new ActionFactory(); $actionFactory = new ActionFactory();
if (array_key_exists('action', $params)) { if (array_key_exists('action', $request)) {
$action = $actionFactory->create($params['action']); $action = $actionFactory->create($request['action']);
$action->userData = $params;
$action->execute(); $action->execute($request);
} else { } else {
$showInactive = filter_input(INPUT_GET, 'show_inactive', FILTER_VALIDATE_BOOLEAN); $showInactive = filter_input(INPUT_GET, 'show_inactive', FILTER_VALIDATE_BOOLEAN);
echo BridgeList::create($showInactive); echo BridgeList::create($showInactive);

View File

@ -24,5 +24,5 @@ interface ActionInterface
* *
* @return void * @return void
*/ */
public function execute(); public function execute(array $request);
} }

View File

@ -85,7 +85,7 @@ class ListActionTest extends TestCase
$action = $actionFactory->create('list'); $action = $actionFactory->create('list');
ob_start(); ob_start();
$action->execute(); $action->execute([]);
$this->data = ob_get_contents(); $this->data = ob_get_contents();
ob_clean(); ob_clean();
ob_end_flush(); ob_end_flush();