RePod-Nextcloud-App/lib/Controller/SearchController.php

45 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\RePod\Controller;
use OCA\GPodderSync\Core\PodcastData\PodcastData;
use OCA\RePod\AppInfo\Application;
use OCA\RePod\Service\FyydService;
use OCA\RePod\Service\ItunesService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use Psr\Log\LoggerInterface;
class SearchController extends Controller
{
public function __construct(
IRequest $request,
private LoggerInterface $logger,
private FyydService $fyydService,
private ItunesService $itunesService
) {
parent::__construct(Application::APP_ID, $request);
}
public function index(string $value): JSONResponse {
$podcasts = [];
$providers = [$this->fyydService, $this->itunesService];
foreach ($providers as $provider) {
try {
$podcasts = [...$podcasts, ...$provider->search($value)];
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), $e->getTrace());
}
}
usort($podcasts, fn (PodcastData $a, PodcastData $b) => $b->getFetchedAtUnix() <=> $a->getFetchedAtUnix());
$podcasts = array_intersect_key($podcasts, array_unique(array_map(fn (PodcastData $feed) => $feed->getLink(), $podcasts)));
return new JSONResponse($podcasts);
}
}