em = $om; // load existing batteries and sizes $this->loadBatteryManufacturers(); $this->loadBatteryModels(); $this->loadBatteries(); $this->loadBatterySizes(); parent::__construct(); } protected function configure() { $this->setName('cmbbatterydata:import') ->setDescription('Import a CSV file with battery data.') ->setHelp('Adds the battery data based on 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; // loop through the rows $row_num = 0; error_log('Processing battery csv file...'); while (($fields = fgetcsv($fh)) !== false) { // data starts at row 2 if ($row_num < 2) { $row_num++; continue; } // battery info $code = trim($fields[self::F_BATT_CODE]); $desc = trim($fields[self::F_BATT_DESC]); $price = trim($fields[self::F_BATT_PRICE]); $clean_price = trim($price, '$'); $battery_info = explode(' ', $desc); // if battery_info has 3 elements, get the last two // [0] = battery manufacturer // [1] = battery model // [2] = battery size // if only 2, get both // [0] = battery manufacturer and battery model // [1] = battery size // if 4, // [0] = battery manufacturer // concatenate [1] and [2] for the battery model // [3] = battery size $battery_manufacturer = ''; $battery_model = ''; $battery_size = ''; if (count($battery_info) == 3) { // sample: Century Marathoner 120-7L $battery_manufacturer = trim($battery_info[0]); $battery_model = trim($battery_info[1]); $battery_size = trim($battery_info[2]); } if (count($battery_info) == 2) { // sample: Marshall DIN55R $battery_manufacturer = trim($battery_info[0]); $battery_model = trim($battery_info[0]); $battery_size = trim($battery_info[1]); } if (count($battery_info) == 4) { // sample: Motolite Classic Wetcharged DIN100L $battery_manufacturer = trim($battery_info[0]); $battery_model = trim($battery_info[1]) . ' ' . trim($battery_info[2]); $battery_size = trim($battery_info[3]); } // check if battery size has () // if so, trim it to ignore the parenthesis and what's after (. $pos = stripos($battery_size, '('); if ($pos == true) { $sizes = explode('(', $battery_size); $clean_size = trim($sizes[0]); } else { $clean_size = $battery_size; } //error_log('battery manufacturer ' . $battery_manufacturer); //error_log('battery model ' . $battery_model); //error_log('battery size ' . $battery_size); // normalize the manufacturer, model and size for the hash // when we add to db for manufacturer, model, and size, we do not use the normalized versions $normalized_manu = $this->normalizeName($battery_manufacturer); $normalized_model = $this->normalizeName($battery_model); $normalized_size = $this->normalizeName($clean_size); // save battery manufacturer if not yet in system if (!isset($this->bmanu_hash[$normalized_manu])) { $this->addBatteryManufacturer($battery_manufacturer); } // save battery model if not yet in system if (!isset($this->bmodel_hash[$normalized_model])) { $this->addBatteryModel($battery_model); } // save battery size if not yet in system if (!isset($this->bsize_hash[$normalized_size])) { $this->addBatterySize($clean_size); } // save battery if not yet in system if (!isset($this->batt_hash[$normalized_manu][$normalized_model][$normalized_size])) { $this->addBattery($normalized_manu, $normalized_model, $normalized_size, $code, $clean_price); } } return 0; } 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 $normalized_name = $this->normalizeName($name); $this->bmanu_hash[$normalized_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 $normalized_name = $this->normalizeName($name); $this->bmodel_hash[$normalized_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 $normalized_name = $this->normalizeName($name); $this->bsize_hash[$normalized_name] = $batt_size; } } protected function addBattery($manufacturer, $brand, $size, $code, $price) { // save to db $bmanu = $this->bmanu_hash[$manufacturer]; $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) ->setProductCode($code) ->setSAPCode($code) ->setSellingPrice($price); $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 = $this->normalizeName($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 = $this->normalizeName($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 = $this->normalizeName($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 = $this->normalizeName($batt->getManufacturer()->getName()); $model = $this->normalizeName($batt->getModel()->getName()); $size = $this->normalizeName($batt->getSize()->getName()); $this->batt_hash[$brand][$model][$size] = $batt; } } protected function normalizeName($name) { $normalized_key = trim(strtolower($name)); return $normalized_key; } }