Completed
Push — master ( 02f0b7...dfad4d )
by Philip
20:25
created

GitkiUsersCommand::editUsername()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 21
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 21
loc 21
rs 9.3142
cc 2
eloc 14
nc 1
nop 4
1
<?php
2
3
4
namespace Dontdrinkandroot\Gitki\WebBundle\Command;
5
6
use Dontdrinkandroot\Gitki\WebBundle\Entity\User;
7
use FOS\UserBundle\Model\UserManagerInterface;
8
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
9
use Symfony\Component\Console\Helper\QuestionHelper;
10
use Symfony\Component\Console\Helper\Table;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Question\ChoiceQuestion;
14
use Symfony\Component\Console\Question\Question;
15
16
abstract class GitkiUsersCommand extends ContainerAwareCommand
17
{
18
    /**
19
     * @return UserManagerInterface
20
     */
21
    protected function getUserManager()
22
    {
23
        return $this->getContainer()->get('fos_user.user_manager');
24
    }
25
26
    /**
27
     * @return QuestionHelper
28
     */
29
    protected function getQuestionHelper()
30
    {
31
        return $this->getHelper('question');
32
    }
33
34
    protected function printUser(User $user, OutputInterface $output)
35
    {
36
        $output->writeln('--------------------');
37
        $output->writeln('Id: ' . $user->getId());
38
        $output->writeln('User Name: ' . $user->getUsername());
39
        $output->writeln('Email: ' . $user->getEmail());
40
        $output->writeln('Roles: ' . implode(',', $user->getRoles()));
41
        $output->writeln('Github Login: ' . $user->getGithubId());
42
        $output->writeln('Google Login: ' . $user->getGoogleId());
43
        $output->writeln('--------------------');
44
    }
45
46
    /**
47
     * @param InputInterface  $input
48
     * @param OutputInterface $output
49
     *
50
     * @return mixed
51
     */
52
    protected function createUser(
53
        InputInterface $input,
54
        OutputInterface $output
55
    ) {
56
        $userManager = $this->getUserManager();
57
        $questionHelper = $this->getQuestionHelper();
58
59
        /** @var User $user */
60
        $user = $userManager->createUser();
61
        $user->setEnabled(true);
62
63
        $user = $this->editUsername($input, $output, $user, $questionHelper);
64
        $user = $this->editRealName($input, $output, $user, $questionHelper);
65
        $user = $this->editEmail($input, $output, $user, $questionHelper);
66
        $user = $this->editRole($input, $output, $user, $questionHelper);
67
        $user = $this->editPassword($input, $output, $user, $questionHelper, $userManager);
68
        $user = $this->editGithubId($input, $output, $user, $questionHelper);
69
        $user = $this->editGoogleId($input, $output, $user, $questionHelper);
70
71
        return $user;
72
    }
73
74
    /**
75
     * @param OutputInterface $output
76
     * @param User[]          $users
77
     */
78
    protected function printUserTable(OutputInterface $output, $users)
79
    {
80
        $table = new Table($output);
81
        $table->setHeaders(['ID', 'User Name', 'Email', 'Roles', 'GitHub ID', 'Google ID']);
82
83
        foreach ($users as $user) {
84
            $table->addRow(
85
                [
86
                    $user->getId(),
87
                    $user->getUsername(),
88
                    $user->getEmail(),
89
                    implode(',', $user->getRoles()),
90
                    $user->getGithubId(),
91
                    $user->getGoogleId()
92
                ]
93
            );
94
        }
95
96
        $table->render();
97
    }
98
99
    /**
100
     * @param InputInterface  $input
101
     * @param OutputInterface $output
102
     * @param User            $user
103
     * @param QuestionHelper  $questionHelper
104
     *
105
     * @return User
106
     */
107 View Code Duplication
    protected function editUsername(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
        InputInterface $input,
109
        OutputInterface $output,
110
        User $user,
111
        QuestionHelper $questionHelper
112
    ) {
113
        $realNameQuestion = new Question('Username [required]: ');
114
        $realNameQuestion->setValidator(
115
            function ($answer) {
116
                if (empty($answer)) {
117
                    throw new \RuntimeException('Username must not be empty');
118
                }
119
120
                return $answer;
121
            }
122
        );
123
        $realNameQuestion->setMaxAttempts(2);
124
        $user->setUsername($questionHelper->ask($input, $output, $realNameQuestion));
125
126
        return $user;
127
    }
128
129
    /**
130
     * @param InputInterface  $input
131
     * @param OutputInterface $output
132
     * @param User            $user
133
     * @param QuestionHelper  $questionHelper
134
     *
135
     * @return User
136
     */
137
    protected function editRealName(
138
        InputInterface $input,
139
        OutputInterface $output,
140
        User $user,
141
        QuestionHelper $questionHelper
142
    ) {
143
        $realNameQuestion = new Question('Real Name [required]: ');
144
        $realNameQuestion->setValidator(
145
            function ($answer) {
146
                if (empty($answer)) {
147
                    throw new \RuntimeException('Real Name must not be empty');
148
                }
149
150
                return $answer;
151
            }
152
        );
153
        $realNameQuestion->setMaxAttempts(2);
154
        $user->setRealName($questionHelper->ask($input, $output, $realNameQuestion));
155
156
        return $user;
157
    }
158
159
    /**
160
     * @param InputInterface  $input
161
     * @param OutputInterface $output
162
     * @param User            $user
163
     * @param QuestionHelper  $questionHelper
164
     *
165
     * @return User
166
     */
167 View Code Duplication
    protected function editEmail(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
        InputInterface $input,
169
        OutputInterface $output,
170
        User $user,
171
        QuestionHelper $questionHelper
172
    ) {
173
        $emailQuestion = new Question('Email [required]: ');
174
        $emailQuestion->setValidator(
175
            function ($answer) {
176
                if (!filter_var($answer, FILTER_VALIDATE_EMAIL)) {
177
                    throw new \RuntimeException("This is not a valid email address");
178
                }
179
180
                return $answer;
181
            }
182
        );
183
        $emailQuestion->setMaxAttempts(2);
184
        $user->setEmail($questionHelper->ask($input, $output, $emailQuestion));
185
186
        return $user;
187
    }
188
189
    /**
190
     * @param InputInterface  $input
191
     * @param OutputInterface $output
192
     * @param User            $user
193
     * @param QuestionHelper  $questionHelper
194
     *
195
     * @return User
196
     */
197
    protected function editRole(
198
        InputInterface $input,
199
        OutputInterface $output,
200
        User $user,
201
        QuestionHelper $questionHelper
202
    ) {
203
        $roleQuestion = new ChoiceQuestion(
204
            'Role [required]: ',
205
            array('ROLE_WATCHER', 'ROLE_COMMITTER', 'ROLE_ADMIN'),
206
            null
207
        );
208
        $user->setRoles([$questionHelper->ask($input, $output, $roleQuestion)]);
209
210
        return $user;
211
    }
212
213
    /**
214
     * @param InputInterface  $input
215
     * @param OutputInterface $output
216
     * @param User            $user
217
     * @param QuestionHelper  $questionHelper
218
     * @param UserManagerInterface $userManager
219
     *
220
     * @return User
221
     */
222
    protected function editPassword(
223
        InputInterface $input,
224
        OutputInterface $output,
225
        User $user,
226
        QuestionHelper $questionHelper,
227
        UserManagerInterface $userManager
228
    ) {
229
        $passwordQuestion = new Question('Password (leave blank to disable form login): ');
230
        $passwordQuestion->setHidden(true);
231
        $passwordQuestion->setHiddenFallback(false);
232
        $passwordQuestion->setMaxAttempts(3);
233
        $password = $questionHelper->ask($input, $output, $passwordQuestion);
234
235
        if (null !== $password) {
236
            $user->setPassword($password);
237
            $userManager->updatePassword($user);
238
        } else {
239
            $user->setPassword(null);
240
        }
241
242
        return $user;
243
    }
244
245
    /**
246
     * @param InputInterface  $input
247
     * @param OutputInterface $output
248
     * @param User            $user
249
     * @param QuestionHelper  $questionHelper
250
     *
251
     * @return User
252
     */
253 View Code Duplication
    protected function editGithubId(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
254
        InputInterface $input,
255
        OutputInterface $output,
256
        User $user,
257
        QuestionHelper $questionHelper
258
    ) {
259
        $githubLoginQuestion = new Question('Github ID: ');
260
        $user->setGithubId($questionHelper->ask($input, $output, $githubLoginQuestion));
261
262
        return $user;
263
    }
264
265
    /**
266
     * @param InputInterface  $input
267
     * @param OutputInterface $output
268
     * @param User            $user
269
     * @param QuestionHelper  $questionHelper
270
     *
271
     * @return User
272
     */
273 View Code Duplication
    protected function editGoogleId(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
274
        InputInterface $input,
275
        OutputInterface $output,
276
        User $user,
277
        QuestionHelper $questionHelper
278
    ) {
279
        $googleLoginQuestion = new Question('Google ID: ');
280
        $user->setGoogleId($questionHelper->ask($input, $output, $googleLoginQuestion));
281
282
        return $user;
283
    }
284
285
    /**
286
     * @param InputInterface  $input
287
     * @param OutputInterface $output
288
     *
289
     * @return User
290
     */
291
    protected function selectUser(InputInterface $input, OutputInterface $output): User
292
    {
293
        $questionHelper = $this->getQuestionHelper();
294
        $users = $this->findUsers();
295
296
        $userChoices = array();
297
        foreach ($users as $user) {
298
            $userChoices[$user->getId()] = $user;
299
        }
300
        $idQuestion = new ChoiceQuestion(
301
            'Select User: ',
302
            $userChoices,
303
            null
304
        );
305
        /** @var User $user */
306
        $user = $questionHelper->ask($input, $output, $idQuestion);
307
308
        return $user;
309
    }
310
311
    /**
312
     * @return User[]
313
     */
314
    protected function findUsers()
315
    {
316
        $userManager = $this->getUserManager();
317
318
        /** @var User[] $users */
319
320
        return $userManager->findUsers();
321
    }
322
}
323