Completed
Pull Request — master (#4168)
by Craig
06:05
created

ZikulaUsersImportCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 4
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Zikula\ZAuthModule\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Style\SymfonyStyle;
11
use Symfony\Component\HttpFoundation\File\File;
12
use Zikula\ZAuthModule\Helper\FileIOHelper;
13
14
class ZikulaUsersImportCommand extends Command
15
{
16
    protected static $defaultName = 'zikula:users:import';
17
18
    /**
19
     * @var FileIOHelper
20
     */
21
    private $ioHelper;
22
23
    public function __construct(FileIOHelper $ioHelper)
24
    {
25
        $this->ioHelper = $ioHelper;
26
27
        parent::__construct();
28
    }
29
30
    protected function configure()
31
    {
32
        $this
33
            ->setDescription('Import a list of users.')
34
            ->addArgument('path', InputArgument::REQUIRED, 'Path to text file containing list of users.')
35
            ->addOption('delimiter', 'd', InputOption::VALUE_REQUIRED, 'Field delimiter (default = ,).')
36
        ;
37
    }
38
39
    protected function execute(InputInterface $input, OutputInterface $output): int
40
    {
41
        $io = new SymfonyStyle($input, $output);
42
        $path = $input->getArgument('path');
43
        $delimiter = $input->getOption('delimiter') ?? ',';
44
45
        $file = new File($path);
0 ignored issues
show
Bug introduced by
It seems like $path can also be of type null and string[]; however, parameter $path of Symfony\Component\HttpFo...ile\File::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        $file = new File(/** @scrutinizer ignore-type */ $path);
Loading history...
46
        $error = $this->ioHelper->importUsersFromFile($file, $delimiter);
47
        if (empty($error)) {
48
            $createdUsers = $this->ioHelper->getCreatedUsers();
49
            $io->success(sprintf('Import successful. %d users imported', count($createdUsers)));
50
51
            return 0;
52
        } else {
53
            $io->error($error);
54
55
            return 1;
56
        }
57
    }
58
}
59