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;
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-07-27 21:01:24 +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-07-27 23:52:48 +00:00
usort($podcasts, fn (array $a, array $b) => new \DateTime((string) $b['last_pub']) <=> new \DateTime((string) $a['last_pub']));
2023-07-27 21:01:24 +00:00
$podcasts = array_intersect_key($podcasts, array_unique(array_map(fn (array $feed) => $feed['feed_url'], $podcasts)));
2023-07-25 23:26:46 +00:00
return new JSONResponse($podcasts);
}
}