Issues (19)

src/Command/GbUserAddCommand.php (2 issues)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gbere\SimpleAuth\Command;
6
7
use Doctrine\ORM\OptimisticLockException;
8
use Doctrine\ORM\ORMException;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Question\Question;
13
use Symfony\Component\Console\Style\SymfonyStyle;
14
use Symfony\Component\Validator\Constraints as Assert;
15
16
final class GbUserAddCommand extends AbstractCommand
17
{
18
    private const QUESTION_MAX_ATTEMPTS = 3;
19
20
    protected static $defaultName = 'gb:user:add';
21
22
    protected function configure(): void
23
    {
24
        $this
25
            ->setDescription('Register a new user')
26
            ->addArgument('email', InputArgument::OPTIONAL)
27
        ;
28
    }
29
30
    /**
31
     * @throws ORMException
32
     * @throws OptimisticLockException
33
     */
34
    protected function execute(InputInterface $input, OutputInterface $output): int
35
    {
36
        $io = new SymfonyStyle($input, $output);
37
        $helper = $this->getHelper('question');
38
        $email = $input->getArgument('email');
39
        $isEmailOk = false;
40
        $questionMaxAttempts = self::QUESTION_MAX_ATTEMPTS;
41
        if ($this->isTestEnv()) {
42
            $questionMaxAttempts = 1;
43
        }
44
45
        while (false === $isEmailOk) {
46
            if (null !== $email) {
47
                $emailConstraint = new Assert\Email();
48
                $errors = $this->validator->validate($email, $emailConstraint);
49
                if (0 < \count($errors)) {
50
                    $io->error($errors[0]->getMessage());
51
52
                    return 1;
53
                }
54
                if (null !== $this->findUserByEmail($email)) {
0 ignored issues
show
It seems like $email can also be of type string[]; however, parameter $email of Gbere\SimpleAuth\Command...mand::findUserByEmail() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
                if (null !== $this->findUserByEmail(/** @scrutinizer ignore-type */ $email)) {
Loading history...
55
                    $io->error(sprintf('The email %s is already registered', $email));
0 ignored issues
show
It seems like $email can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
                    $io->error(sprintf('The email %s is already registered', /** @scrutinizer ignore-type */ $email));
Loading history...
56
57
                    return 1;
58
                }
59
                $isEmailOk = true;
60
            } else {
61
                $question = new Question('Enter the email address of the new user: ');
62
                $question->setValidator(function ($answer) {
63
                    if (null === $answer) {
64
                        throw new \Exception('The email address is required to create a new user');
65
                    }
66
67
                    return $answer;
68
                });
69
                $question->setMaxAttempts($questionMaxAttempts);
70
                $email = $helper->ask($input, $output, $question);
71
            }
72
        }
73
74
        $question = new Question('Enter a password: ');
75
        $question->setValidator(function ($answer) {
76
            if (null === $answer || '' === trim($answer)) {
77
                throw new \Exception('The password can\'t be empty');
78
            }
79
80
            return $answer;
81
        });
82
        $question->setMaxAttempts($questionMaxAttempts);
83
        if (false === $this->isTestEnv()) {
84
            $question->setHidden(true);
85
        }
86
        $password = $helper->ask($input, $output, $question);
87
88
        $question = new Question('Enter the name of the new user: ');
89
        $question->setValidator(function ($answer) {
90
            if (null === $answer) {
91
                throw new \Exception('The name field is required to create a new user');
92
            }
93
94
            return $answer;
95
        });
96
        $question->setMaxAttempts($questionMaxAttempts);
97
        $name = $helper->ask($input, $output, $question);
98
99
        $user = $this->userRepository->createUser();
100
        $user->setEmail($email);
101
        $user->setName($name);
102
        $user->setPassword($this->userRepository->encodePassword($password));
103
        $this->userRepository->persistAndFlush($user);
104
105
        $io->success(sprintf('The new user with email %s, was successfully created', $user->getEmail()));
106
107
        return 0;
108
    }
109
}
110