Completed
Push — master ( 802ebe...107004 )
by Craig
07:08
created

ZikulaUsersImportCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\ZAuthModule\Command;
15
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\Console\Style\SymfonyStyle;
22
use Symfony\Component\HttpFoundation\File\File;
23
use Zikula\ZAuthModule\Helper\FileIOHelper;
24
25
class ZikulaUsersImportCommand extends Command
26
{
27
    protected static $defaultName = 'zikula:users:import';
28
29
    /**
30
     * @var FileIOHelper
31
     */
32
    private $ioHelper;
33
34
    public function __construct(FileIOHelper $ioHelper)
35
    {
36
        $this->ioHelper = $ioHelper;
37
38
        parent::__construct();
39
    }
40
41
    protected function configure()
42
    {
43
        $this
44
            ->setDescription('Import a list of users.')
45
            ->addArgument('path', InputArgument::REQUIRED, 'Path to text file containing list of users.')
46
            ->addOption('delimiter', 'd', InputOption::VALUE_REQUIRED, 'Field delimiter (default = ,).')
47
        ;
48
    }
49
50
    protected function execute(InputInterface $input, OutputInterface $output): int
51
    {
52
        $io = new SymfonyStyle($input, $output);
53
        $path = $input->getArgument('path');
54
        $delimiter = $input->getOption('delimiter') ?? ',';
55
56
        $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

56
        $file = new File(/** @scrutinizer ignore-type */ $path);
Loading history...
57
        $error = $this->ioHelper->importUsersFromFile($file, $delimiter);
58
        if (empty($error)) {
59
            $createdUsers = $this->ioHelper->getCreatedUsers();
60
            $io->success(sprintf('Import successful. %d users imported', count($createdUsers)));
61
62
            return 0;
63
        }
64
        $io->error($error);
65
66
        return 1;
67
    }
68
}
69