Completed
Push — master ( dfad4d...8cf454 )
by Philip
21:24
created

AbstractUserCommand   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 309
Duplicated Lines 20.71 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 9
dl 64
loc 309
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getUserManager() 0 4 1
A getQuestionHelper() 0 4 1
A printUser() 0 13 1
A createUser() 0 21 1
A printUserTable() 0 20 2
A editUsername() 21 21 2
A editRealName() 0 21 2
A editEmail() 21 21 2
A editRole() 0 15 1
A editPassword() 0 22 2
A editGithubId() 11 11 1
A editGoogleId() 11 11 1
A selectUser() 0 19 2
A findUsers() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Dontdrinkandroot\Gitki\WebBundle\Command;
4
5
use Dontdrinkandroot\Gitki\WebBundle\Entity\User;
6
use FOS\UserBundle\Model\UserManagerInterface;
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Symfony\Component\Console\Helper\QuestionHelper;
9
use Symfony\Component\Console\Helper\Table;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Question\ChoiceQuestion;
13
use Symfony\Component\Console\Question\Question;
14
15
abstract class AbstractUserCommand extends ContainerAwareCommand
16
{
17
    /**
18
     * @return UserManagerInterface
19
     */
20
    protected function getUserManager()
21
    {
22
        return $this->getContainer()->get('fos_user.user_manager');
23
    }
24
25
    /**
26
     * @return QuestionHelper
27
     */
28
    protected function getQuestionHelper()
29
    {
30
        return $this->getHelper('question');
31
    }
32
33
    protected function printUser(User $user, OutputInterface $output)
34
    {
35
        $output->writeln('--------------------');
36
        $output->writeln('Id: ' . $user->getId());
37
        $output->writeln('Username: ' . $user->getUsername());
38
        $output->writeln('Real Name: ' . $user->getRealName());
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('Facebook Login:' . $user->getFacebookId());
44
        $output->writeln('--------------------');
45
    }
46
47
    /**
48
     * @param InputInterface  $input
49
     * @param OutputInterface $output
50
     *
51
     * @return mixed
52
     */
53
    protected function createUser(
54
        InputInterface $input,
55
        OutputInterface $output
56
    ) {
57
        $userManager = $this->getUserManager();
58
        $questionHelper = $this->getQuestionHelper();
59
60
        /** @var User $user */
61
        $user = $userManager->createUser();
62
        $user->setEnabled(true);
63
64
        $user = $this->editUsername($input, $output, $user, $questionHelper);
65
        $user = $this->editRealName($input, $output, $user, $questionHelper);
66
        $user = $this->editEmail($input, $output, $user, $questionHelper);
67
        $user = $this->editRole($input, $output, $user, $questionHelper);
68
        $user = $this->editPassword($input, $output, $user, $questionHelper, $userManager);
69
        $user = $this->editGithubId($input, $output, $user, $questionHelper);
70
        $user = $this->editGoogleId($input, $output, $user, $questionHelper);
71
72
        return $user;
73
    }
74
75
    /**
76
     * @param OutputInterface $output
77
     * @param User[]          $users
78
     */
79
    protected function printUserTable(OutputInterface $output, $users)
80
    {
81
        $table = new Table($output);
82
        $table->setHeaders(['ID', 'User Name', 'Email', 'Roles', 'GitHub ID', 'Google ID']);
83
84
        foreach ($users as $user) {
85
            $table->addRow(
86
                [
87
                    $user->getId(),
88
                    $user->getUsername(),
89
                    $user->getEmail(),
90
                    implode(',', $user->getRoles()),
91
                    $user->getGithubId(),
92
                    $user->getGoogleId()
93
                ]
94
            );
95
        }
96
97
        $table->render();
98
    }
99
100
    /**
101
     * @param InputInterface  $input
102
     * @param OutputInterface $output
103
     * @param User            $user
104
     * @param QuestionHelper  $questionHelper
105
     *
106
     * @return User
107
     */
108 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...
109
        InputInterface $input,
110
        OutputInterface $output,
111
        User $user,
112
        QuestionHelper $questionHelper
113
    ) {
114
        $realNameQuestion = new Question('Username [required]: ');
115
        $realNameQuestion->setValidator(
116
            function ($answer) {
117
                if (empty($answer)) {
118
                    throw new \RuntimeException('Username must not be empty');
119
                }
120
121
                return $answer;
122
            }
123
        );
124
        $realNameQuestion->setMaxAttempts(2);
125
        $user->setUsername($questionHelper->ask($input, $output, $realNameQuestion));
126
127
        return $user;
128
    }
129
130
    /**
131
     * @param InputInterface  $input
132
     * @param OutputInterface $output
133
     * @param User            $user
134
     * @param QuestionHelper  $questionHelper
135
     *
136
     * @return User
137
     */
138
    protected function editRealName(
139
        InputInterface $input,
140
        OutputInterface $output,
141
        User $user,
142
        QuestionHelper $questionHelper
143
    ) {
144
        $realNameQuestion = new Question('Real Name [required]: ');
145
        $realNameQuestion->setValidator(
146
            function ($answer) {
147
                if (empty($answer)) {
148
                    throw new \RuntimeException('Real Name must not be empty');
149
                }
150
151
                return $answer;
152
            }
153
        );
154
        $realNameQuestion->setMaxAttempts(2);
155
        $user->setRealName($questionHelper->ask($input, $output, $realNameQuestion));
156
157
        return $user;
158
    }
159
160
    /**
161
     * @param InputInterface  $input
162
     * @param OutputInterface $output
163
     * @param User            $user
164
     * @param QuestionHelper  $questionHelper
165
     *
166
     * @return User
167
     */
168 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...
169
        InputInterface $input,
170
        OutputInterface $output,
171
        User $user,
172
        QuestionHelper $questionHelper
173
    ) {
174
        $emailQuestion = new Question('Email [required]: ');
175
        $emailQuestion->setValidator(
176
            function ($answer) {
177
                if (!filter_var($answer, FILTER_VALIDATE_EMAIL)) {
178
                    throw new \RuntimeException("This is not a valid email address");
179
                }
180
181
                return $answer;
182
            }
183
        );
184
        $emailQuestion->setMaxAttempts(2);
185
        $user->setEmail($questionHelper->ask($input, $output, $emailQuestion));
186
187
        return $user;
188
    }
189
190
    /**
191
     * @param InputInterface  $input
192
     * @param OutputInterface $output
193
     * @param User            $user
194
     * @param QuestionHelper  $questionHelper
195
     *
196
     * @return User
197
     */
198
    protected function editRole(
199
        InputInterface $input,
200
        OutputInterface $output,
201
        User $user,
202
        QuestionHelper $questionHelper
203
    ) {
204
        $roleQuestion = new ChoiceQuestion(
205
            'Role [required]: ',
206
            array('ROLE_WATCHER', 'ROLE_COMMITTER', 'ROLE_ADMIN'),
207
            null
208
        );
209
        $user->setRoles([$questionHelper->ask($input, $output, $roleQuestion)]);
210
211
        return $user;
212
    }
213
214
    /**
215
     * @param InputInterface       $input
216
     * @param OutputInterface      $output
217
     * @param User                 $user
218
     * @param QuestionHelper       $questionHelper
219
     * @param UserManagerInterface $userManager
220
     *
221
     * @return User
222
     */
223
    protected function editPassword(
224
        InputInterface $input,
225
        OutputInterface $output,
226
        User $user,
227
        QuestionHelper $questionHelper,
228
        UserManagerInterface $userManager
229
    ) {
230
        $passwordQuestion = new Question('Password (leave blank to disable form login): ');
231
        $passwordQuestion->setHidden(true);
232
        $passwordQuestion->setHiddenFallback(false);
233
        $passwordQuestion->setMaxAttempts(3);
234
        $password = $questionHelper->ask($input, $output, $passwordQuestion);
235
236
        if (null !== $password) {
237
            $user->setPassword($password);
238
            $userManager->updatePassword($user);
239
        } else {
240
            $user->setPassword(null);
241
        }
242
243
        return $user;
244
    }
245
246
    /**
247
     * @param InputInterface  $input
248
     * @param OutputInterface $output
249
     * @param User            $user
250
     * @param QuestionHelper  $questionHelper
251
     *
252
     * @return User
253
     */
254 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...
255
        InputInterface $input,
256
        OutputInterface $output,
257
        User $user,
258
        QuestionHelper $questionHelper
259
    ) {
260
        $githubLoginQuestion = new Question('Github ID: ');
261
        $user->setGithubId($questionHelper->ask($input, $output, $githubLoginQuestion));
262
263
        return $user;
264
    }
265
266
    /**
267
     * @param InputInterface  $input
268
     * @param OutputInterface $output
269
     * @param User            $user
270
     * @param QuestionHelper  $questionHelper
271
     *
272
     * @return User
273
     */
274 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...
275
        InputInterface $input,
276
        OutputInterface $output,
277
        User $user,
278
        QuestionHelper $questionHelper
279
    ) {
280
        $googleLoginQuestion = new Question('Google ID: ');
281
        $user->setGoogleId($questionHelper->ask($input, $output, $googleLoginQuestion));
282
283
        return $user;
284
    }
285
286
    /**
287
     * @param InputInterface  $input
288
     * @param OutputInterface $output
289
     *
290
     * @return User
291
     */
292
    protected function selectUser(InputInterface $input, OutputInterface $output): User
293
    {
294
        $questionHelper = $this->getQuestionHelper();
295
        $users = $this->findUsers();
296
297
        $userChoices = array();
298
        foreach ($users as $user) {
299
            $userChoices[$user->getId()] = $user;
300
        }
301
        $idQuestion = new ChoiceQuestion(
302
            'Select User: ',
303
            $userChoices,
304
            null
305
        );
306
        /** @var User $user */
307
        $user = $questionHelper->ask($input, $output, $idQuestion);
308
309
        return $user;
310
    }
311
312
    /**
313
     * @return User[]
314
     */
315
    protected function findUsers()
316
    {
317
        $userManager = $this->getUserManager();
318
319
        /** @var User[] $users */
320
321
        return $userManager->findUsers();
322
    }
323
}
324