From 201934a8524c9f90f7c7dbe17862d7db16ec558b Mon Sep 17 00:00:00 2001 From: Lio Novelli Date: Mon, 5 Apr 2021 20:05:05 +0200 Subject: [PATCH] Fix configuration service injection. --- app/dependencies.php | 21 ++++++------ app/src/Commands/RprtCommand.php | 32 +++++++++++++------ .../Configuration/ConfigurationService.php | 9 +++--- app/src/Utils/CsvReport/CsvReport.php | 17 +++++++--- 4 files changed, 51 insertions(+), 28 deletions(-) diff --git a/app/dependencies.php b/app/dependencies.php index d19aacc..0ee65f9 100644 --- a/app/dependencies.php +++ b/app/dependencies.php @@ -8,24 +8,25 @@ use RprtCli\Utils\Configuration\ConfigurationInterface; use RprtCli\Utils\Configuration\ConfigurationService; use RprtCli\Utils\CsvReport\CsvReport; use RprtCli\Utils\CsvReport\CsvReportInterface; -use Symfony\Component\Yaml\Yaml; use GuzzleHttp\Client; return [ 'config.file' => 'rprt.config.yml', 'config.path' => '~/.config/rprt-cli/', - 'guzzle' => create()->constructor(\GuzzleHttp\Client::class), - 'yaml' => create()->constructor(\Symfony\Component\Yaml\Yaml::class), - \GuzzleHttp\ClientInterface::class => DI\get(\GuzzleHttp\Client::class), - CsvReportInterface::class => DI\get(CsvReport::class), + 'guzzle' => create()->constructor(Client::class), ConfigurationInterface::class => get(ConfigurationService::class), ConfigurationService::class => create()->constructor( get('config.path'), - get('config.file'), - get('yaml') + get('config.file') ), - RprtCommand::class => DI\create()->constructor( - get(CsvReportInterface::class), - get(ConfigurationInterface::class) + 'config.service' => get(ConfigurationInterface::class), + CsvReportInterface::class => get(CsvReport::class), + CsvReport::class => create()->constructor( + get('config.service') + ), + 'csv.report' => get(CsvReportInterface::class), + RprtCommand::class => create()->constructor( + get('csv.report'), + get('config.service') ) ]; diff --git a/app/src/Commands/RprtCommand.php b/app/src/Commands/RprtCommand.php index 6ceae71..eed27a9 100644 --- a/app/src/Commands/RprtCommand.php +++ b/app/src/Commands/RprtCommand.php @@ -49,20 +49,34 @@ class RprtCommand extends Command { } protected function generateTable($output, $data) { - // @TODO - get configuration $table = new Table($output); $table->setHeaders(['Project', 'Hours', 'Rate', 'Price']); - $rows = []; - $together = 0; - $config = $this->configuration->get('projects'); - var_dump($config); - foreach ($data as $project => $hours) { - $row = [$project, $hours/60]; - $together += $hours / 60; + list($rows, $total_hours, $total_price) = [[], 0, 0]; + $projects_config = $this->configuration->get('projects'); + var_dump($projects_config); + foreach ($projects_config as $name => $config) { + if (!isset($data[$name])) { + 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(); - $rows[] = ['Zusamen', $together]; + // @TODO Check rate in final result. + $rows[] = ['Zusamen', $total_hours, $config['price'], $total_price]; $table->setRows($rows); return $table; } diff --git a/app/src/Utils/Configuration/ConfigurationService.php b/app/src/Utils/Configuration/ConfigurationService.php index 50c8792..e621b2f 100644 --- a/app/src/Utils/Configuration/ConfigurationService.php +++ b/app/src/Utils/Configuration/ConfigurationService.php @@ -35,21 +35,20 @@ class ConfigurationService implements ConfigurationInterface /** * Yaml service. * - * @var \Symfony\Component\Yaml\Yaml; + * @var \Symfony\Component\Yaml\Yaml::parseFile; */ - protected $yaml; + protected $yamlParseFile; /** * Construct method. */ - function __construct(string $filepath, string $filename, Yaml $yaml) { + function __construct(string $filepath, string $filename) { $file = $filepath . $filename; $this->configFileName = $filename; $this->configFilePath = $this->findConfig($file); if ($this->configFilePath) { $this->getConfig(); } - $this->yaml = $yaml; } /** @@ -79,7 +78,7 @@ class ConfigurationService implements ConfigurationInterface public function getConfig() { if ($this->configFilePath) { var_dump($this->configFilePath); - $config = $this->yaml->parseFile($this->configFilePath); + $config = Yaml::parseFile($this->configFilePath); $this->data = $config; return TRUE; } diff --git a/app/src/Utils/CsvReport/CsvReport.php b/app/src/Utils/CsvReport/CsvReport.php index 1c0ebfd..bcb3dd0 100644 --- a/app/src/Utils/CsvReport/CsvReport.php +++ b/app/src/Utils/CsvReport/CsvReport.php @@ -2,15 +2,23 @@ namespace RprtCli\Utils\CsvReport; +use RprtCli\Utils\Configuration\ConfigurationInterface; use RprtCli\Utils\CsvReport\CsvReportInterface; class CsvReport implements CsvReportInterface { + protected $configurationService; + + function __construct(ConfigurationInterface $config) { + $this->configurationService = $config; + } + public function getReportData(string $file_path): array { $output = []; // @TODO replace with config service. - $config = $this->dummyConfig(); - foreach (array_keys($config['projects']) as $key) { + // $config = $this->dummyConfig()['projects']; + $config = $this->configurationService->get('projects'); + foreach (array_keys($config) as $key) { $output[$key] = 0; } if ($file = fopen($file_path, 'r')) { @@ -27,9 +35,10 @@ class CsvReport implements CsvReportInterface { } protected function parseCsvFile(array $raw_data): array { - $config = $this->dummyConfig(); + // $config = $this->dummyConfig(); + $config = $this->configurationService->get('projects'); // var_dump($raw_data); - foreach ($config['projects'] as $key => $project) { + foreach ($config as $key => $project) { if (preg_match('/'.$project['pattern'].'/', $raw_data[1])) { return [$key => $raw_data[4]]; }