diff --git a/src/Command/ImportBatteryModelsCommand.php b/src/Command/ImportBatteryModelsCommand.php new file mode 100644 index 00000000..f3321ada --- /dev/null +++ b/src/Command/ImportBatteryModelsCommand.php @@ -0,0 +1,92 @@ +em = $om; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('batterymodel:import') + ->setDescription('Retrieve from a CSV file battery model information.') + ->setHelp('Creates battery models based on data from imported CSV.') + ->addArgument('file', InputArgument::REQUIRED, 'Path to the CSV file.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $csv_file = $input->getArgument('file'); + + // attempt to open file + try + { + $fh = fopen($csv_file, "r"); + } + catch (Exception $e) + { + throw new Exception('The file "' . $csv_file . '" could be read.'); + } + + // get entity manager + $em = $this->em; + + // find the battery model row, as of now, it's 2nd row, same names as the manufacturers + // Possible TODO, this might change + $row_num = 0; + while (($fields = fgetcsv($fh)) !== false) + { + if ($row_num < 1) + { + $row_num++; + continue; + } + + // get battery models when row_num == 1 + if ($row_num == 1) + { + $this->addBatteryModel(trim($fields[self::F_BATT_SDFC])); + $this->addBatteryModel(trim($fields[self::F_BATT_ULTRAMAX])); + $this->addBatteryModel(trim($fields[self::F_BATT_MOTOLITE])); + $this->addBatteryModel(trim($fields[self::F_BATT_MARATHONER])); + $this->addBatteryModel(trim($fields[self::F_BATT_EXCEL])); + } + + break; + } + } + + protected function addBatteryModel($name) + { + $batt_model = new BatteryModel(); + + $batt_model->setName($name); + + $this->em->persist($batt_model); + $this->em->flush(); + } +}