Add command to add battery models from given csv file. #270
This commit is contained in:
parent
5980715128
commit
987668ffbc
1 changed files with 92 additions and 0 deletions
92
src/Command/ImportBatteryModelsCommand.php
Normal file
92
src/Command/ImportBatteryModelsCommand.php
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
<?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();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue