fix global search provider
This commit is contained in:
parent
0bd0e8cad3
commit
7cddb4ccfa
51
lib/Command/Search.php
Normal file
51
lib/Command/Search.php
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace OCA\Radio\Command;
|
||||||
|
|
||||||
|
use OCA\Radio\Service\SearchService;
|
||||||
|
use OCA\Mail\Service\CleanupService;
|
||||||
|
use Symfony\Component\Console\Command\Command;
|
||||||
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
|
||||||
|
class Search extends Command {
|
||||||
|
|
||||||
|
/** @var CleanupService */
|
||||||
|
private $cleanupService;
|
||||||
|
/**
|
||||||
|
* @var SearchService
|
||||||
|
*/
|
||||||
|
private $searchService;
|
||||||
|
|
||||||
|
public function __construct(SearchService $searchService) {
|
||||||
|
parent::__construct();
|
||||||
|
|
||||||
|
$this->searchService = $searchService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function configure() {
|
||||||
|
$this->setName('radio:search');
|
||||||
|
$this->setDescription('Search for radio stations');
|
||||||
|
|
||||||
|
$this->addArgument("term", InputArgument::OPTIONAL);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||||
|
$radioStations = $this->searchService->findRadioStations(
|
||||||
|
$input->getArgument("term")
|
||||||
|
);
|
||||||
|
|
||||||
|
$output->writeln("Found radio stations");
|
||||||
|
foreach ($radioStations as $radioStation) {
|
||||||
|
$output->writeln("* $radioStation");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
59
lib/Service/SearchService.php
Normal file
59
lib/Service/SearchService.php
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace OCA\Radio\Service;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use OCP\Http\Client\IClientService;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use function array_filter;
|
||||||
|
use function array_map;
|
||||||
|
use function json_decode;
|
||||||
|
use function mb_strpos;
|
||||||
|
use function mb_strtolower;
|
||||||
|
|
||||||
|
class SearchService {
|
||||||
|
|
||||||
|
/** @var IClientService */
|
||||||
|
private $clientService;
|
||||||
|
|
||||||
|
/** @var LoggerInterface */
|
||||||
|
private $logger;
|
||||||
|
|
||||||
|
public function __construct(IClientService $clientService,
|
||||||
|
LoggerInterface $logger) {
|
||||||
|
$this->clientService = $clientService;
|
||||||
|
$this->logger = $logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function findRadioStations(?string $term = null): array {
|
||||||
|
$this->logger->debug('searching radio stations');
|
||||||
|
|
||||||
|
$client = $this->clientService->newClient();
|
||||||
|
|
||||||
|
try {
|
||||||
|
$response = $client->get("https://cat-fact.herokuapp.com/facts?animal_type=cat");
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->logger->error("Could not search for radio stations. Please check connection to radio-browser API.");
|
||||||
|
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
|
||||||
|
$body = $response->getBody();
|
||||||
|
$parsed = json_decode($body, true);
|
||||||
|
|
||||||
|
$mapped = array_map(function(array $radioStation) {
|
||||||
|
return $radioStation['text'];
|
||||||
|
}, $parsed['all']);
|
||||||
|
|
||||||
|
if (empty($term)) {
|
||||||
|
return $mapped;
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_filter($mapped, function(string $radioStation) use ($term) {
|
||||||
|
return mb_strpos(mb_strtolower($radioStation), mb_strtolower($term));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user