Files
watchstate/src/Commands/Backend/Search/IdCommand.php

84 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Commands\Backend\Search;
use App\Command;
use App\Libs\Config;
use App\Libs\Options;
use RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Yaml;
final class IdCommand extends Command
{
protected function configure(): void
{
$this->setName('backend:search:id')
->setDescription('Get backend metadata related to specific id.')
->addOption('include-raw-response', null, InputOption::VALUE_NONE, 'Include unfiltered raw response.')
->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'Use Alternative config file.')
->addArgument('backend', InputArgument::REQUIRED, 'Backend name.')
->addArgument('id', InputArgument::REQUIRED, 'Item id.');
}
protected function runCommand(InputInterface $input, OutputInterface $output): int
{
$mode = $input->getOption('output');
$id = $input->getArgument('id');
// -- Use Custom servers.yaml file.
if (($config = $input->getOption('config'))) {
try {
Config::save('servers', Yaml::parseFile($this->checkCustomServersFile($config)));
} catch (RuntimeException $e) {
$arr = [
'error' => $e->getMessage()
];
$this->displayContent('table' === $mode ? [$arr] : $arr, $output, $mode);
return self::FAILURE;
}
}
try {
$backend = $this->getBackend($input->getArgument('backend'));
$opts = [];
if ($input->getOption('include-raw-response')) {
$opts[Options::RAW_RESPONSE] = true;
}
$results = $backend->searchId(id: $id, opts: $opts);
if (count($results) < 1) {
$arr = [
'info' => sprintf('%s: No results were found for this id #\'%s\' .', $backend->getName(), $id),
];
$this->displayContent('table' === $mode ? [$arr] : $arr, $output, $mode);
return self::FAILURE;
}
$this->displayContent('table' === $mode ? [$results] : $results, $output, $mode);
return self::SUCCESS;
} catch (RuntimeException $e) {
$arr = [
'error' => $e->getMessage(),
];
if ('table' !== $mode) {
$arr += [
'file' => $e->getFile(),
'line' => $e->getLine(),
];
}
$this->displayContent('table' === $mode ? [$arr] : $arr, $output, $mode);
return self::FAILURE;
}
}
}