Passed
Push — v2 ( d6a970...09f656 )
by Daniel
04:52
created

UserCreateCommand::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 12
ccs 0
cts 9
cp 0
crap 2
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentBundle\Command;
15
16
use Exception;
17
use Silverback\ApiComponentBundle\Factory\UserFactory;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Input\InputArgument;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
use Symfony\Component\Console\Output\OutputInterface;
23
use Symfony\Component\Console\Question\Question;
24
25
/**
26
 * From FOSUserBundle: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Command/CreateUserCommand.php.
27
 */
28
class UserCreateCommand extends Command
29
{
30
    protected static $defaultName = 'silverback:api-component:user:create';
31
    private UserFactory $userFactory;
32
33
    public function __construct(UserFactory $userFactory)
34
    {
35
        parent::__construct();
36
        $this->userFactory = $userFactory;
37
    }
38
39
    protected function configure(): void
40
    {
41
        $this
42
            ->setDescription('Create a user.')
43
            ->setDefinition([
44
                new InputArgument('username', InputArgument::REQUIRED, 'The username'),
45
                new InputArgument('email', InputArgument::REQUIRED, 'The email'),
46
                new InputArgument('password', InputArgument::REQUIRED, 'The password'),
47
                new InputOption('super-admin', null, InputOption::VALUE_NONE, 'Set the user as super admin'),
48
                new InputOption('inactive', null, InputOption::VALUE_NONE, 'Set the user as inactive'),
49
                new InputOption('overwrite', null, InputOption::VALUE_NONE, 'Overwrite the user if they already exist'),
50
            ])
51
            ->setHelp(<<<'EOT'
52
                The <info>silverback:api-component:user:create</info> command creates a user:
53
                  <info>php %command.full_name% daniel</info>
54
                This interactive shell will ask you for an email and then a password.
55
                You can alternatively specify the email and password as the second and third arguments:
56
                  <info>php %command.full_name% daniel [email protected] mypassword</info>
57
                You can create a super admin via the super-admin flag:
58
                  <info>php %command.full_name% admin --super-admin</info>
59
                You can create an inactive user (will not be able to log in):
60
                  <info>php %command.full_name% disabled_user --inactive</info>
61
                You can overwrite a user if they already exist:
62
                  <info>php %command.full_name% existing_username --overwrite</info>
63
                EOT
64
            );
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    protected function execute(InputInterface $input, OutputInterface $output): void
71
    {
72
        $username = $input->getArgument('username');
73
        $email = $input->getArgument('email');
74
        $password = $input->getArgument('password');
75
        $inactive = $input->getOption('inactive');
76
        $superadmin = $input->getOption('super-admin');
77
        $overwrite = $input->getOption('overwrite');
78
79
        $this->userFactory->create($username, $password, $email, $inactive, $superadmin, $overwrite);
0 ignored issues
show
Bug introduced by
It seems like $username can also be of type null and string[]; however, parameter $username of Silverback\ApiComponentB...y\UserFactory::create() 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

79
        $this->userFactory->create(/** @scrutinizer ignore-type */ $username, $password, $email, $inactive, $superadmin, $overwrite);
Loading history...
Bug introduced by
It seems like $password can also be of type null and string[]; however, parameter $password of Silverback\ApiComponentB...y\UserFactory::create() 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

79
        $this->userFactory->create($username, /** @scrutinizer ignore-type */ $password, $email, $inactive, $superadmin, $overwrite);
Loading history...
Bug introduced by
It seems like $email can also be of type string[]; however, parameter $email of Silverback\ApiComponentB...y\UserFactory::create() does only seem to accept null|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

79
        $this->userFactory->create($username, $password, /** @scrutinizer ignore-type */ $email, $inactive, $superadmin, $overwrite);
Loading history...
80
81
        $output->writeln(sprintf('Created user <comment>%s</comment>', $username));
0 ignored issues
show
Bug introduced by
It seems like $username can also be of type string[]; however, parameter $args of sprintf() 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

81
        $output->writeln(sprintf('Created user <comment>%s</comment>', /** @scrutinizer ignore-type */ $username));
Loading history...
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    protected function interact(InputInterface $input, OutputInterface $output): void
88
    {
89
        $questions = [];
90
91
        if (!$input->getArgument('username')) {
92
            $question = new Question('Please choose a username:');
93
            $question->setValidator(
94
                static function ($username) {
95
                    if (empty($username)) {
96
                        throw new Exception('Username can not be empty');
97
                    }
98
99
                    return $username;
100
                });
101
            $questions['username'] = $question;
102
        }
103
104
        if (!$input->getArgument('email')) {
105
            $question = new Question('Please choose an email (leave blank to use same as username):');
106
            $questions['email'] = $question;
107
        }
108
109
        if (!$input->getArgument('password')) {
110
            $question = new Question('Please choose a password:');
111
            $question->setValidator(
112
                static function ($password) {
113
                    if (empty($password)) {
114
                        throw new Exception('Password can not be empty');
115
                    }
116
117
                    return $password;
118
                });
119
            $question->setHidden(true);
120
            $questions['password'] = $question;
121
        }
122
123
        foreach ($questions as $name => $question) {
124
            $answer = $this->getHelper('question')->ask($input, $output, $question);
125
            $input->setArgument($name, $answer);
126
        }
127
    }
128
}
129