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

UserEditCommand::execute()   C

Complexity

Conditions 9
Paths 14

Size

Total Lines 53
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 6.8963
c 0
b 0
f 0
cc 9
eloc 42
nc 14
nop 2

How to fix   Long Method   

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
namespace Dontdrinkandroot\Gitki\WebBundle\Command;
5
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Console\Question\ChoiceQuestion;
9
use Symfony\Component\Console\Question\ConfirmationQuestion;
10
11
class UserEditCommand extends AbstractUserCommand
12
{
13
    const FIELD_PASSWORD = 'Password';
14
    const FIELD_REAL_NAME = 'Real Name';
15
16
    protected function configure()
17
    {
18
        $this
19
            ->setName('gitki:user:edit')
20
            ->setDescription('Edits an existing user');
21
    }
22
23
    protected function execute(InputInterface $input, OutputInterface $output)
24
    {
25
        $userManager = $this->getUserManager();
26
        $questionHelper = $this->getQuestionHelper();
27
28
        $user = $this->selectUser($input, $output);
29
30
        $fields = [
31
            self::FIELD_REAL_NAME,
32
            'Email',
33
            'Role',
34
            self::FIELD_PASSWORD,
35
            'Github Login',
36
            'Google Login',
37
            'Done'
38
        ];
39
40
        $fieldQuestion = new ChoiceQuestion(
41
            'Select Field: ',
42
            $fields
43
        );
44
        do {
45
            $this->printUser($user, $output);
46
            $field = $questionHelper->ask($input, $output, $fieldQuestion);
47
            switch ($field) {
48
                case self::FIELD_REAL_NAME:
49
                    $user = $this->editRealName($input, $output, $user, $questionHelper);
50
                    break;
51
                case 'Email':
52
                    $user = $this->editEmail($input, $output, $user, $questionHelper);
53
                    break;
54
                case 'Role':
55
                    $user = $this->editRole($input, $output, $user, $questionHelper);
56
                    break;
57
                case self::FIELD_PASSWORD:
58
                    $user = $this->editPassword($input, $output, $user, $questionHelper, $userManager);
59
                    break;
60
                case 'Github Login':
61
                    $user = $this->editGithubId($input, $output, $user, $questionHelper);
62
                    break;
63
                case 'Google Login':
64
                    $user = $this->editGoogleId($input, $output, $user, $questionHelper);
65
                    break;
66
            }
67
        } while ('Done' !== $field);
68
69
        $this->printUser($user, $output);
70
71
        $saveQuestion = new ConfirmationQuestion('Save? ', false);
72
        if ($questionHelper->ask($input, $output, $saveQuestion)) {
73
            $userManager->updateUser($user);
74
        }
75
    }
76
}
77