191 lines
5.2 KiB
PHP
191 lines
5.2 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\Outlet;
|
|
use App\Entity\Hub;
|
|
|
|
use CrEOF\Spatial\PHP\Types\Geometry\Point;
|
|
use DateTime;
|
|
|
|
class ImportOutletsCommand extends Command
|
|
{
|
|
private $object_manager;
|
|
|
|
public function __construct(EntityManagerInterface $om, ValidatorInterface $validator)
|
|
{
|
|
$this->object_manager = $om;
|
|
$this->validator = $validator;
|
|
|
|
parent::__construct();
|
|
}
|
|
|
|
protected function configure()
|
|
{
|
|
$this->setName('outlets:import')
|
|
->setDescription('Import a CSV file with outlets and hubs.')
|
|
->setHelp('Creates outlets and hubs based 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 - area
|
|
// 1 - name
|
|
// 2 - branch
|
|
// 3 - address
|
|
// 4 - hub yes/no
|
|
// 5 - annex yes/no
|
|
// 6 - latitude
|
|
// 7 - longitude
|
|
|
|
// attempt to open file
|
|
try
|
|
{
|
|
$handle = fopen($csv_file, "r");
|
|
}
|
|
catch (Exception $e)
|
|
{
|
|
throw new Exception('The file "' . $csv_file . '" could be read.');
|
|
}
|
|
|
|
// get entity manager
|
|
$em = $this->object_manager;
|
|
|
|
// row counter
|
|
$row = 1;
|
|
|
|
// has error?
|
|
$has_error = false;
|
|
|
|
// loop through rows
|
|
while (($fields = fgetcsv($handle)) !== false)
|
|
{
|
|
$output->writeln("Parsing row " . $row . "...");
|
|
|
|
// check if this is also a hub
|
|
$isHub = $fields[4] == 'Yes' ? true : false;
|
|
|
|
if ($isHub)
|
|
{
|
|
$output->writeln("This is a hub! Creating and validating hub entity...");
|
|
|
|
// create hub first
|
|
$hub = new Hub();
|
|
|
|
// set properties
|
|
$this->setObject($hub, $fields);
|
|
|
|
// validate
|
|
$errors = $this->validator->validate($hub);
|
|
$error_array = [];
|
|
|
|
// add errors to list
|
|
foreach ($errors as $error)
|
|
{
|
|
$error_array[$error->getPropertyPath()] = $error->getMessage();
|
|
}
|
|
|
|
if (!empty($error_array))
|
|
{
|
|
foreach ($error_array as $key => $message)
|
|
{
|
|
$output->writeln("[Hub] Error on row " . $row . ", field '" . $key . "': " . $message);
|
|
}
|
|
|
|
// stop on error
|
|
$has_error = true;
|
|
break;
|
|
}
|
|
|
|
// save hub
|
|
$em->persist($hub);
|
|
$output->writeln("Saved hub data for row ". $row . ".");
|
|
}
|
|
|
|
$output->writeln("Creating and validating outlet entity...");
|
|
|
|
// create outlet
|
|
$outlet = new Outlet();
|
|
|
|
// set properties
|
|
$this->setObject($outlet, $fields);
|
|
|
|
// validate
|
|
$errors = $this->validator->validate($outlet);
|
|
$error_array = [];
|
|
|
|
// add errors to list
|
|
foreach ($errors as $error)
|
|
{
|
|
$error_array[$error->getPropertyPath()] = $error->getMessage();
|
|
}
|
|
|
|
if (!empty($error_array))
|
|
{
|
|
foreach ($error_array as $key => $message)
|
|
{
|
|
$output->writeln("[Outlet] Error on row " . $row . ", field '" . $key . "': " . $message);
|
|
}
|
|
|
|
// stop on error
|
|
$has_error = true;
|
|
break;
|
|
}
|
|
|
|
if ($isHub)
|
|
{
|
|
// assign to hub
|
|
$outlet->setHub($hub);
|
|
}
|
|
|
|
// save outlet
|
|
$em->persist($outlet);
|
|
$output->writeln("Saved outlet data for row ". $row . ".");
|
|
|
|
// increment row
|
|
$row++;
|
|
}
|
|
|
|
if (!$has_error)
|
|
{
|
|
// apply db changes
|
|
$em->flush();
|
|
$output->writeln("Done! All changes saved to database.");
|
|
}
|
|
else
|
|
{
|
|
$output->writeln("Errors occurred! Please check the CSV file is in the proper format.");
|
|
}
|
|
|
|
}
|
|
|
|
protected function setObject($obj, $fields)
|
|
{
|
|
// times
|
|
$format = 'g:i A';
|
|
$time_open = DateTime::createFromFormat($format, "12:00 AM");
|
|
$time_close = DateTime::createFromFormat($format, "11:59 PM");
|
|
|
|
// coordinates
|
|
$point = new Point($fields[7], $fields[6]);
|
|
|
|
// set properties
|
|
$obj->setName($fields[1])
|
|
->setBranch($fields[2])
|
|
->setAddress($fields[3])
|
|
->setTimeOpen($time_open)
|
|
->setTimeClose($time_close)
|
|
->setCoordinates($point);
|
|
}
|
|
}
|