resq/src/Command/ImportSAPBatteryCommand.php

149 lines
4 KiB
PHP

<?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 Symfony\Component\Validator\Validator\ValidatorInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\SAPBattery;
use App\Entity\SAPBatteryBrand;
use App\Entity\SAPBatterySize;
use DateTime;
class ImportSAPBatteryCommand extends Command
{
private $em;
private $battery_hash;
private $batt_size_hash;
private $batt_brand_hash;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
$this->initBatteryHash();
$this->initBatterySizeHash();
$this->initBatteryBrandHash();
parent::__construct();
}
protected function configure()
{
$this->setName('sap_battery:import')
->setDescription('Import a CSV file with SAP battery data.')
->setHelp('Creates SAP battery data entries off imported CSV.')
->addArgument('file', InputArgument::REQUIRED, 'Path to the CSV file.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$csv_file = $input->getArgument('file');
// CSV column order:
// 0 - brand
// 1 - sku
// 2 - size
// attempt to open file
try
{
$handle = fopen($csv_file, "r");
}
catch (Exception $e)
{
throw new Exception('The file "' . $csv_file . '" could not be read.');
}
// get entity manager
$em = $this->em;
// row counter
$row = 1;
// has error?
$has_error = false;
// loop through rows and build hashes
while (($fields = fgetcsv($handle)) !== false)
{
// check if blank row
if (strlen(trim($fields[0])) == 0)
continue;
// clean up fields
$clean_brand = trim($fields[0]);
$clean_sku = trim($fields[1]);
$clean_size = trim($fields[2]);
$output->writeln("Parsing $clean_sku...");
// brand hash
if (!isset($this->batt_brand_hash[$clean_brand]))
{
$brand = new SAPBatteryBrand();
$brand->setName($clean_brand);
$em->persist($brand);
$this->batt_brand_hash[$clean_brand] = $brand;
}
// size hash
if (!isset($this->batt_size_hash[$clean_size]))
{
$size = new SAPBatterySize();
$size->setName($clean_size);
$em->persist($size);
$this->batt_size_hash[$clean_size] = $size;
}
// battery hash
if (!isset($this->battery_hash[$clean_sku]))
{
// create battery entry
$battery = new SAPBattery();
$battery->setID($clean_sku)
->setSize($size_hash[$clean_size])
->setBrand($brand_hash[$clean_brand]);
$em->persist($battery);
}
}
$em->flush();
}
protected function initBatteryHash()
{
$this->battery_hash = [];
$batts = $this->em->getRepository(SAPBattery::class)->findAll();
foreach ($batts as $batt)
$this->battery_hash[$batt->getID()] = $batt;
}
protected function initBatterySizeHash()
{
$this->batt_size_hash = [];
$sizes = $this->em->getRepository(SAPBatterySize::class)->findAll();
foreach ($sizes as $size)
$this->batt_size_hash[$size->getName()] = $size;
}
protected function initBatteryBrandHash()
{
$this->batt_brand_hash = [];
$brands = $this->em->getRepository(SAPBatteryBrand::class)->findAll();
foreach ($brands as $brand)
$this->batt_brand_hash[$brand->getName()] = $brand;
}
}