From 54c9871189267df8821424a9c5cffe7e6c4f3ed4 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 2 Oct 2019 11:27:10 +0000 Subject: [PATCH] merge import batter commands under one command. #270 --- src/Command/ImportBatteriesCommand.php | 174 --------- .../ImportBatteryManufacturersCommand.php | 90 ----- src/Command/ImportBatteryModelsCommand.php | 92 ----- src/Command/ImportBatterySizesCommand.php | 111 ------ src/Command/ImportCMBBatteryDataCommand.php | 355 ++++++++++++++++++ 5 files changed, 355 insertions(+), 467 deletions(-) delete mode 100644 src/Command/ImportBatteriesCommand.php delete mode 100644 src/Command/ImportBatteryManufacturersCommand.php delete mode 100644 src/Command/ImportBatteryModelsCommand.php delete mode 100644 src/Command/ImportBatterySizesCommand.php create mode 100644 src/Command/ImportCMBBatteryDataCommand.php diff --git a/src/Command/ImportBatteriesCommand.php b/src/Command/ImportBatteriesCommand.php deleted file mode 100644 index a7c78cc3..00000000 --- a/src/Command/ImportBatteriesCommand.php +++ /dev/null @@ -1,174 +0,0 @@ -em = $om; - - $this->loadBatteryManufacturers(); - $this->loadBatteryModels(); - $this->loadBatterySizes(); - - parent::__construct(); - } - - protected function configure() - { - $this->setName('battery:create') - ->setDescription('Retrieve from a CSV file battery information.') - ->setHelp('Creates batteries 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; - - // go through the file - // data starts at row 3 - $row_num = 0; - $sdfc = ''; - $ultra = ''; - $motolite = ''; - $marathoner = ''; - $excel = ''; - - while (($fields = fgetcsv($fh)) !== false) - { - if ($row_num < 1) - { - $row_num++; - continue; - } - - // get manufacturer names - if ($row_num == 1) - { - $sdfc = trim($fields[self::F_BATT_SDFC]); - $ultra = trim($fields[self::F_BATT_ULTRAMAX]); - $motolite = trim($fields[self::F_BATT_MOTOLITE]); - $marathoner = trim($fields[self::F_BATT_MARATHONER]); - $excel = trim($fields[self::F_BATT_EXCEL]); - - $row_num++; - } - else - { - $sdfc_size = trim($fields[self::F_BATT_SDFC]); - $ultra_size = trim($fields[self::F_BATT_ULTRAMAX]); - $motolite_size = trim($fields[self::F_BATT_MOTOLITE]); - $marathoner_size = trim($fields[self::F_BATT_MARATHONER]); - $excel_size = trim($fields[self::F_BATT_EXCEL]); - - //$output->writeln('moogle batt brand ' . $sdfc); - //$output->writeln('moogle batt size ' . $sdfc_size); - - // check if battery has been added - if (!isset($this->batt_hash[$sdfc][$sdfc][$sdfc_size])) - { - //$output->writeln('moogle new batt'); - if (!(empty($sdfc_size))) - { - //$output->writeln('moogle about to add'); - $this->addBattery($sdfc, $sdfc_size); - } - } - } - } - } - - protected function addBattery($brand, $size) - { - // save to db - $battery = new Battery(); - $battery->setManufacturer($this->bmanu_hash[$brand]) - ->setModel($this->bmodel_hash[$brand]) - ->setSize($this->bsize_hash[$size]) - ->setWarrantyPrivate(21) - ->setWarrantyCommercial(6) - ->setWarrantyTnv(12); - - $this->em->persist($battery); - $this->em->flush(); - - - // insert into hash - $this->batt_hash[$brand][$brand][$size] = $battery; - } - - protected function loadBatteryManufacturers() - { - $this->bmanu_hash = []; - - $batt_manufacturers = $this->em->getRepository(BatteryManufacturer::class)->findAll(); - foreach ($batt_manufacturers as $batt_manu) - { - $name = $batt_manu->getName(); - $this->bmanu_hash[$name] = $batt_manu; - } - } - - protected function loadBatteryModels() - { - $this->bmodel_hash = []; - - $batt_models = $this->em->getRepository(BatteryModel::class)->findAll(); - foreach ($batt_models as $batt_model) - { - $name = $batt_model->getName(); - $this->bmodel_hash[$name] = $batt_model; - } - } - - protected function loadBatterySizes() - { - $this->bsize_hash = []; - - $batt_sizes = $this->em->getRepository(BatterySize::class)->findAll(); - foreach ($batt_sizes as $batt_size) - { - $name = $batt_size->getName(); - $this->bsize_hash[$name] = $batt_size; - } - } -} diff --git a/src/Command/ImportBatteryManufacturersCommand.php b/src/Command/ImportBatteryManufacturersCommand.php deleted file mode 100644 index 24c2785c..00000000 --- a/src/Command/ImportBatteryManufacturersCommand.php +++ /dev/null @@ -1,90 +0,0 @@ -em = $om; - - parent::__construct(); - } - - protected function configure() - { - $this->setName('batterymanufacturer:import') - ->setDescription('Retrieve from a CSV file battery manufacturer information.') - ->setHelp('Creates battery manufacturers 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 manufacturer row = 2nd row - $row_num = 0; - while (($fields = fgetcsv($fh)) !== false) - { - if ($row_num < 1) - { - $row_num++; - continue; - } - - // get battery manufacturer when row_num == 1 - if ($row_num == 1) - { - $this->addBatteryManufacturer(trim($fields[self::F_BATT_SDFC])); - $this->addBatteryManufacturer(trim($fields[self::F_BATT_ULTRAMAX])); - $this->addBatteryManufacturer(trim($fields[self::F_BATT_MOTOLITE])); - $this->addBatteryManufacturer(trim($fields[self::F_BATT_MARATHONER])); - $this->addBatteryManufacturer(trim($fields[self::F_BATT_EXCEL])); - } - - break; - } - } - - protected function addBatteryManufacturer($name) - { - $batt_manufacturer = new BatteryManufacturer(); - - $batt_manufacturer->setName($name); - - $this->em->persist($batt_manufacturer); - $this->em->flush(); - } -} diff --git a/src/Command/ImportBatteryModelsCommand.php b/src/Command/ImportBatteryModelsCommand.php deleted file mode 100644 index f3321ada..00000000 --- a/src/Command/ImportBatteryModelsCommand.php +++ /dev/null @@ -1,92 +0,0 @@ -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(); - } -} diff --git a/src/Command/ImportBatterySizesCommand.php b/src/Command/ImportBatterySizesCommand.php deleted file mode 100644 index 07d6cec0..00000000 --- a/src/Command/ImportBatterySizesCommand.php +++ /dev/null @@ -1,111 +0,0 @@ -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 - } -} diff --git a/src/Command/ImportCMBBatteryDataCommand.php b/src/Command/ImportCMBBatteryDataCommand.php new file mode 100644 index 00000000..2d2f699f --- /dev/null +++ b/src/Command/ImportCMBBatteryDataCommand.php @@ -0,0 +1,355 @@ +em = $om; + + // load existing battery data + $this->loadBatteryManufacturers(); + $this->loadBatteryModels(); + $this->loadBatterySizes(); + $this->loadBatteries(); + + // load existing vehicle data + $this->loadVehicleManufacturers(); + $this->loadVehicleMakes(); + + parent::__construct(); + } + + protected function configure() + { + $this->setName('cmbbatterydata:import') + ->setDescription('Retrieve from a CSV file battery and vehicle information.') + ->setHelp('Creates battery manufacturers, models, sizes, vehicle makes, and 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 manufacturer row = 2nd row + $row_num = 0; + + // brand names + $brand_sdfc = ''; + $brand_ultramax = ''; + $brand_motolite = ''; + $brand_marathoner = ''; + $brand_excel = ''; + + while (($fields = fgetcsv($fh)) !== false) + { + if ($row_num < 1) + { + $row_num++; + continue; + } + + $sdfc = trim($fields[self::F_BATT_SDFC]); + $ultramax = trim($fields[self::F_BATT_ULTRAMAX]); + $motolite = trim($fields[self::F_BATT_MOTOLITE]); + $marathoner = trim($fields[self::F_BATT_MARATHONER]); + $excel = trim($fields[self::F_BATT_EXCEL]); + + // get battery manufacturer when row_num == 1 + if ($row_num == 1) + { + // store the brand names to be used when adding a battery + $brand_sdfc = $sdfc; + $brand_ultramax = $ultramax; + $brand_motolite = $motolite; + $brand_marathoner = $marathoner; + $brand_excel = $excel; + + // check if manufacturer has been added + if (!isset($this->bmanu_hash[$sdfc])) + $this->addBatteryManufacturer($sdfc); + + if (!isset($this->bmanu_hash[$ultramax])) + $this->addBatteryManufacturer($ultramax); + + if (!isset($this->bmanu_hash[$motolite])) + $this->addBatteryManufacturer($motolite); + + if (!isset($this->bmanu_hash[$marathoner])) + $this->addBatteryManufacturer($marathoner); + + if (!isset($this->bmanu_hash[$excel])) + $this->addBatteryManufacturer($excel); + + // right now, manufacturer == model + // check if model has been added + if (!isset($this->bmodel_hash[$sdfc])) + $this->addBatteryModel($sdfc); + + if (!isset($this->bmodel_hash[$ultramax])) + $this->addBatteryModel($ultramax); + + if (!isset($this->bmodel_hash[$motolite])) + $this->addBatteryModel($motolite); + + if (!isset($this->bmodel_hash[$marathoner])) + $this->addBatteryModel($marathoner); + + if (!isset($this->bmodel_hash[$excel])) + $this->addBatteryModel($excel); + + $row_num++; + } + else + { + // vehicle and battery data + + // battery size + // check if battery size has been added + if (!isset($this->bsize_hash[$sdfc])) + $this->addBatterySize($sdfc); + + if (!isset($this->bsize_hash[$ultramax])) + $this->addBatterySize($ultramax); + + if (!isset($this->bsize_hash[$motolite])) + $this->addBatterySize($motolite); + + if (!isset($this->bsize_hash[$marathoner])) + $this->addBatterySize($marathoner); + + if (!isset($this->bsize_hash[$excel])) + $this->addBatterySize($excel); + + // check if battery has been added + if (!isset($this->batt_hash[$brand_sdfc][$brand_sdfc][$sdfc])) + { + if (!(empty($sdfc))) + { + $this->addBattery($brand_sdfc, $sdfc); + } + } + + if (!isset($this->batt_hash[$brand_ultramax][$brand_ultramax][$ultramax])) + { + if (!(empty($ultramax))) + { + $this->addBattery($brand_ultramax, $ultramax); + } + } + + if (!isset($this->batt_hash[$brand_motolite][$brand_motolite][$motolite])) + { + if (!(empty($motolite))) + { + $this->addBattery($brand_motolite, $motolite); + } + } + + if (!isset($this->batt_hash[$brand_marathoner][$brand_marathoner][$marathoner])) + { + if (!(empty($marathoner))) + { + $this->addBattery($brand_marathoner, $marathoner); + } + } + + if (!isset($this->batt_hash[$brand_excel][$brand_excel][$excel])) + { + if (!(empty($excel))) + { + $this->addBattery($brand_excel, $excel); + } + } + + + $row_num++; + } + } + } + + protected function addBatteryManufacturer($name) + { + $batt_manufacturer = new BatteryManufacturer(); + + $batt_manufacturer->setName($name); + + $this->em->persist($batt_manufacturer); + $this->em->flush(); + + // add new manufacturer to hash + $this->bmanu_hash[$name] = $batt_manufacturer; + } + + protected function addBatteryModel($name) + { + $batt_model = new BatteryModel(); + + $batt_model->setName($name); + + $this->em->persist($batt_model); + $this->em->flush(); + + // add new model to hash + $this->bmodel_hash[$name] = $batt_model; + } + + protected function addBatterySize($name) + { + if (!empty($name)) + { + // save to db + $batt_size = new BatterySize(); + + $batt_size->setName($name); + + $this->em->persist($batt_size); + $this->em->flush(); + + + // add new size into hash + $this->bsize_hash[$name] = $batt_size; + } + } + + protected function addBattery($brand, $size) + { + // save to db + $bmanu = $this->bmanu_hash[$brand]; + $bmodel = $this->bmodel_hash[$brand]; + $bsize = $this->bsize_hash[$size]; + + $battery = new Battery(); + $battery->setManufacturer($bmanu) + ->setModel($bmodel) + ->setSize($bsize) + ->setWarrantyPrivate(21) + ->setWarrantyCommercial(6) + ->setWarrantyTnv(12); + + $this->em->persist($battery); + $this->em->flush(); + + // insert into hash + $this->batt_hash[$brand][$brand][$size] = $battery; + + // add battery into battery manufacturer, battery model, and battery size + $bmanu->addBattery($battery); + $bmodel->addBattery($battery); + $bsize->addBattery($battery); + + $this->em->persist($bmanu); + $this->em->persist($bmodel); + $this->em->persist($bsize); + + $this->em->flush(); + } + + + + protected function loadBatteryManufacturers() + { + $this->bmanu_hash = []; + + $batt_manufacturers = $this->em->getRepository(BatteryManufacturer::class)->findAll(); + foreach ($batt_manufacturers as $batt_manu) + { + $name = $batt_manu->getName(); + $this->bmanu_hash[$name] = $batt_manu; + } + } + + protected function loadBatteryModels() + { + $this->bmodel_hash = []; + + $batt_models = $this->em->getRepository(BatteryModel::class)->findAll(); + foreach ($batt_models as $batt_model) + { + $name = $batt_model->getName(); + $this->bmodel_hash[$name] = $batt_model; + } + } + + protected function loadBatterySizes() + { + $this->bsize_hash = []; + + $batt_sizes = $this->em->getRepository(BatterySize::class)->findAll(); + foreach ($batt_sizes as $batt_size) + { + $name = $batt_size->getName(); + $this->bsize_hash[$name] = $batt_size; + } + } + + protected function loadBatteries() + { + $this->batt_hash = []; + + $batts = $this->em->getRepository(Battery::class)->findAll(); + foreach ($batts as $batt) + { + $brand = $batt->getManufacturer()->getName(); + $model = $batt->getModel()->getName(); + $size = $batt->getSize()->getName(); + + $this->batt_hash[$brand][$model][$size] = $batt; + } + } + + protected function loadVehicleManufacturers() + { + } + + protected function loadVehicleMakes() + { + } + + + +}