repod/lib/Controller/SearchController.php
Michel Roux f188181528
All checks were successful
repod / nextcloud (push) Successful in 44s
repod / nodejs (push) Successful in 1m17s
Fix itunes bug and add logger
2023-07-28 03:09:06 +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['last_pub']) <=> new \DateTime((string) $a['last_pub']));
$podcasts = array_intersect_key($podcasts, array_unique(array_map(fn (array $feed) => $feed['feed_url'], $podcasts)));
return new JSONResponse($podcasts);
}
}