merge import batter commands under one command. #270

This commit is contained in:
Korina Cordero 2019-10-02 11:27:10 +00:00
parent a29ea8756b
commit 54c9871189
5 changed files with 355 additions and 467 deletions

View file

@ -1,174 +0,0 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\Common\Persistence\ObjectManager;
use App\Entity\BatteryManufacturer;
use App\Entity\BatteryModel;
use App\Entity\BatterySize;
use App\Entity\Battery;
class ImportBatteriesCommand extends Command
{
// field index in csv file
const F_BATT_SDFC = 4;
const F_BATT_ULTRAMAX = 5;
const F_BATT_MOTOLITE = 6;
const F_BATT_MARATHONER = 7;
const F_BATT_EXCEL = 8;
protected $em;
protected $bmanu_hash;
protected $bmodel_hash;
protected $bsize_hash;
public function __construct(ObjectManager $om)
{
$this->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;
}
}
}

View file

@ -1,90 +0,0 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\Common\Persistence\ObjectManager;
use App\Entity\BatteryManufacturer;
class ImportBatteryManufacturersCommand extends Command
{
// field index in csv file
const F_BATT_SDFC = 4;
const F_BATT_ULTRAMAX = 5;
const F_BATT_MOTOLITE = 6;
const F_BATT_MARATHONER = 7;
const F_BATT_EXCEL = 8;
protected $em;
public function __construct(ObjectManager $om)
{
$this->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();
}
}

View file

@ -1,92 +0,0 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\Common\Persistence\ObjectManager;
use App\Entity\BatteryModel;
class ImportBatteryModelsCommand extends Command
{
// field index in csv file
// Possible TODO: this might change
const F_BATT_SDFC = 4;
const F_BATT_ULTRAMAX = 5;
const F_BATT_MOTOLITE = 6;
const F_BATT_MARATHONER = 7;
const F_BATT_EXCEL = 8;
protected $em;
public function __construct(ObjectManager $om)
{
$this->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();
}
}

View file

@ -1,111 +0,0 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\Common\Persistence\ObjectManager;
use App\Entity\BatterySize;
class ImportBatterySizesCommand extends Command
{
// field index in csv file
const F_BATT_SIZE_SDFC = 4;
const F_BATT_SIZE_ULTRAMAX = 5;
const F_BATT_SIZE_MOTOLITE = 6;
const F_BATT_SIZE_MARATHONER = 7;
const F_BATT_SIZE_EXCEL = 8;
protected $em;
protected $bsize_hash;
public function __construct(ObjectManager $om)
{
$this->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
}
}

View file

@ -0,0 +1,355 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\Common\Persistence\ObjectManager;
use App\Entity\BatteryManufacturer;
use App\Entity\BatteryModel;
use App\Entity\BatterySize;
use App\Entity\Battery;
class ImportCMBBatteryDataCommand extends Command
{
// field index in csv file
const F_BATT_SDFC = 4;
const F_BATT_ULTRAMAX = 5;
const F_BATT_MOTOLITE = 6;
const F_BATT_MARATHONER = 7;
const F_BATT_EXCEL = 8;
protected $em;
protected $bmanu_hash;
protected $bmodel_hash;
protected $bsize_hash;
protected $batt_hash;
protected $vmanu_hash;
protected $vmake_hash;
public function __construct(ObjectManager $om)
{
$this->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()
{
}
}