repod/lib/Controller/PodcastController.php
Michel Roux 3f9bc372ce
All checks were successful
repod / nextcloud (push) Successful in 1m51s
repod / nodejs (push) Successful in 2m43s
EpisodeActionExtra done
2023-08-24 12:48:10 +02:00

48 lines
1.2 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 PodcastController 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 $url): JSONResponse
{
$podcasts = $this->podcastDataReader->getCachedOrFetchPodcastData($url, $this->userService->getUserUID());
if ($podcasts) {
return new JSONResponse(['data' => $podcasts]);
}
$client = $this->clientService->newClient();
$feed = $client->get($url);
$statusCode = $feed->getStatusCode();
if ($statusCode < 200 || $statusCode >= 300) {
throw new \ErrorException("Web request returned non-2xx status code: {$statusCode}");
}
$podcasts = PodcastData::parseRssXml((string) $feed->getBody());
return new JSONResponse(['data' => $podcasts]);
}
}