Fix configuration service injection.

master
Lio Novelli 2021-04-05 20:05:05 +02:00
parent b6bae7cc9a
commit 201934a852
4 changed files with 51 additions and 28 deletions

View File

@ -8,24 +8,25 @@ use RprtCli\Utils\Configuration\ConfigurationInterface;
use RprtCli\Utils\Configuration\ConfigurationService; use RprtCli\Utils\Configuration\ConfigurationService;
use RprtCli\Utils\CsvReport\CsvReport; use RprtCli\Utils\CsvReport\CsvReport;
use RprtCli\Utils\CsvReport\CsvReportInterface; use RprtCli\Utils\CsvReport\CsvReportInterface;
use Symfony\Component\Yaml\Yaml;
use GuzzleHttp\Client; use GuzzleHttp\Client;
return [ return [
'config.file' => 'rprt.config.yml', 'config.file' => 'rprt.config.yml',
'config.path' => '~/.config/rprt-cli/', 'config.path' => '~/.config/rprt-cli/',
'guzzle' => create()->constructor(\GuzzleHttp\Client::class), 'guzzle' => create()->constructor(Client::class),
'yaml' => create()->constructor(\Symfony\Component\Yaml\Yaml::class),
\GuzzleHttp\ClientInterface::class => DI\get(\GuzzleHttp\Client::class),
CsvReportInterface::class => DI\get(CsvReport::class),
ConfigurationInterface::class => get(ConfigurationService::class), ConfigurationInterface::class => get(ConfigurationService::class),
ConfigurationService::class => create()->constructor( ConfigurationService::class => create()->constructor(
get('config.path'), get('config.path'),
get('config.file'), get('config.file')
get('yaml')
), ),
RprtCommand::class => DI\create()->constructor( 'config.service' => get(ConfigurationInterface::class),
get(CsvReportInterface::class), CsvReportInterface::class => get(CsvReport::class),
get(ConfigurationInterface::class) CsvReport::class => create()->constructor(
get('config.service')
),
'csv.report' => get(CsvReportInterface::class),
RprtCommand::class => create()->constructor(
get('csv.report'),
get('config.service')
) )
]; ];

View File

@ -49,20 +49,34 @@ class RprtCommand extends Command {
} }
protected function generateTable($output, $data) { protected function generateTable($output, $data) {
// @TODO - get configuration
$table = new Table($output); $table = new Table($output);
$table->setHeaders(['Project', 'Hours', 'Rate', 'Price']); $table->setHeaders(['Project', 'Hours', 'Rate', 'Price']);
$rows = []; list($rows, $total_hours, $total_price) = [[], 0, 0];
$together = 0; $projects_config = $this->configuration->get('projects');
$config = $this->configuration->get('projects'); var_dump($projects_config);
var_dump($config); foreach ($projects_config as $name => $config) {
foreach ($data as $project => $hours) { if (!isset($data[$name])) {
$row = [$project, $hours/60]; var_dump('Project ' . $name . ' is not set!');
$together += $hours / 60; 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; $rows[] = $row;
$total_hours += $hours;
$total_price += $price;
} }
$rows[] = new TableSeparator(); $rows[] = new TableSeparator();
$rows[] = ['Zusamen', $together]; // @TODO Check rate in final result.
$rows[] = ['Zusamen', $total_hours, $config['price'], $total_price];
$table->setRows($rows); $table->setRows($rows);
return $table; return $table;
} }

View File

@ -35,21 +35,20 @@ class ConfigurationService implements ConfigurationInterface
/** /**
* Yaml service. * Yaml service.
* *
* @var \Symfony\Component\Yaml\Yaml; * @var \Symfony\Component\Yaml\Yaml::parseFile;
*/ */
protected $yaml; protected $yamlParseFile;
/** /**
* Construct method. * Construct method.
*/ */
function __construct(string $filepath, string $filename, Yaml $yaml) { function __construct(string $filepath, string $filename) {
$file = $filepath . $filename; $file = $filepath . $filename;
$this->configFileName = $filename; $this->configFileName = $filename;
$this->configFilePath = $this->findConfig($file); $this->configFilePath = $this->findConfig($file);
if ($this->configFilePath) { if ($this->configFilePath) {
$this->getConfig(); $this->getConfig();
} }
$this->yaml = $yaml;
} }
/** /**
@ -79,7 +78,7 @@ class ConfigurationService implements ConfigurationInterface
public function getConfig() { public function getConfig() {
if ($this->configFilePath) { if ($this->configFilePath) {
var_dump($this->configFilePath); var_dump($this->configFilePath);
$config = $this->yaml->parseFile($this->configFilePath); $config = Yaml::parseFile($this->configFilePath);
$this->data = $config; $this->data = $config;
return TRUE; return TRUE;
} }

View File

@ -2,15 +2,23 @@
namespace RprtCli\Utils\CsvReport; namespace RprtCli\Utils\CsvReport;
use RprtCli\Utils\Configuration\ConfigurationInterface;
use RprtCli\Utils\CsvReport\CsvReportInterface; use RprtCli\Utils\CsvReport\CsvReportInterface;
class CsvReport implements CsvReportInterface { class CsvReport implements CsvReportInterface {
protected $configurationService;
function __construct(ConfigurationInterface $config) {
$this->configurationService = $config;
}
public function getReportData(string $file_path): array { public function getReportData(string $file_path): array {
$output = []; $output = [];
// @TODO replace with config service. // @TODO replace with config service.
$config = $this->dummyConfig(); // $config = $this->dummyConfig()['projects'];
foreach (array_keys($config['projects']) as $key) { $config = $this->configurationService->get('projects');
foreach (array_keys($config) as $key) {
$output[$key] = 0; $output[$key] = 0;
} }
if ($file = fopen($file_path, 'r')) { if ($file = fopen($file_path, 'r')) {
@ -27,9 +35,10 @@ class CsvReport implements CsvReportInterface {
} }
protected function parseCsvFile(array $raw_data): array { protected function parseCsvFile(array $raw_data): array {
$config = $this->dummyConfig(); // $config = $this->dummyConfig();
$config = $this->configurationService->get('projects');
// var_dump($raw_data); // var_dump($raw_data);
foreach ($config['projects'] as $key => $project) { foreach ($config as $key => $project) {
if (preg_match('/'.$project['pattern'].'/', $raw_data[1])) { if (preg_match('/'.$project['pattern'].'/', $raw_data[1])) {
return [$key => $raw_data[4]]; return [$key => $raw_data[4]];
} }