repod/lib/Controller/FetchController.php
Michel Roux 941e1cabbd
All checks were successful
repod / nextcloud (push) Successful in 1m15s
repod / nodejs (push) Successful in 1m52s
Back ready !
2023-08-22 19:41:17 +02:00

48 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\RePod\Controller;
use OCA\GPodderSync\Core\PodcastData\PodcastData;
use OCA\GPodderSync\Core\PodcastData\PodcastDataReader;
use OCA\RePod\AppInfo\Application;
use OCA\RePod\Service\UserService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\Http\Client\IClientService;
use OCP\IRequest;
class FetchController extends Controller
{
public function __construct(
IRequest $request,
private IClientService $clientService,
private UserService $userService,
private PodcastDataReader $podcastDataReader
) {
parent::__construct(Application::APP_ID, $request);
}
public function index(string $uri): JSONResponse
{
$podcastData = $this->podcastDataReader->getCachedOrFetchPodcastData($uri, $this->userService->getUserUID());
if ($podcastData) {
return new JSONResponse($podcastData->toArray());
}
$client = $this->clientService->newClient();
$feed = $client->get($uri);
$statusCode = $feed->getStatusCode();
if ($statusCode < 200 || $statusCode >= 300) {
throw new \ErrorException("Web request returned non-2xx status code: {$statusCode}");
}
$podcastData = PodcastData::parseRssXml((string) $feed->getBody());
return new JSONResponse($podcastData->toArray());
}
}