Make command sap_battery:import repeatable #178
This commit is contained in:
parent
407f7ad54a
commit
f38b9944c2
1 changed files with 48 additions and 17 deletions
|
|
@ -18,10 +18,16 @@ use DateTime;
|
|||
class ImportSAPBatteryCommand extends Command
|
||||
{
|
||||
private $em;
|
||||
private $battery_hash;
|
||||
private $batt_size_hash;
|
||||
private $batt_brand_hash;
|
||||
|
||||
public function __construct(ObjectManager $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->initBatteryHash();
|
||||
$this->initBatterySizeHash();
|
||||
$this->initBatteryBrandHash();
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
|
@ -62,16 +68,10 @@ class ImportSAPBatteryCommand extends Command
|
|||
// has error?
|
||||
$has_error = false;
|
||||
|
||||
// hashes
|
||||
$brand_hash = [];
|
||||
$size_hash = [];
|
||||
$battery_hash = [];
|
||||
|
||||
// loop through rows and build hashes
|
||||
while (($fields = fgetcsv($handle)) !== false)
|
||||
{
|
||||
|
||||
// check if blank
|
||||
// check if blank row
|
||||
if (strlen(trim($fields[0])) == 0)
|
||||
continue;
|
||||
|
||||
|
|
@ -83,36 +83,67 @@ class ImportSAPBatteryCommand extends Command
|
|||
$output->writeln("Parsing $clean_sku...");
|
||||
|
||||
// brand hash
|
||||
if (!isset($brand_hash[$clean_brand]))
|
||||
if (!isset($this->batt_brand_hash[$clean_brand]))
|
||||
{
|
||||
$brand = new SAPBatteryBrand();
|
||||
$brand->setName($clean_brand);
|
||||
|
||||
$em->persist($brand);
|
||||
|
||||
$brand_hash[$clean_brand] = $brand;
|
||||
$this->batt_brand_hash[$clean_brand] = $brand;
|
||||
}
|
||||
|
||||
// size hash
|
||||
if (!isset($size_hash[$clean_size]))
|
||||
if (!isset($this->batt_size_hash[$clean_size]))
|
||||
{
|
||||
$size = new SAPBatterySize();
|
||||
$size->setName($clean_size);
|
||||
|
||||
$em->persist($size);
|
||||
|
||||
$size_hash[$clean_size] = $size;
|
||||
$this->batt_size_hash[$clean_size] = $size;
|
||||
}
|
||||
|
||||
// create battery entry
|
||||
$battery = new SAPBattery();
|
||||
$battery->setID($clean_sku)
|
||||
->setSize($size_hash[$clean_size])
|
||||
->setBrand($brand_hash[$clean_brand]);
|
||||
// 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->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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue