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