repod/lib/Controller/SearchController.php

45 lines
1.2 KiB
PHP
Raw Normal View History

2023-07-25 23:26:46 +00:00
<?php
declare(strict_types=1);
namespace OCA\RePod\Controller;
2023-08-22 17:41:17 +00:00
use OCA\GPodderSync\Core\PodcastData\PodcastData;
2023-07-25 23:26:46 +00:00
use OCA\RePod\AppInfo\Application;
2023-07-28 00:37:57 +00:00
use OCA\RePod\Service\FyydService;
use OCA\RePod\Service\ItunesService;
2023-07-25 23:26:46 +00:00
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
2023-07-28 01:09:06 +00:00
use Psr\Log\LoggerInterface;
2023-07-25 23:26:46 +00:00
class SearchController extends Controller
{
public function __construct(
IRequest $request,
2023-07-28 01:09:06 +00:00
private LoggerInterface $logger,
2023-07-28 00:37:57 +00:00
private FyydService $fyydService,
private ItunesService $itunesService
2023-07-25 23:26:46 +00:00
) {
parent::__construct(Application::APP_ID, $request);
}
2023-12-23 16:25:20 +00:00
public function index(string $value): JSONResponse {
2023-07-25 23:26:46 +00:00
$podcasts = [];
2023-07-28 00:37:57 +00:00
$providers = [$this->fyydService, $this->itunesService];
2023-07-25 23:26:46 +00:00
2023-07-28 00:37:57 +00:00
foreach ($providers as $provider) {
try {
$podcasts = [...$podcasts, ...$provider->search($value)];
} catch (\Exception $e) {
2023-07-28 01:09:06 +00:00
$this->logger->error($e->getMessage(), $e->getTrace());
2023-07-27 21:01:24 +00:00
}
}
2023-08-22 18:14:15 +00:00
usort($podcasts, fn (PodcastData $a, PodcastData $b) => $b->getFetchedAtUnix() <=> $a->getFetchedAtUnix());
2023-08-22 17:41:17 +00:00
$podcasts = array_intersect_key($podcasts, array_unique(array_map(fn (PodcastData $feed) => $feed->getLink(), $podcasts)));
2023-07-27 21:01:24 +00:00
2023-07-25 23:26:46 +00:00
return new JSONResponse($podcasts);
}
}