CreateAdminCommand::interact()   B
last analyzed

Complexity

Conditions 11
Paths 33

Size

Total Lines 62
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 36
c 0
b 0
f 0
nc 33
nop 2
dl 0
loc 62
rs 7.3166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the GitControlBundle package.
5
 *
6
 * (c) Paul Schweppe <[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
namespace VersionControl\GitControlBundle\Command;
13
14
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Console\Question\Question;
19
20
/**
21
 * Command to create a new admin user for the version control application.
22
 *
23
 * @author Paul Schweppe<[email protected]>
24
 */
25
class CreateAdminCommand extends ContainerAwareCommand
26
{
27
    /**
28
     * @see Command
29
     */
30
    protected function configure()
31
    {
32
        $this
33
            ->setName('version:admin:create')
34
            ->setDescription('Create a new admin user.')
35
            ->setDefinition(array(
36
                new InputArgument('username', InputArgument::REQUIRED, 'The username'),
37
                new InputArgument('email', InputArgument::REQUIRED, 'The email'),
38
                new InputArgument('password', InputArgument::REQUIRED, 'The password'),
39
                new InputArgument('name', InputArgument::REQUIRED, 'The display name'),
40
            ))
41
            ->setHelp(<<<'EOT'
42
The <info>version:admin:create</info> command creates an administrator user:
43
44
  <info>php app/console version:admin:create adminname</info>
45
46
This interactive shell will ask you for an email, display name and then a password.
47
48
49
EOT
50
            );
51
    }
52
53
    /**
54
     * @see Command
55
     */
56
    protected function execute(InputInterface $input, OutputInterface $output)
57
    {
58
        $username = $input->getArgument('username');
59
        $email = $input->getArgument('email');
60
        $password = $input->getArgument('password');
61
        $name = $input->getArgument('name');
62
63
        $this->createuser($username, $password, $email, $name);
0 ignored issues
show
Bug introduced by
It seems like $username can also be of type string[]; however, parameter $username of VersionControl\GitContro...inCommand::createUser() 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

63
        $this->createuser(/** @scrutinizer ignore-type */ $username, $password, $email, $name);
Loading history...
Bug introduced by
It seems like $name can also be of type string[]; however, parameter $name of VersionControl\GitContro...inCommand::createUser() 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

63
        $this->createuser($username, $password, $email, /** @scrutinizer ignore-type */ $name);
Loading history...
Bug introduced by
It seems like $email can also be of type string[]; however, parameter $email of VersionControl\GitContro...inCommand::createUser() 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

63
        $this->createuser($username, $password, /** @scrutinizer ignore-type */ $email, $name);
Loading history...
Bug introduced by
It seems like $password can also be of type string[]; however, parameter $password of VersionControl\GitContro...inCommand::createUser() 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

63
        $this->createuser($username, /** @scrutinizer ignore-type */ $password, $email, $name);
Loading history...
64
65
        $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

65
        $output->writeln(sprintf('Created user <comment>%s</comment>', /** @scrutinizer ignore-type */ $username));
Loading history...
66
    }
67
68
    /**
69
     * @see Command
70
     */
71
    protected function interact(InputInterface $input, OutputInterface $output)
72
    {
73
        if (!$this->getHelperSet()->has('question')) {
74
            $this->legacyInteract($input, $output);
0 ignored issues
show
Bug introduced by
The method legacyInteract() does not exist on VersionControl\GitContro...mand\CreateAdminCommand. ( Ignorable by Annotation )

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

74
            $this->/** @scrutinizer ignore-call */ 
75
                   legacyInteract($input, $output);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
76
            return;
77
        }
78
79
        $questions = array();
80
81
        if (!$input->getArgument('username')) {
82
            $question = new Question('Please choose a username:');
83
            $question->setValidator(function ($username) {
84
                if (empty($username)) {
85
                    throw new \Exception('Username can not be empty');
86
                }
87
88
                return $username;
89
            });
90
            $questions['username'] = $question;
91
        }
92
93
        if (!$input->getArgument('email')) {
94
            $question = new Question('Please choose an email:');
95
            $question->setValidator(function ($email) {
96
                if (empty($email)) {
97
                    throw new \Exception('Email can not be empty');
98
                }
99
100
                return $email;
101
            });
102
            $questions['email'] = $question;
103
        }
104
105
        if (!$input->getArgument('password')) {
106
            $question = new Question('Please choose a password:');
107
            $question->setValidator(function ($password) {
108
                if (empty($password)) {
109
                    throw new \Exception('Password can not be empty');
110
                }
111
112
                return $password;
113
            });
114
            $question->setHidden(true);
115
            $questions['password'] = $question;
116
        }
117
118
        if (!$input->getArgument('name')) {
119
            $question = new Question('Please choose a display name:');
120
            $question->setValidator(function ($name) {
121
                if (empty($name)) {
122
                    throw new \Exception('Display name can not be empty');
123
                }
124
125
                return $name;
126
            });
127
            $questions['name'] = $question;
128
        }
129
130
        foreach ($questions as $name => $question) {
131
            $answer = $this->getHelper('question')->ask($input, $output, $question);
132
            $input->setArgument($name, $answer);
133
        }
134
    }
135
136
    /**
137
     * Creates a new admin user.
138
     *
139
     * @param string $username
140
     * @param string $password
141
     * @param string $email
142
     * @param string $name
143
     *
144
     * @return \FOS\UserBundle\Model\UserInterface
145
     */
146
    protected function createUser($username, $password, $email, $name)
147
    {
148
        $userManager = $this->getContainer()->get('fos_user.user_manager');
149
150
        $user = $userManager->createUser();
151
        $user->setUsername($username);
152
        $user->setEmail($email);
153
        $user->setPlainPassword($password);
154
        $user->setName($name);
155
156
        $user->setEnabled((bool) true);
157
        $user->addRole('ROLE_ADMIN');
158
        //$user->setSuperAdmin((Boolean) true);
159
160
        $userManager->updateUser($user);
161
162
        return $user;
163
    }
164
}
165