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

45 lines
1.2 KiB
PHP
Raw Normal View History

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