From f498315b538ade9a2d139d39f52a4e443bfb3b9d Mon Sep 17 00:00:00 2001 From: Lio Novelli Date: Sun, 15 May 2022 11:55:50 +0200 Subject: [PATCH] Standardize input options, some verbosity, separator constants. --- .gitignore | 1 + app/src/Commands/InvoiceCommand.php | 12 +++++++++--- app/src/Commands/ReportCommand.php | 17 +++++++++++++++-- app/src/Utils/CsvReport/ReportCsv.php | 16 ++++++++-------- app/src/Utils/CsvReport/ReportCsvInterface.php | 16 ++++++++++++++++ app/src/Utils/PdfExport/PdfExportService.php | 5 +++-- .../TimeTrackingServices/YoutrackService.php | 7 ++++--- 7 files changed, 56 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 4e3dce7..7a91d23 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /resources/data /scratch .phpcs-cache +*~undo-tree~ diff --git a/app/src/Commands/InvoiceCommand.php b/app/src/Commands/InvoiceCommand.php index 35a46df..91c7595 100644 --- a/app/src/Commands/InvoiceCommand.php +++ b/app/src/Commands/InvoiceCommand.php @@ -103,7 +103,7 @@ class InvoiceCommand extends Command ); $this->addOption( 'recipients', - 'r', + 't', InputOption::VALUE_REQUIRED, 'Comma separated list of recipients that should get the exported pdf.' ); @@ -129,7 +129,7 @@ class InvoiceCommand extends Command ); $this->addOption( 'report', - 't', + 'r', InputOption::VALUE_OPTIONAL, 'Show time tracked for report.', FALSE @@ -164,6 +164,10 @@ class InvoiceCommand extends Command } if ($youtrack = $input->getOption('youtrack')) { $report_id = $this->youtrack->getReportId(); + $cache_clear_status = $this->youtrack->clearReportCache($report_id); + if ($output->isVerbose()) { + $output->writeln("Report cache cleared, status: {$cache_clear_status}"); + } $file = $this->youtrack->downloadReport($report_id); } if ($input->hasParameterOption('--expenses') || $input->hasParameterOption('-e')) { @@ -174,7 +178,9 @@ class InvoiceCommand extends Command } if ($youtrack || $file = $input->getOption('file')) { // Youtrack can also provide a file name. - // var_dump($file); + if ($output->isVerbose()) { + $output->writeln("Csv file downloaded to: {$file}"); + } $data = $this->csv->getInvoiceData($file); if (!empty($expenses)) { $data = array_merge($data, $expenses); diff --git a/app/src/Commands/ReportCommand.php b/app/src/Commands/ReportCommand.php index 67d54ea..2e8cf3d 100644 --- a/app/src/Commands/ReportCommand.php +++ b/app/src/Commands/ReportCommand.php @@ -73,12 +73,25 @@ class ReportCommand extends Command { elseif ($report = $this->config->get('tracking_service.youtrack.report.default')) { $this->trackingService->setReportId($report); } + elseif ($timeRange = $input->getParameterOption('time-range')) { + // @TODO: Implement time range option: + // - Request workTime items from tracking service + // - Filter them, join by issue, project ... + $output->writeln('This option is not supported yet.'); + return Command::FAILURE; + } // Currently we only support csv download. $report_id = $this->trackingService->getReportId(); $report_name = $reports[$report_id]; - $this->trackingService->clearReportCache($report_id); + // Code duplication. + $cache_clear_status = $this->timeTrackingService->clearReportCache($report_id); + if ($output->isVerbose()) { + $output->writeln("Report cache cleared, status: {$cache_clear_status}"); + } $file = $this->trackingService->downloadReport($report_id); - // var_dump($file); + if ($output->isVerbose()) { + $output->writeln("Csv file downloaded to: {$file}"); + } $output->writeln("report: {$report_name}"); $data = $this->csv->generateReportTable($file); $table = $this->buildTable($output, $data); diff --git a/app/src/Utils/CsvReport/ReportCsv.php b/app/src/Utils/CsvReport/ReportCsv.php index e331ac2..f33d326 100644 --- a/app/src/Utils/CsvReport/ReportCsv.php +++ b/app/src/Utils/CsvReport/ReportCsv.php @@ -128,8 +128,8 @@ class ReportCsv implements ReportCsvInterface foreach ($data as $invoice_element) { if ($invoice_element instanceof ExpensesInterface) { if (!isset($added_expenses)) { - // @TODO - separator 0: Make next line bold and centered. - $rows[] = 0; + // Separator 0: Make next line bold and centered. + $rows[] = ReportCsvInterface::SEPARATOR_HARD; $rows[] = [ null, null, @@ -137,7 +137,7 @@ class ReportCsv implements ReportCsvInterface 'EUR', ]; // Don't make next line bold. See RprtCli\PdfExport\PdfExportService::parsedDataToHtml. - $rows[] = FALSE; + $rows[] = ReportCsvInterface::SEPARATOR_SOFT; $added_expenses = TRUE; } $add_separator = TRUE; @@ -151,7 +151,7 @@ class ReportCsv implements ReportCsvInterface } } if ($add_separator) { - $rows[] = null; + $rows[] = ReportCsvInterface::SEPARATOR_MEDIUM; } $rows[] = [null, null, 'Gessamt brutto', number_format($totalPrice, 2, ',', '.')]; return $rows; @@ -184,9 +184,9 @@ class ReportCsv implements ReportCsvInterface $previous_project = explode('-', $previous)[0]; if ($project !== $previous_project) { // When project changes, add a sum of time for that project. - $table[] = null; + $table[] = ReportCsvInterface::SEPARATOR_MEDIUM; $table[] = [null, $previous_project, null, $project_time/60]; - $table[] = null; + $table[] = ReportCsvInterface::SEPARATOR_MEDIUM; $time_sum += (float) $project_time; $project_time = 0; $all_projects[] = $project; @@ -196,12 +196,12 @@ class ReportCsv implements ReportCsvInterface $table[] = array_values($line); } // Add sum for the last project. - $table[] = null; + $table[] = ReportCsvInterface::SEPARATOR_MEDIUM; $table[] = [null, $project, null, $project_time / 60]; $time_sum += (float) $project_time; $all_projects[] = $project; // Add a sum of time for whole day. - $table[] = null; + $table[] = ReportCsvInterface::SEPARATOR_MEDIUM; $table[] = [null, implode(', ', $all_projects), null, $time_sum/60]; return $table; } diff --git a/app/src/Utils/CsvReport/ReportCsvInterface.php b/app/src/Utils/CsvReport/ReportCsvInterface.php index 75adc85..501a29c 100644 --- a/app/src/Utils/CsvReport/ReportCsvInterface.php +++ b/app/src/Utils/CsvReport/ReportCsvInterface.php @@ -9,6 +9,22 @@ namespace RprtCli\Utils\CsvReport; */ interface ReportCsvInterface { + + /** + * Normal separator. + */ + const SEPARATOR_SOFT = FALSE; + + /** + * Medium separator. Next line bold. + */ + const SEPARATOR_MEDIUM = NULL; + + /** + * Next line should be bold and centered. + */ + const SEPARATOR_HARD = 0; + /** * Returns array of hours per configured projects. * diff --git a/app/src/Utils/PdfExport/PdfExportService.php b/app/src/Utils/PdfExport/PdfExportService.php index 6ef2185..ac41cda 100644 --- a/app/src/Utils/PdfExport/PdfExportService.php +++ b/app/src/Utils/PdfExport/PdfExportService.php @@ -7,6 +7,7 @@ namespace RprtCli\Utils\PdfExport; use Exception; use RprtCli\Utils\Configuration\ConfigurationInterface; use Mpdf\Output\Destination; +use RprtCli\Utils\CsvReport\ReportCsvInterface; class PdfExportService implements PdfExportInterface { @@ -59,10 +60,10 @@ class PdfExportService implements PdfExportInterface { $table .= ''; foreach ($data as $row_index => $row) { if (!$row) { - if ($row === NULL) { + if ($row === ReportCsvInterface::SEPARATOR_MEDIUM) { $classes = 'bold'; } - elseif ($row === 0) { + elseif ($row === ReportCsvInterface::SEPARATOR_HARD) { $classes = 'bold center'; } continue; diff --git a/app/src/Utils/TimeTrackingServices/YoutrackService.php b/app/src/Utils/TimeTrackingServices/YoutrackService.php index e3bc97f..df184ac 100644 --- a/app/src/Utils/TimeTrackingServices/YoutrackService.php +++ b/app/src/Utils/TimeTrackingServices/YoutrackService.php @@ -104,7 +104,7 @@ class YoutrackService implements YoutrackInterface ]); // Write csv response test into temporary file. - $csv_file = tempnam(sys_get_temp_dir(), 'yt_csv_'); + $csv_file = tempnam(sys_get_temp_dir(), "rprt-cli/{$report_id}_"); file_put_contents($csv_file, $csv_response->getBody()); return $csv_file; } @@ -113,7 +113,7 @@ class YoutrackService implements YoutrackInterface if (isset($this->ytToken)) { return $this->ytToken; } - $yt_token = $this->config->get('tracking_service.youtrack.auth_token', FALSE); // + $yt_token = $this->config->get('tracking_service.youtrack.auth_token', FALSE); if (!$yt_token) { $yt_token = readline('Enter your youtrack authentication token: '); } @@ -184,8 +184,9 @@ class YoutrackService implements YoutrackInterface 'headers' => $this->getHeaders(), 'json' => $post, ]); - $body = (string) $response->getBody()->getContents(); + // $body = (string) $response->getBody()->getContents(); // var_dump($body); + return $response->getStatusCode(); } }