2023-01-01 23:41:40 +01:00
|
|
|
<?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;
|
|
|
|
|
2023-01-02 15:18:52 +01:00
|
|
|
use function array_flip;
|
|
|
|
|
2023-01-01 23:41:40 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2023-01-02 15:18:52 +01:00
|
|
|
trait SelectReportTrait
|
|
|
|
{
|
|
|
|
protected function checkContext()
|
|
|
|
{
|
|
|
|
if (! isset($this->trackingService)) {
|
|
|
|
return false;
|
2023-01-01 23:41:40 +01:00
|
|
|
}
|
|
|
|
return $this instanceof Command;
|
|
|
|
}
|
|
|
|
|
2023-01-02 15:18:52 +01:00
|
|
|
protected function getReportParameter(
|
|
|
|
InputInterface $input,
|
|
|
|
OutputInterface $output,
|
|
|
|
$default = 'tracking_service.youtrack.report.report'
|
|
|
|
) {
|
|
|
|
if (! $this->checkContext()) {
|
2023-01-01 23:41:40 +01:00
|
|
|
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
|
2023-01-02 15:18:52 +01:00
|
|
|
->setErrorMessage('Report %s does not exist!')
|
|
|
|
->setAutocompleterValues($reports);
|
2023-01-01 23:41:40 +01:00
|
|
|
$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();
|
|
|
|
}
|
|
|
|
|
2023-01-02 15:18:52 +01:00
|
|
|
protected function getReportCsvFilePath(
|
|
|
|
InputInterface $input,
|
|
|
|
OutputInterface $output,
|
|
|
|
?string $default = 'tracking_service.youtrack.report.report'
|
|
|
|
): ?string {
|
|
|
|
if (! $file = $input->getOption('file')) {
|
|
|
|
$this->getReportParameter($input, $output, $default);
|
|
|
|
$report_id = $this->trackingService->getReportId();
|
2023-01-01 23:41:40 +01:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|