point-tools/src/Skobkin/Bundle/PointToolsBundle/Command/ImportUsersCommand.php

105 lines
3.1 KiB
PHP
Raw Normal View History

2015-05-28 19:21:58 +00:00
<?php
namespace Skobkin\Bundle\PointToolsBundle\Command;
use Skobkin\Bundle\PointToolsBundle\Entity\User;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
/**
* Import users from CSV file exported from database by query:
2016-12-12 17:44:36 +00:00
* COPY (
* SELECT u.id, u.login, ui.name, to_char(ui.created, 'YYYY-MM-DD_HH24:MI:SS') AS created_at
* FROM users.logins u
* LEFT JOIN users.info ui ON (ui.id = u.id)
* WHERE u.id <> (-1)
* ) TO '/tmp/point_users.csv' WITH HEADER DELIMITER '|' CSV;
2015-05-28 19:21:58 +00:00
*/
class ImportUsersCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('point:import:users')
->setDescription('Import users from CSV file')
->addArgument(
'file',
InputArgument::REQUIRED,
'CSV file path'
)
->addOption(
'check-only',
null,
InputOption::VALUE_NONE,
'If set, command will not perform write operations in the database'
)
->addOption(
'no-skip-first',
null,
InputOption::VALUE_NONE,
'Do not skip first line (if no headers in CSV file)'
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
$fs = new Filesystem();
$fileName = $input->getArgument('file');
if (!($fs->exists($fileName) && is_readable($fileName))) {
$output->writeln('File does not exists or not readable.');
2016-12-12 17:44:36 +00:00
return 1;
2015-05-28 19:21:58 +00:00
}
if (false === ($file = fopen($fileName, 'r'))) {
$output->writeln('fopen() error');
2016-12-12 17:44:36 +00:00
return 1;
2015-05-28 19:21:58 +00:00
}
if (!$input->getOption('no-skip-first')) {
// Reading headers line
2016-12-12 17:44:36 +00:00
fgets($file);
2015-05-28 19:21:58 +00:00
}
$count = 0;
while (false !== ($row = fgetcsv($file, 1000, '|'))) {
if (count($row) !== 4) {
continue;
}
2017-01-17 01:36:35 +00:00
$createdAt = \DateTime::createFromFormat('Y-m-d_H:i:s', $row[3]);
2015-05-28 19:21:58 +00:00
2017-01-17 01:36:35 +00:00
if (!$createdAt) {
$createdAt = new \DateTime();
}
$user = (new User($row[0], $row[1], $row[2]))
->setCreatedAt($createdAt)
;
2015-05-28 19:21:58 +00:00
if (!$input->getOption('check-only')) {
$em->persist($user);
$em->flush($user);
$em->detach($user);
}
2016-12-12 17:44:36 +00:00
if (OutputInterface::VERBOSITY_VERBOSE === $output->getVerbosity()) {
2015-05-28 19:21:58 +00:00
$output->writeln('@' . $row[1] . ' added');
}
$count++;
}
$output->writeln($count . ' users imported.');
2016-12-12 17:56:40 +00:00
return 0;
2015-05-28 19:21:58 +00:00
}
}