configurationService = $config; } /** * {@inheritdoc} */ public function getReportData(string $filePath) : array { $output = []; // @TODO replace with config service. // $config = $this->dummyConfig()['projects']; $config = $this->configurationService->get('projects'); foreach (array_keys($config) as $key) { $output[$key] = 0; } if ($file = fopen($filePath, 'r')) { while (($line = fgetcsv($file)) !== false) { $parsed = $this->parseCsvFile($line); // $key = reset(array_keys($parsed)); $key = array_key_first($parsed); if (isset($output[$key])) { $output[$key] += (int) reset($parsed); } } } return $output; } /** * Get correct values from the raw data lines of csv. * * * Columns with data are specified in config. * * Project key and unit of time spent. */ protected function parseCsvFile(array $rawData) : array { $config = $this->configurationService->get('projects'); foreach ($config as $key => $project) { if (preg_match('/' . $project['pattern'] . '/', $rawData[1])) { return [$key => $rawData[4]]; } } return []; } protected function dummyConfig() : array { return [ 'projects' => [ 'LDP' => [ 'name' => 'lupus.digital', 'pattern' => 'LDP-[0-9]+', 'price' => 26, // optional specify columns ], 'WV' => [ 'name' => 'Wirtschaftsverlag', 'pattern' => 'WV-[0-9]+', 'price' => 26, // optional specify columns ], 'Other' => [ 'name' => 'Other projects', 'pattern' => '(?!.\bLDP\b)(?!.\bWV\b)', 'price' => 26, // optional specify columns ], ], ]; } }