RprtCli/app/src/Commands/RprtCommand.php

160 lines
4.9 KiB
PHP

<?php
declare(strict_types=1);
// src/Commands/RprtCommand.php;
namespace RprtCli\Commands;
use RprtCli\Utils\Configuration\ConfigurationInterface;
use RprtCli\Utils\CsvReport\CsvReportInterface;
use RprtCli\Utils\TimeTrackingServices\YoutrackInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
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 Symfony\Contracts\Translation\TranslatorInterface;
use function var_dump;
/**
* Main file - rprt command.
*/
class RprtCommand extends Command
{
protected $csv;
protected $configuration;
protected $youtrack;
public function __construct(
CsvReportInterface $csv,
ConfigurationInterface $configuration,
YoutrackInterface $youtrack,
?string $name = null
) {
$this->csv = $csv;
$this->configuration = $configuration;
$this->youtrack = $youtrack;
parent::__construct($name);
}
/**
* Get configuration.
*/
protected function configure() : void
{
$this->setName('rprt');
$this->setDescription('Generate monthly report');
// @TODO $this->addUsage('');
$this->addOption(
'file',
'f',
InputOption::VALUE_REQUIRED,
'Specify the input csv file to generate report from.'
);
$this->addOption(
'youtrack',
'y',
InputOption::VALUE_NONE,
'Use youtrack api to get a report. If this option is used --file does not have any effect.'
);
$this->addOption(
'pdf',
'p',
InputOption::VALUE_NONE,
'Create invoice pdf from template.'
);
$this->addOption(
'test',
't',
InputOption::VALUE_NONE,
'Test login into youtrack service.'
);
}
protected function execute(InputInterface $input, OutputInterface $output) : int
{
if ($input->getOption('test')) {
$test = $this->youtrack->testYoutrackapi();
$output->writeln($test);
}
if ($file = $input->getOption('file')) {
$data = $this->csv->getReportData($file);
$table = $this->generateTable($output, $data);
$table->render();
return Command::SUCCESS;
}
$this->dummyOutput($input, $output);
return Command::SUCCESS;
}
/**
* Create table from data that is already inline with configuration.
*/
protected function generateTable(OutputInterface $output, array $data) : Table
{
$table = new Table($output);
$table->setHeaders([
// $this->translator->trans('Project', [], 'messages', 'sl_SI'),
// $this->translator->trans('Hours', [], 'messages', 'sl_SI'),
// $this->translator->trans('Rate'),
// $this->translator->trans('Price'),
'Project', 'Hours', 'Rate', 'Price',
]);
[$rows, $totalHours, $totalPrice] = [[], 0, 0];
$projectsConfig = $this->configuration->get('projects');
foreach ($projectsConfig as $name => $config) {
if (! isset($data[$name])) {
// @TODO Proper error handling.
var_dump('Project ' . $name . ' is not set!');
continue;
}
$hours = $data[$name];
if ($config['time_format'] === 'm') {
$hours /= 60;
}
$price = $hours * (int) $config['price'];
$row = [
$config['name'],
$hours,
$config['price'],
$hours * $config['price'],
];
$rows[] = $row;
$totalHours += $hours;
$totalPrice += $price;
}
$rows[] = new TableSeparator();
// @TODO Check rate in final result.
// $rows[] = [$this->translator->trans('Sum'), $totalHours, $config['price'], $totalPrice];
$rows[] = ['Sum', $totalHours, $config['price'], $totalPrice];
$table->setRows($rows);
return $table;
}
/**
* Dummy output for testing.
*/
protected function dummyOutput(InputInterface $input, OutputInterface $output) : void
{
// $txt = $this->translator->trans('From [start-date] to [end-date].', [], 'rprt', 'sl_SI');
// $output->writeln($txt);
$table = new Table($output);
$table->setHeaders(['Project', 'Hours', 'Price']);
$table->setRows([
['LDP', 100, 2600],
['WV', 50, 1300],
new TableSeparator(),
['Zusamen', 150, 3900],
]);
// $table->setStyle('borderless');
$table->render();
}
}