Passed
Push — master ( 77290b...f3fb78 )
by Marcel
09:09
created

AddUserCommand::execute()   B

Complexity

Conditions 9
Paths 2

Size

Total Lines 85
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 9
eloc 47
c 3
b 1
f 0
nc 2
nop 2
dl 0
loc 85
ccs 0
cts 51
cp 0
crap 90
rs 7.6008

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
namespace App\Command;
4
5
use RuntimeException;
6
use App\Entity\User;
7
use App\Entity\UserType;
8
use App\Repository\UserRepositoryInterface;
9
use App\Repository\UserTypeRepositoryInterface;
10
use Symfony\Component\Console\Attribute\AsCommand;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Question\ChoiceQuestion;
15
use Symfony\Component\Console\Question\ConfirmationQuestion;
16
use Symfony\Component\Console\Style\SymfonyStyle;
17
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
18
use Symfony\Component\Validator\Constraints\Email;
19
use Symfony\Component\Validator\Validator\ValidatorInterface;
20
21
#[AsCommand(name: 'app:add-user', description: 'Adds a new user')]
22
class AddUserCommand extends Command {
23
24
    public function __construct(private readonly UserTypeRepositoryInterface $userTypeRepository, private readonly UserRepositoryInterface $userRepository,
25
                                private readonly UserPasswordHasherInterface $passwordHasher, private readonly ValidatorInterface $validator, $name = null)
26
    {
27
        parent::__construct($name);
28
    }
29
30
    public function execute(InputInterface $input, OutputInterface $output): int {
31
        $io = new SymfonyStyle($input, $output);
32
33
        $username = $io->ask('Username', null, function($username) {
34
            if(empty($username)) {
35
                throw new RuntimeException('Username must not be empty');
36
            }
37
38
            if($this->validator->validate($username, new Email())->count() > 0) {
39
                throw new RuntimeException('Username must be an email address.');
40
            }
41
42
            return $username;
43
        });
44
45
        $firstname = $io->ask('Firstname', null, function($firstname) {
46
            if(empty($firstname)) {
47
                throw new RuntimeException('Firstname must not be empty');
48
            }
49
50
            return $firstname;
51
        });
52
53
        $lastname = $io->ask('Lastname', null, function($lastname) {
54
            if(empty($lastname)) {
55
                throw new RuntimeException('Lastname must not be empty');
56
            }
57
58
            return $lastname;
59
        });
60
61
        $email = $io->ask('E-Mail', null, function($email) {
62
            if(empty($email)) {
63
                throw new RuntimeException('E-Mail must not be empty');
64
            }
65
66
            return $email;
67
        });
68
69
        $password = $io->askHidden('Password', function($password) {
70
            if(empty($password)) {
71
                throw new RuntimeException('Password must not be empty');
72
            }
73
74
            return $password;
75
        });
76
77
        $io->askHidden('Repeat Password', function($repeatPassword) use ($password) {
78
            if($repeatPassword !== $password) {
79
                throw new RuntimeException('Passwords must match');
80
            }
81
82
            return $repeatPassword;
83
        });
84
85
        $question = new ConfirmationQuestion('Is this an administrator?');
86
        $isAdmin = $io->askQuestion($question);
87
88
        $userTypes = $this->userTypeRepository->findAll();
89
        $choices = array_map(fn(UserType $type) => $type->getAlias(), $userTypes);
90
91
        $question = new ChoiceQuestion('Select user type', $choices, 0);
92
        $selectedUserTypeAlias = $io->askQuestion($question);
93
        $userType = $userTypes[array_search($selectedUserTypeAlias, $choices)];
94
95
        $user = (new User())
96
            ->setUsername($username)
97
            ->setFirstname($firstname)
98
            ->setLastname($lastname)
99
            ->setEmail($email)
100
            ->setType($userType);
101
102
        if($isAdmin === true) {
103
            $user->setRoles(['ROLE_SUPER_ADMIN']);
104
        }
105
106
        $encodedPassword = $this->passwordHasher->hashPassword($user, $password);
107
108
        $user->setPassword($encodedPassword);
109
110
        $this->userRepository->persist($user);
111
112
        $io->success('User successfully added');
113
114
        return 0;
115
    }
116
}