UserEditCommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 66
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
B execute() 0 53 9
1
<?php
2
3
namespace App\Command;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Console\Question\ChoiceQuestion;
8
use Symfony\Component\Console\Question\ConfirmationQuestion;
9
10
class UserEditCommand extends AbstractUserCommand
11
{
12
    const FIELD_PASSWORD = 'Password';
13
    const FIELD_REAL_NAME = 'Real Name';
14
15
    protected function configure()
16
    {
17
        $this
18
            ->setName('gitki:user:edit')
19
            ->setDescription('Edits an existing user');
20
    }
21
22
    protected function execute(InputInterface $input, OutputInterface $output)
23
    {
24
        $userManager = $this->getUserManager();
25
        $questionHelper = $this->getQuestionHelper();
26
27
        $user = $this->selectUser($input, $output);
28
29
        $fields = [
30
            self::FIELD_REAL_NAME,
31
            'Email',
32
            'Role',
33
            self::FIELD_PASSWORD,
34
            'Github Login',
35
            'Google Login',
36
            'Done'
37
        ];
38
39
        $fieldQuestion = new ChoiceQuestion(
40
            'Select Field: ',
41
            $fields
42
        );
43
        do {
44
            $this->printUser($user, $output);
45
            $field = $questionHelper->ask($input, $output, $fieldQuestion);
46
            switch ($field) {
47
                case self::FIELD_REAL_NAME:
48
                    $user = $this->editRealName($input, $output, $user, $questionHelper);
49
                    break;
50
                case 'Email':
51
                    $user = $this->editEmail($input, $output, $user, $questionHelper);
52
                    break;
53
                case 'Role':
54
                    $user = $this->editRole($input, $output, $user, $questionHelper);
55
                    break;
56
                case self::FIELD_PASSWORD:
57
                    $user = $this->editPassword($input, $output, $user, $questionHelper, $userManager);
0 ignored issues
show
Bug introduced by
It seems like $userManager defined by $this->getUserManager() on line 24 can be null; however, App\Command\AbstractUserCommand::editPassword() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
58
                    break;
59
                case 'Github Login':
60
                    $user = $this->editGithubId($input, $output, $user, $questionHelper);
61
                    break;
62
                case 'Google Login':
63
                    $user = $this->editGoogleId($input, $output, $user, $questionHelper);
64
                    break;
65
            }
66
        } while ('Done' !== $field);
67
68
        $this->printUser($user, $output);
69
70
        $saveQuestion = new ConfirmationQuestion('Save? ', false);
71
        if ($questionHelper->ask($input, $output, $saveQuestion)) {
72
            $userManager->updateUser($user);
73
        }
74
    }
75
}
76