RprtCli/app/src/Commands/ReportCommand.php

122 lines
4.1 KiB
PHP
Raw Normal View History

2022-05-11 19:43:05 +02:00
<?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;
2022-05-11 19:43:05 +02:00
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;
2022-05-11 19:43:05 +02:00
2023-01-02 15:18:52 +01:00
class ReportCommand extends Command
{
2023-01-01 23:41:40 +01:00
use SelectReportTrait;
protected YoutrackInterface $trackingService;
2022-05-11 19:43:05 +02:00
protected ConfigurationInterface $config;
2022-05-11 19:43:05 +02:00
protected ReportCsvInterface $csv;
2022-05-11 19:43:05 +02:00
2023-01-02 15:18:52 +01:00
// phpcs:ignore
public function __construct(ConfigurationInterface $configuration, YoutrackInterface $tracking_service, ReportCsvInterface $csv, ?string $name = null)
{
2022-05-11 19:43:05 +02:00
$this->config = $configuration;
// @TODO generalize tracking service.
$this->trackingService = $tracking_service;
$this->csv = $csv;
2022-05-11 19:43:05 +02:00
parent::__construct($name);
}
2023-01-02 22:03:59 +01:00
protected function configure() : void
2023-01-02 15:18:52 +01:00
{
2022-05-11 19:43:05 +02:00
$this->setName('report');
$this->setDescription('Get a time-tracking report into command line.');
$this->addOption(
'report',
'r',
InputOption::VALUE_OPTIONAL,
2023-01-01 23:41:40 +01:00
'Select a report from list ofo your reports'
2022-05-11 19:43:05 +02:00
);
2023-01-01 23:41:40 +01:00
// 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)).
2022-05-11 19:43:05 +02:00
$this->addOption(
'time-range',
't',
InputOption::VALUE_REQUIRED,
'Calculates report from tracking service work items directly for time range'
);
2023-01-01 23:41:40 +01:00
$this->addOption(
'file',
'f',
InputOption::VALUE_REQUIRED,
'Specify the input csv file to generate report from.'
);
2022-05-11 19:43:05 +02:00
}
2023-01-02 22:03:59 +01:00
protected function execute(InputInterface $input, OutputInterface $output) : int
{
2022-05-15 13:04:50 +02:00
if ($timeRange = $input->getOption('time-range')) {
// @TODO: Implement time range option:
// - Request workTime items from tracking service
// - Filter them, join by issue, project ...
2023-01-01 23:41:40 +01:00
// This will came in other report service that will gather data
// directly from cache.
2022-05-15 13:04:50 +02:00
if ($output->isVerbose()) {
$output->writeln("Time range: {$timeRange}");
}
$output->writeln('<error>This option is not supported yet.</error>');
return Command::FAILURE;
}
2022-05-11 19:43:05 +02:00
// Currently we only support csv download.
2023-01-02 15:18:52 +01:00
$file = $this->getReportCsvFilePath($input, $output);
2023-01-01 23:41:40 +01:00
$report_name = $this->trackingService->getReportName();
$output->writeln("report: <info>{$report_name}</info>");
$data = $this->csv->generateReportTable($file);
2022-05-11 19:43:05 +02:00
$table = $this->buildTable($output, $data);
$table->render();
return Command::SUCCESS;
}
/**
* Builds table from the report csv data.
*
* @TODO: Code duplication with InvoiceCommand::getTable.
*/
2023-01-02 22:03:59 +01:00
protected function buildTable(OutputInterface $output, array $rows) : Table
{
2022-05-11 19:43:05 +02:00
$table = new Table($output);
$table->setHeaders([
'Ticket Id',
'Name',
'Time',
'Estimation',
2022-05-11 19:43:05 +02:00
]);
foreach ($rows as $key => $row) {
if (! $row) {
2022-05-11 19:43:05 +02:00
$rows[$key] = new TableSeparator();
} elseif (is_array($row) && is_null($row[0]) && is_null($row[2])) {
// Check which elements in array are null.
2023-01-02 15:18:52 +01:00
$rows[$key] = [
new TableCell($row[1], ['colspan' => 2]),
new TableCell((string) $row[3], ['colspan' => 2]),
];
2022-05-11 19:43:05 +02:00
}
}
$table->setRows($rows);
return $table;
}
}