RprtCli/app/src/Commands/ReportCommand.php

122 lines
4.0 KiB
PHP

<?php
declare(strict_types=1);
namespace RprtCli\Commands;
use RprtCli\Utils\Configuration\ConfigurationInterface;
use RprtCli\Utils\CsvReport\ReportCsvInterface;
use RprtCli\Utils\TimeTrackingServices\YoutrackInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableCell;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use function is_array;
use function is_null;
class ReportCommand extends Command
{
use SelectReportTrait;
protected $trackingService;
protected $config;
protected $csv;
// phpcs:ignore
public function __construct(ConfigurationInterface $configuration, YoutrackInterface $tracking_service, ReportCsvInterface $csv, ?string $name = null)
{
$this->config = $configuration;
// @TODO generalize tracking service.
$this->trackingService = $tracking_service;
$this->csv = $csv;
parent::__construct($name);
}
protected function configure(): void
{
$this->setName('report');
$this->setDescription('Get a time-tracking report into command line.');
$this->addOption(
'report',
'r',
InputOption::VALUE_OPTIONAL,
'Select a report from list ofo your reports'
);
// Not supported by by ReportCsv service!
// @TODO Build factory for time tracking service (youtrack-csv-export,
// youtrack-api, jira-api, kimai-api (biro.radiostudent.si)).
$this->addOption(
'time-range',
't',
InputOption::VALUE_REQUIRED,
'Calculates report from tracking service work items directly for time range'
);
$this->addOption(
'file',
'f',
InputOption::VALUE_REQUIRED,
'Specify the input csv file to generate report from.'
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
if ($timeRange = $input->getOption('time-range')) {
// @TODO: Implement time range option:
// - Request workTime items from tracking service
// - Filter them, join by issue, project ...
// This will came in other report service that will gather data
// directly from cache.
if ($output->isVerbose()) {
$output->writeln("Time range: {$timeRange}");
}
$output->writeln('<error>This option is not supported yet.</error>');
return Command::FAILURE;
}
// Currently we only support csv download.
$file = $this->getReportCsvFilePath($input, $output);
$report_name = $this->trackingService->getReportName();
$output->writeln("report: <info>{$report_name}</info>");
$data = $this->csv->generateReportTable($file);
$table = $this->buildTable($output, $data);
$table->render();
return Command::SUCCESS;
}
/**
* Builds table from the report csv data.
*
* @TODO: Code duplication with InvoiceCommand::getTable.
*/
protected function buildTable(OutputInterface $output, array $rows): Table
{
$table = new Table($output);
$table->setHeaders([
'Ticket Id',
'Name',
'Time',
'Estimation',
]);
foreach ($rows as $key => $row) {
if (! $row) {
$rows[$key] = new TableSeparator();
} elseif (is_array($row) && is_null($row[0]) && is_null($row[2])) {
// Check which elements in array are null.
$rows[$key] = [
new TableCell($row[1], ['colspan' => 2]),
new TableCell((string) $row[3], ['colspan' => 2]),
];
}
}
$table->setRows($rows);
return $table;
}
}