point-tools/src/Command/ImportUsersCommand.php

110 lines
3.1 KiB
PHP
Raw Normal View History

2015-05-28 19:21:58 +00:00
<?php
2023-03-19 14:55:24 +00:00
declare(strict_types=1);
2015-05-28 19:21:58 +00:00
2023-03-19 14:55:24 +00:00
namespace App\Command;
2015-05-28 19:21:58 +00:00
2023-03-19 14:55:24 +00:00
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
2023-03-19 14:55:24 +00:00
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
2015-05-28 19:21:58 +00:00
use Symfony\Component\Console\Output\OutputInterface;
2023-03-19 14:55:24 +00:00
use Symfony\Component\Console\Style\SymfonyStyle;
2015-05-28 19:21:58 +00:00
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
*/
2023-03-19 14:55:24 +00:00
#[AsCommand(name: 'app:import:users', description: 'Import users from CSV file')]
class ImportUsersCommand extends Command
2015-05-28 19:21:58 +00:00
{
2023-03-19 14:55:24 +00:00
public function __construct(private readonly EntityManagerInterface $em)
{
parent::__construct();
}
2023-03-19 14:55:24 +00:00
protected function configure(): void
2015-05-28 19:21:58 +00:00
{
$this
->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)'
)
;
}
2023-03-19 14:55:24 +00:00
protected function execute(InputInterface $input, OutputInterface $output): int
2015-05-28 19:21:58 +00:00
{
2023-03-19 14:55:24 +00:00
$io = new SymfonyStyle($input, $output);
2015-05-28 19:21:58 +00:00
$fs = new Filesystem();
$fileName = $input->getArgument('file');
2023-03-19 14:55:24 +00:00
if (!($fs->exists($fileName) && \is_readable($fileName))) {
$io->error('File does not exists or not readable.');
return Command::FAILURE;
2015-05-28 19:21:58 +00:00
}
2023-03-19 14:55:24 +00:00
if (false === ($file = fopen($fileName, 'rb'))) {
$io->error('fopen() error');
return Command::FAILURE;
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, '|'))) {
2023-03-19 14:55:24 +00:00
if (\count($row) !== 4) {
2015-05-28 19:21:58 +00:00
continue;
}
2017-01-17 21:58:53 +00:00
$createdAt = \DateTime::createFromFormat('Y-m-d_H:i:s', $row[3]) ?: new \DateTime();
2015-05-28 19:21:58 +00:00
2017-01-17 21:58:53 +00:00
$user = new User($row[0], $createdAt, $row[1], $row[2]);
2015-05-28 19:21:58 +00:00
if (!$input->getOption('check-only')) {
$this->em->persist($user);
$this->em->flush($user);
$this->em->detach($user);
2015-05-28 19:21:58 +00:00
}
2023-03-19 14:55:24 +00:00
if (OutputInterface::VERBOSITY_VERBOSE === $io->getVerbosity()) {
$io->info('@' . $row[1] . ' added');
2015-05-28 19:21:58 +00:00
}
$count++;
}
2023-03-19 14:55:24 +00:00
$io->success($count . ' users imported.');
2016-12-12 17:56:40 +00:00
2023-03-19 14:55:24 +00:00
return Command::SUCCESS;
2015-05-28 19:21:58 +00:00
}
2023-03-19 14:55:24 +00:00
}