repod/lib/Controller/SearchController.php
Michel Roux 098db9f09e
All checks were successful
repod / nextcloud (push) Successful in 1m3s
repod / nodejs (push) Successful in 1m20s
Refacto API to be pseudo compatible with gpodder
2023-08-01 23:18:37 +02:00

45 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\RePod\Controller;
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 (array $a, array $b) => new \DateTime((string) $b['lastpub']) <=> new \DateTime((string) $a['lastpub']));
$podcasts = array_intersect_key($podcasts, array_unique(array_map(fn (array $feed) => $feed['url'], $podcasts)));
return new JSONResponse($podcasts);
}
}