rss-bridge/actions/DetectAction.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

52 lines
1.5 KiB
PHP
Raw Normal View History

2019-02-06 20:34:51 +03:00
<?php
2022-06-22 19:30:37 +03:00
class DetectAction implements ActionInterface
{
private BridgeFactory $bridgeFactory;
public function __construct(
BridgeFactory $bridgeFactory
) {
$this->bridgeFactory = $bridgeFactory;
}
2024-08-07 01:21:06 +03:00
public function __invoke(Request $request): Response
{
$url = $request->get('url');
$format = $request->get('format');
2019-02-06 20:34:51 +03:00
if (!$url) {
return new Response(render(__DIR__ . '/../templates/error.html.php', ['message' => 'You must specify a url']));
}
if (!$format) {
return new Response(render(__DIR__ . '/../templates/error.html.php', ['message' => 'You must specify a format']));
}
foreach ($this->bridgeFactory->getBridgeClassNames() as $bridgeClassName) {
if (!$this->bridgeFactory->isEnabled($bridgeClassName)) {
continue;
}
2019-02-06 20:34:51 +03:00
$bridge = $this->bridgeFactory->create($bridgeClassName);
2019-02-06 20:34:51 +03:00
$bridgeParams = $bridge->detectParameters($url);
2019-02-06 20:34:51 +03:00
if (!$bridgeParams) {
2019-02-06 20:34:51 +03:00
continue;
}
2019-02-06 20:34:51 +03:00
$query = [
'action' => 'display',
'bridge' => $bridgeClassName,
'format' => $format,
];
$query = array_merge($query, $bridgeParams);
return new Response('', 301, ['location' => '?' . http_build_query($query)]);
2019-02-06 20:34:51 +03:00
}
return new Response(render(__DIR__ . '/../templates/error.html.php', [
'message' => 'No bridge found for given URL: ' . $url,
]));
2019-02-06 20:34:51 +03:00
}
}