diff --git a/src/Command/ImportYokohamaVehicleBatteryCompatibilityCommand.php b/src/Command/ImportYokohamaVehicleBatteryCompatibilityCommand.php index 50296cad..1599eef3 100644 --- a/src/Command/ImportYokohamaVehicleBatteryCompatibilityCommand.php +++ b/src/Command/ImportYokohamaVehicleBatteryCompatibilityCommand.php @@ -62,6 +62,7 @@ class ImportYokohamaVehicleBatteryCompatibilityCommand extends Command $csv_file = $input->getArgument('input_file'); $output_file = $input->getArgument('output_file'); + $this->populateVehicleManufacturerIndex(); $this->populateVehicleIndex(); $this->populateBatteryIndex(); @@ -260,6 +261,23 @@ class ImportYokohamaVehicleBatteryCompatibilityCommand extends Command protected function validateManufacturerVehicle($fields, $brand, $make, $model, $bsize, $bmodel) { $output_info = []; + + // check if manufacturer is blank + if (empty($brand)) + { + $message = 'No manufacturer provided.'; + $output_info = $this->setOutputInfo($fields, 'NOT ADDED', $message); + return $output_info; + } + + // check if make is blank + if (empty($make)) + { + $message = 'No make provided.'; + $output_info = $this->setOutputInfo($fields, 'NOT ADDED', $message); + return $output_info; + } + // process model year data if ($model == 'NONE') { @@ -277,12 +295,42 @@ class ImportYokohamaVehicleBatteryCompatibilityCommand extends Command $m_year_to = 0; } + // get manufacturer or make one + if (!isset($this->vmfg_index[$brand])) + { + // manufacturer + $mfg = new VehicleManufacturer(); + $mfg->setName($brand); + $this->em->persist($mfg); + } + else + { + $mfg = $this->vmfg_index[$brand]; + } + if (!isset($this->v_index[$brand][$make . '|' . intval($m_year_from) . '|' . intval($m_year_to)])) { - $message = 'Invalid vehicle'; - $output_info = $this->setOutputInfo($fields, 'NOT ADDED', $message); - return $output_info; + // vehicle + $vehicle = new Vehicle(); + $vehicle->setManufacturer($mfg) + ->setMake($make) + ->setModelYearFrom($m_year_from) + ->setModelYearTo($m_year_to); + $this->em->persist($vehicle); } + else + { + $vehicle = $this->v_index[$brand][$make . '|' . intval($m_year_from) . '|' . intval($m_year_to)]; + } + + // save to db new manufacturer and vehicle + $this->em->flush(); + + // add the vehicle manufacturer to hash + $this->vmfg_index[$brand] = $mfg; + + // add the new vehicle to hash + $this->v_index[$brand][$make . '|' . $m_year_from . '|' . $m_year_to] = $vehicle; // recommended battery $batt_key = $this->getBatteryKey($bsize, $bmodel); @@ -354,16 +402,27 @@ class ImportYokohamaVehicleBatteryCompatibilityCommand extends Command fclose($fh); } + protected function populateVehicleManufacturerIndex() + { + $vmfgs = $this->em->getRepository(VehicleManufacturer::class)->findAll(); + + $this->vmfg_index = []; + + foreach ($vmfgs as $vmfg) + { + $mfg_name = $this->normalizeName($vmfg->getName()); + $this->vmfg_index[$mfg_name] = $vmfg; + } + } + protected function populateVehicleIndex() { $vs = $this->em->getRepository(Vehicle::class)->findAll(); $this->v_index = []; - $this->vmfg_index = []; foreach ($vs as $v) { $mfg_name = $this->normalizeName($v->getManufacturer()->getName()); - $this->vmfg_index[$mfg_name] = $v->getManufacturer(); if (!isset($this->v_index[$mfg_name])) $this->v_index[$mfg_name] = []; diff --git a/src/Entity/Vehicle.php b/src/Entity/Vehicle.php index a93bed4e..61fdcec9 100644 --- a/src/Entity/Vehicle.php +++ b/src/Entity/Vehicle.php @@ -36,7 +36,7 @@ class Vehicle // make /** - * @ORM\Column(type="string", length=80) + * @ORM\Column(type="string", length=110) * @Assert\NotBlank() */ protected $make;