Completed
Push — master ( 9aa90f...051175 )
by Paul
06:58
created

CreateAdminCommand::interact()   B

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;
0 ignored issues
show
Bug introduced by
The type Symfony\Bundle\Framework...d\ContainerAwareCommand was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Symfony\Component\Console\Input\InputArgument;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputArgument was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Output\OutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Symfony\Component\Console\Question\Question;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Question\Question was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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);
64
65
        $output->writeln(sprintf('Created user <comment>%s</comment>', $username));
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);
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
0 ignored issues
show
Bug introduced by
The type FOS\UserBundle\Model\UserInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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