diff --git a/src/Command/ImportBatterySizesCommand.php b/src/Command/ImportBatterySizesCommand.php new file mode 100644 index 00000000..07d6cec0 --- /dev/null +++ b/src/Command/ImportBatterySizesCommand.php @@ -0,0 +1,111 @@ +em = $om; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('batterysize:import') + ->setDescription('Retrieve from a CSV file battery size information.') + ->setHelp('Creates battery sizes 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; + $this->bsize_hash = []; + + // go through the file + // data starts at row 3 + $row_num = 0; + while (($fields = fgetcsv($fh)) !== false) + { + if ($row_num < 2) + { + $row_num++; + continue; + } + + // go through the whole row for the sizes + $bsize = trim($fields[self::F_BATT_SIZE_SDFC]); + $this->addBatterySize($bsize); + + $bsize = trim($fields[self::F_BATT_SIZE_ULTRAMAX]); + $this->addBatterySize($bsize); + + $bsize = trim($fields[self::F_BATT_SIZE_MOTOLITE]); + $this->addBatterySize($bsize); + + $bsize = trim($fields[self::F_BATT_SIZE_MARATHONER]); + $this->addBatterySize($bsize); + + $bsize = trim($fields[self::F_BATT_SIZE_EXCEL]); + $this->addBatterySize($bsize); + + } + } + + protected function addBatterySize($name) + { + if (!empty($name)) + { + if (!in_array(strtoupper($name), $this->bsize_hash)) + { + // save to db + $batt_size = new BatterySize(); + + $batt_size->setName($name); + + $this->em->persist($batt_size); + $this->em->flush(); + + + // insert into hash + $this->bsize_hash[] = $name; + } + } + + // do nothing if in array or blank + } +}