Skip to content

Commit 41df17b

Browse files
authored
refactor (RSS-Bridge#3712)
* test: refactor test suite * docs * refactor * yup * docs
1 parent 0c92cf3 commit 41df17b

23 files changed

+310
-567
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ Modify `report_limit` so that an error must occur 3 times before it is reported.
248248
; Defines how often an error must occur before it is reported to the user
249249
report_limit = 3
250250
251+
The report count is reset to 0 each day.
252+
251253
### How to password-protect the instance
252254
253255
HTTP basic access authentication:

actions/ConnectivityAction.php

-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
<?php
22

3-
/**
4-
* This file is part of RSS-Bridge, a PHP project capable of generating RSS and
5-
* Atom feeds for websites that don't have one.
6-
*
7-
* For the full license information, please view the UNLICENSE file distributed
8-
* with this source code.
9-
*
10-
* @package Core
11-
* @license http://unlicense.org/ UNLICENSE
12-
* @link https://github.com/rss-bridge/rss-bridge
13-
*/
14-
153
/**
164
* Checks if the website for a given bridge is reachable.
175
*

actions/DetectAction.php

-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
<?php
22

3-
/**
4-
* This file is part of RSS-Bridge, a PHP project capable of generating RSS and
5-
* Atom feeds for websites that don't have one.
6-
*
7-
* For the full license information, please view the UNLICENSE file distributed
8-
* with this source code.
9-
*
10-
* @package Core
11-
* @license http://unlicense.org/ UNLICENSE
12-
* @link https://github.com/rss-bridge/rss-bridge
13-
*/
14-
153
class DetectAction implements ActionInterface
164
{
175
public function execute(array $request)

actions/DisplayAction.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ private function createResponse(array $request, BridgeAbstract $bridge, FormatAb
101101
try {
102102
$bridge->loadConfiguration();
103103
// Remove parameters that don't concern bridges
104-
$bridgeData = array_diff_key($request, array_fill_keys(['action', 'bridge', 'format', '_noproxy', '_cache_timeout', '_error_time'], ''));
105-
$bridge->setDatas($bridgeData);
104+
$input = array_diff_key($request, array_fill_keys(['action', 'bridge', 'format', '_noproxy', '_cache_timeout', '_error_time'], ''));
105+
$bridge->setInput($input);
106106
$bridge->collectData();
107107
$items = $bridge->getItems();
108108
if (isset($items[0]) && is_array($items[0])) {

actions/ListAction.php

+8-20
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
<?php
22

3-
/**
4-
* This file is part of RSS-Bridge, a PHP project capable of generating RSS and
5-
* Atom feeds for websites that don't have one.
6-
*
7-
* For the full license information, please view the UNLICENSE file distributed
8-
* with this source code.
9-
*
10-
* @package Core
11-
* @license http://unlicense.org/ UNLICENSE
12-
* @link https://github.com/rss-bridge/rss-bridge
13-
*/
14-
153
class ListAction implements ActionInterface
164
{
175
public function execute(array $request)
@@ -26,14 +14,14 @@ public function execute(array $request)
2614
$bridge = $bridgeFactory->create($bridgeClassName);
2715

2816
$list->bridges[$bridgeClassName] = [
29-
'status' => $bridgeFactory->isEnabled($bridgeClassName) ? 'active' : 'inactive',
30-
'uri' => $bridge->getURI(),
31-
'donationUri' => $bridge->getDonationURI(),
32-
'name' => $bridge->getName(),
33-
'icon' => $bridge->getIcon(),
34-
'parameters' => $bridge->getParameters(),
35-
'maintainer' => $bridge->getMaintainer(),
36-
'description' => $bridge->getDescription()
17+
'status' => $bridgeFactory->isEnabled($bridgeClassName) ? 'active' : 'inactive',
18+
'uri' => $bridge->getURI(),
19+
'donationUri' => $bridge->getDonationURI(),
20+
'name' => $bridge->getName(),
21+
'icon' => $bridge->getIcon(),
22+
'parameters' => $bridge->getParameters(),
23+
'maintainer' => $bridge->getMaintainer(),
24+
'description' => $bridge->getDescription()
3725
];
3826
}
3927
$list->total = count($list->bridges);

actions/SetBridgeCacheAction.php

-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
<?php
22

3-
/**
4-
* This file is part of RSS-Bridge, a PHP project capable of generating RSS and
5-
* Atom feeds for websites that don't have one.
6-
*
7-
* For the full license information, please view the UNLICENSE file distributed
8-
* with this source code.
9-
*
10-
* @package Core
11-
* @license http://unlicense.org/ UNLICENSE
12-
* @link https://github.com/rss-bridge/rss-bridge
13-
*/
14-
153
class SetBridgeCacheAction implements ActionInterface
164
{
175
private CacheInterface $cache;

bridges/DemoBridge.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class DemoBridge extends BridgeAbstract
44
{
55
const MAINTAINER = 'teromene';
66
const NAME = 'DemoBridge';
7-
const URI = 'http://github.com/rss-bridge/rss-bridge';
7+
const URI = 'https://github.com/rss-bridge/rss-bridge';
88
const DESCRIPTION = 'Bridge used for demos';
99
const CACHE_TIMEOUT = 15;
1010

lib/ApiAuthenticationMiddleware.php

-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
<?php
22

3-
/**
4-
* This file is part of RSS-Bridge, a PHP project capable of generating RSS and
5-
* Atom feeds for websites that don't have one.
6-
*
7-
* For the full license information, please view the UNLICENSE file distributed
8-
* with this source code.
9-
*
10-
* @package Core
11-
* @license http://unlicense.org/ UNLICENSE
12-
* @link https://github.com/rss-bridge/rss-bridge
13-
*/
14-
153
final class ApiAuthenticationMiddleware
164
{
175
public function __invoke($request): void

lib/AuthenticationMiddleware.php

-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
<?php
22

3-
/**
4-
* This file is part of RSS-Bridge, a PHP project capable of generating RSS and
5-
* Atom feeds for websites that don't have one.
6-
*
7-
* For the full license information, please view the UNLICENSE file distributed
8-
* with this source code.
9-
*
10-
* @package Core
11-
* @license http://unlicense.org/ UNLICENSE
12-
* @link https://github.com/rss-bridge/rss-bridge
13-
*/
14-
153
final class AuthenticationMiddleware
164
{
175
public function __construct()

lib/BridgeAbstract.php

+65-97
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,70 @@ public function getCacheTimeout()
9090
return static::CACHE_TIMEOUT;
9191
}
9292

93-
/**
94-
* Sets the input values for a given context.
95-
*
96-
* @param array $inputs Associative array of inputs
97-
* @param string $queriedContext The context name
98-
* @return void
99-
*/
100-
protected function setInputs(array $inputs, $queriedContext)
93+
public function loadConfiguration()
94+
{
95+
foreach (static::CONFIGURATION as $optionName => $optionValue) {
96+
$section = $this->getShortName();
97+
$configurationOption = Configuration::getConfig($section, $optionName);
98+
99+
if ($configurationOption !== null) {
100+
$this->configuration[$optionName] = $configurationOption;
101+
continue;
102+
}
103+
104+
if (isset($optionValue['required']) && $optionValue['required'] === true) {
105+
throw new \Exception(sprintf('Missing configuration option: %s', $optionName));
106+
} elseif (isset($optionValue['defaultValue'])) {
107+
$this->configuration[$optionName] = $optionValue['defaultValue'];
108+
}
109+
}
110+
}
111+
112+
public function setInput(array $input)
113+
{
114+
$context = $input['context'] ?? null;
115+
if ($context) {
116+
// Context hinting (optional)
117+
$this->queriedContext = $context;
118+
unset($input['context']);
119+
}
120+
121+
$parameters = $this->getParameters();
122+
123+
if (!$parameters) {
124+
if ($input) {
125+
throw new \Exception('Invalid parameters value(s)');
126+
}
127+
return;
128+
}
129+
130+
$validator = new ParameterValidator();
131+
132+
// $input is passed by reference!
133+
if (!$validator->validateInput($input, $parameters)) {
134+
$invalidParameterKeys = array_column($validator->getInvalidParameters(), 'name');
135+
throw new \Exception(sprintf('Invalid parameters value(s): %s', implode(', ', $invalidParameterKeys)));
136+
}
137+
138+
// Guess the context from input data
139+
if (empty($this->queriedContext)) {
140+
$queriedContext = $validator->getQueriedContext($input, $parameters);
141+
$this->queriedContext = $queriedContext;
142+
}
143+
144+
if (is_null($this->queriedContext)) {
145+
throw new \Exception('Required parameter(s) missing');
146+
} elseif ($this->queriedContext === false) {
147+
throw new \Exception('Mixed context parameters');
148+
}
149+
150+
$this->setInputWithContext($input, $this->queriedContext);
151+
}
152+
153+
private function setInputWithContext(array $input, $queriedContext)
101154
{
102155
// Import and assign all inputs to their context
103-
foreach ($inputs as $name => $value) {
156+
foreach ($input as $name => $value) {
104157
foreach (static::PARAMETERS as $context => $set) {
105158
if (array_key_exists($name, static::PARAMETERS[$context])) {
106159
$this->inputs[$context][$name]['value'] = $value;
@@ -128,7 +181,7 @@ protected function setInputs(array $inputs, $queriedContext)
128181

129182
switch ($type) {
130183
case 'checkbox':
131-
$this->inputs[$context][$name]['value'] = $inputs[$context][$name]['value'] ?? false;
184+
$this->inputs[$context][$name]['value'] = $input[$context][$name]['value'] ?? false;
132185
break;
133186
case 'list':
134187
if (!isset($properties['defaultValue'])) {
@@ -153,8 +206,8 @@ protected function setInputs(array $inputs, $queriedContext)
153206
// Copy global parameter values to the guessed context
154207
if (array_key_exists('global', static::PARAMETERS)) {
155208
foreach (static::PARAMETERS['global'] as $name => $properties) {
156-
if (isset($inputs[$name])) {
157-
$value = $inputs[$name];
209+
if (isset($input[$name])) {
210+
$value = $input[$name];
158211
} else {
159212
if ($properties['type'] ?? null === 'checkbox') {
160213
$value = false;
@@ -176,91 +229,6 @@ protected function setInputs(array $inputs, $queriedContext)
176229
}
177230
}
178231

179-
/**
180-
* Set inputs for the bridge
181-
*
182-
* Returns errors and aborts execution if the provided input parameters are
183-
* invalid.
184-
*
185-
* @param array List of input parameters. Each element in this list must
186-
* relate to an item in {@see BridgeAbstract::PARAMETERS}
187-
* @return void
188-
*/
189-
public function setDatas(array $inputs)
190-
{
191-
if (isset($inputs['context'])) { // Context hinting (optional)
192-
$this->queriedContext = $inputs['context'];
193-
unset($inputs['context']);
194-
}
195-
196-
if (empty(static::PARAMETERS)) {
197-
if (!empty($inputs)) {
198-
throw new \Exception('Invalid parameters value(s)');
199-
}
200-
201-
return;
202-
}
203-
204-
$validator = new ParameterValidator();
205-
206-
if (!$validator->validateData($inputs, static::PARAMETERS)) {
207-
$parameters = array_map(
208-
function ($i) {
209-
return $i['name'];
210-
}, // Just display parameter names
211-
$validator->getInvalidParameters()
212-
);
213-
214-
throw new \Exception(sprintf('Invalid parameters value(s): %s', implode(', ', $parameters)));
215-
}
216-
217-
// Guess the context from input data
218-
if (empty($this->queriedContext)) {
219-
$this->queriedContext = $validator->getQueriedContext($inputs, static::PARAMETERS);
220-
}
221-
222-
if (is_null($this->queriedContext)) {
223-
throw new \Exception('Required parameter(s) missing');
224-
} elseif ($this->queriedContext === false) {
225-
throw new \Exception('Mixed context parameters');
226-
}
227-
228-
$this->setInputs($inputs, $this->queriedContext);
229-
}
230-
231-
/**
232-
* Loads configuration for the bridge
233-
*
234-
* Returns errors and aborts execution if the provided configuration is
235-
* invalid.
236-
*
237-
* @return void
238-
*/
239-
public function loadConfiguration()
240-
{
241-
foreach (static::CONFIGURATION as $optionName => $optionValue) {
242-
$section = $this->getShortName();
243-
$configurationOption = Configuration::getConfig($section, $optionName);
244-
245-
if ($configurationOption !== null) {
246-
$this->configuration[$optionName] = $configurationOption;
247-
continue;
248-
}
249-
250-
if (isset($optionValue['required']) && $optionValue['required'] === true) {
251-
throw new \Exception(sprintf('Missing configuration option: %s', $optionName));
252-
} elseif (isset($optionValue['defaultValue'])) {
253-
$this->configuration[$optionName] = $optionValue['defaultValue'];
254-
}
255-
}
256-
}
257-
258-
/**
259-
* Returns the value for the provided input
260-
*
261-
* @param string $input The input name
262-
* @return mixed|null The input value or null if the input is not defined
263-
*/
264232
protected function getInput($input)
265233
{
266234
return $this->inputs[$this->queriedContext][$input]['value'] ?? null;

lib/BridgeCard.php

-20
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,5 @@
11
<?php
22

3-
/**
4-
* This file is part of RSS-Bridge, a PHP project capable of generating RSS and
5-
* Atom feeds for websites that don't have one.
6-
*
7-
* For the full license information, please view the UNLICENSE file distributed
8-
* with this source code.
9-
*
10-
* @package Core
11-
* @license http://unlicense.org/ UNLICENSE
12-
* @link https://github.com/rss-bridge/rss-bridge
13-
*/
14-
15-
/**
16-
* A generator class for a single bridge card on the home page of RSS-Bridge.
17-
*
18-
* This class generates the HTML content for a single bridge card for the home
19-
* page of RSS-Bridge.
20-
*
21-
* @todo Return error if a caller creates an object of this class.
22-
*/
233
final class BridgeCard
244
{
255
/**

lib/BridgeFactory.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ final class BridgeFactory
44
{
55
private CacheInterface $cache;
66
private Logger $logger;
7-
private $bridgeClassNames = [];
8-
private $enabledBridges = [];
9-
private $missingEnabledBridges = [];
7+
private array $bridgeClassNames = [];
8+
private array $enabledBridges = [];
9+
private array $missingEnabledBridges = [];
1010

1111
public function __construct()
1212
{
@@ -22,7 +22,7 @@ public function __construct()
2222

2323
$enabledBridges = Configuration::getConfig('system', 'enabled_bridges');
2424
if ($enabledBridges === null) {
25-
throw new \Exception('No bridges are enabled... wtf?');
25+
throw new \Exception('No bridges are enabled...');
2626
}
2727
foreach ($enabledBridges as $enabledBridge) {
2828
if ($enabledBridge === '*') {

0 commit comments

Comments
 (0)