RprtCli/app/src/Commands/RprtCommand.php

106 lines
3.0 KiB
PHP

<?php
// src/Commands/RprtCommand.php;
namespace RprtCli\Commands;
use RprtCli\Utils\Configuration\ConfigurationInterface;
use RprtCli\Utils\CsvReport\CsvReportInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputOption;
class RprtCommand extends Command {
protected $csv;
protected $configuration;
public function __construct(CsvReportInterface $csv, ConfigurationInterface $configuration) {
$this->csv = $csv;
$this->configuration = $configuration;
parent::__construct();
}
/**
* 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.');
}
protected function execute(InputInterface $input, OutputInterface $output): int {
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($output, $data) {
$table = new Table($output);
$table->setHeaders(['Project', 'Hours', 'Rate', 'Price']);
list($rows, $total_hours, $total_price) = [[], 0, 0];
$projects_config = $this->configuration->get('projects');
foreach ($projects_config 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 = $hours/60;
}
$price = $hours * (int) $config['price'];
$row = [
$config['name'],
$hours,
$config['price'],
$hours * $config['price'],
];
$rows[] = $row;
$total_hours += $hours;
$total_price += $price;
}
$rows[] = new TableSeparator();
// @TODO Check rate in final result.
$rows[] = ['Zusamen', $total_hours, $config['price'], $total_price];
$table->setRows($rows);
return $table;
}
/**
* Dummy output for testing.
*/
protected function dummyOutput(InputInterface $input, OutputInterface $output): void {
$output->writeln('I will output a nice table.');
$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();
}
}