81 lines
3.3 KiB
PHP
81 lines
3.3 KiB
PHP
|
<?php
|
||
|
|
||
|
declare(strict_types=1);
|
||
|
|
||
|
namespace RprtCli\Commands;
|
||
|
|
||
|
use Symfony\Component\Console\Command\Command;
|
||
|
use Symfony\Component\Console\Input\InputInterface;
|
||
|
use Symfony\Component\Console\Output\OutputInterface;
|
||
|
use Symfony\Component\Console\Question\ChoiceQuestion;
|
||
|
|
||
|
/**
|
||
|
* Trait to select report.
|
||
|
*
|
||
|
* To be used on commands with trackingService property and config property.
|
||
|
* Command must have report input option. It is not the most elegant solution
|
||
|
* but it helps avoiding code duplication.
|
||
|
*/
|
||
|
trait SelectReportTrait {
|
||
|
|
||
|
protected function checkContext() {
|
||
|
if (!isset($this->trackingService)) {
|
||
|
return FALSE;
|
||
|
}
|
||
|
return $this instanceof Command;
|
||
|
}
|
||
|
|
||
|
protected function getReportParameter(InputInterface $input, OutputInterface $output, $default = 'tracking_service.youtrack.report.report') {
|
||
|
if (!$this->checkContext()) {
|
||
|
return Command::FAILURE;
|
||
|
}
|
||
|
$reports = $this->trackingService->listReports();
|
||
|
// Could just parse a csv file or actually get workItems from youtrack ...
|
||
|
if ($input->hasParameterOption('--report') || $input->hasParameterOption('-r')) {
|
||
|
if (! $report_id = $input->getOption('report')) {
|
||
|
// @TODO Make selection nicer.
|
||
|
// QuestionHelper: https://symfony.com/doc/current/components/console/helpers/questionhelper.html
|
||
|
$helper = $this->getHelper('question');
|
||
|
$question = new ChoiceQuestion('Select report:', $reports);
|
||
|
$question
|
||
|
->setErrorMessage('Report %s does not exist!')
|
||
|
->setAutocompleterValues($reports);
|
||
|
$report_id = $helper->ask($input, $output, $question);
|
||
|
$output->writeln('Report ' . $report_id . ' selected.');
|
||
|
}
|
||
|
// If parameter option is not recognised check if report name was given.
|
||
|
if (! isset($reports[$report_id])) {
|
||
|
if (! isset(array_flip($reports)[$report_id])) {
|
||
|
$output->writeln('Non-existing report ' . $report_id . '. Exiting.');
|
||
|
return Command::FAILURE;
|
||
|
}
|
||
|
$report_id = array_flip($reports)[$report_id];
|
||
|
}
|
||
|
$this->trackingService->setReportId($report_id);
|
||
|
} elseif ($report_id = $this->config->get($default)) {
|
||
|
// If report is not set, try getting default report from configuration.
|
||
|
// This is dependant on the config.
|
||
|
$this->trackingService->setReportId($report_id);
|
||
|
}
|
||
|
$this->trackingService->setReportName();
|
||
|
}
|
||
|
|
||
|
protected function getReportCsvFilePath(InputInterface $input, OutputInterface $output, ?string $report_id) : ?string {
|
||
|
if (!$file = $input->getOption('file')) {
|
||
|
$cache_clear_status = $this->trackingService->clearReportCache($report_id);
|
||
|
if ($output->isVerbose()) {
|
||
|
$output->writeln("Report <info>{$report_id}</info> cache cleared, status: {$cache_clear_status}");
|
||
|
}
|
||
|
$file = $this->trackingService->downloadReport($report_id);
|
||
|
if ($output->isVerbose()) {
|
||
|
$output->writeln("Csv file downloaded to: <info>{$file}</info>");
|
||
|
}
|
||
|
} else {
|
||
|
$this->trackingService->setReportName($file);
|
||
|
}
|
||
|
return $file;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|