2019-02-06 20:34:51 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-22 19:30:37 +03:00
|
|
|
class DetectAction implements ActionInterface
|
|
|
|
{
|
2024-08-29 23:48:59 +03:00
|
|
|
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
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2024-01-29 23:51:34 +03:00
|
|
|
$url = $request->get('url');
|
2024-01-25 18:06:24 +03:00
|
|
|
$format = $request->get('format');
|
2019-02-06 20:34:51 +03:00
|
|
|
|
2024-01-29 23:51:34 +03:00
|
|
|
if (!$url) {
|
|
|
|
return new Response(render(__DIR__ . '/../templates/error.html.php', ['message' => 'You must specify a url']));
|
2022-08-06 23:46:28 +03:00
|
|
|
}
|
|
|
|
if (!$format) {
|
2024-01-29 23:51:34 +03:00
|
|
|
return new Response(render(__DIR__ . '/../templates/error.html.php', ['message' => 'You must specify a format']));
|
2022-08-06 23:46:28 +03:00
|
|
|
}
|
2019-06-18 19:55:29 +03:00
|
|
|
|
2024-08-29 23:48:59 +03:00
|
|
|
foreach ($this->bridgeFactory->getBridgeClassNames() as $bridgeClassName) {
|
|
|
|
if (!$this->bridgeFactory->isEnabled($bridgeClassName)) {
|
2019-06-18 19:55:29 +03:00
|
|
|
continue;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2019-02-06 20:34:51 +03:00
|
|
|
|
2024-08-29 23:48:59 +03:00
|
|
|
$bridge = $this->bridgeFactory->create($bridgeClassName);
|
2019-02-06 20:34:51 +03:00
|
|
|
|
2024-01-29 23:51:34 +03:00
|
|
|
$bridgeParams = $bridge->detectParameters($url);
|
2019-02-06 20:34:51 +03:00
|
|
|
|
2024-01-29 23:51:34 +03:00
|
|
|
if (!$bridgeParams) {
|
2019-02-06 20:34:51 +03:00
|
|
|
continue;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2019-02-06 20:34:51 +03:00
|
|
|
|
2024-01-29 23:51:34 +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
|
|
|
}
|
|
|
|
|
2024-01-29 23:51:34 +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
|
|
|
}
|
|
|
|
}
|