diff --git a/old/src/PointToolsBundle/Command/ImportUsersCommand.php b/src/Command/ImportUsersCommand.php similarity index 62% rename from old/src/PointToolsBundle/Command/ImportUsersCommand.php rename to src/Command/ImportUsersCommand.php index 1710021..33a3a2e 100644 --- a/old/src/PointToolsBundle/Command/ImportUsersCommand.php +++ b/src/Command/ImportUsersCommand.php @@ -1,12 +1,17 @@ (-1) * ) TO '/tmp/point_users.csv' WITH HEADER DELIMITER '|' CSV; */ +#[AsCommand(name: 'app:import:users', description: 'Import users from CSV file')] class ImportUsersCommand extends Command { - /** @var EntityManager */ - private $em; - - public function __construct(EntityManagerInterface $em) + public function __construct(private readonly EntityManagerInterface $em) { - $this->em = $em; - parent::__construct(); } - protected function configure() + protected function configure(): void { $this - ->setName('point:import:users') - ->setDescription('Import users from CSV file') ->addArgument( 'file', InputArgument::REQUIRED, @@ -55,20 +54,24 @@ class ImportUsersCommand extends Command ; } - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { + $io = new SymfonyStyle($input, $output); + $fs = new Filesystem(); $fileName = $input->getArgument('file'); - if (!($fs->exists($fileName) && is_readable($fileName))) { - $output->writeln('File does not exists or not readable.'); - return 1; + if (!($fs->exists($fileName) && \is_readable($fileName))) { + $io->error('File does not exists or not readable.'); + + return Command::FAILURE; } - if (false === ($file = fopen($fileName, 'r'))) { - $output->writeln('fopen() error'); - return 1; + if (false === ($file = fopen($fileName, 'rb'))) { + $io->error('fopen() error'); + + return Command::FAILURE; } if (!$input->getOption('no-skip-first')) { @@ -79,7 +82,7 @@ class ImportUsersCommand extends Command $count = 0; while (false !== ($row = fgetcsv($file, 1000, '|'))) { - if (count($row) !== 4) { + if (\count($row) !== 4) { continue; } @@ -93,15 +96,15 @@ class ImportUsersCommand extends Command $this->em->detach($user); } - if (OutputInterface::VERBOSITY_VERBOSE === $output->getVerbosity()) { - $output->writeln('@' . $row[1] . ' added'); + if (OutputInterface::VERBOSITY_VERBOSE === $io->getVerbosity()) { + $io->info('@' . $row[1] . ' added'); } $count++; } - $output->writeln($count . ' users imported.'); + $io->success($count . ' users imported.'); - return 0; + return Command::SUCCESS; } -} \ No newline at end of file +}