Issues (19)

src/Command/GbUserPromoteCommand.php (3 issues)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gbere\SimpleAuth\Command;
6
7
use Gbere\SimpleAuth\Entity\Role;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Question\ChoiceQuestion;
12
use Symfony\Component\Console\Question\Question;
13
use Symfony\Component\Console\Style\SymfonyStyle;
14
15
final class GbUserPromoteCommand extends AbstractCommand
16
{
17
    private const QUESTION_MAX_ATTEMPTS = 3;
18
19
    protected static $defaultName = 'gb:user:promote';
20
21
    protected function configure(): void
22
    {
23
        $this
24
            ->setDescription('Promote a user')
25
            ->addArgument('email', InputArgument::OPTIONAL)
26
            ->addArgument('role', InputArgument::OPTIONAL)
27
        ;
28
    }
29
30
    protected function execute(InputInterface $input, OutputInterface $output): int
31
    {
32
        $io = new SymfonyStyle($input, $output);
33
        $helper = $this->getHelper('question');
34
        $email = $input->getArgument('email');
35
        $roleName = $input->getArgument('role');
36
37
        /** @var Role[] $allRoles */
38
        $allRoles = $this->findAllRoles();
39
        if (0 === \count($allRoles)) {
40
            $io->error('There is no role to select. Please, create some role first.');
41
42
            return 1;
43
        }
44
45
        if (null === $email) {
46
            $question = new Question('Please, enter the email address of the user: ');
47
            $question->setValidator(function ($answer) {
48
                if (null === $answer) {
49
                    throw new \Exception('The email address of the user to promote is required');
50
                }
51
52
                return $answer;
53
            });
54
            $question->setMaxAttempts(self::QUESTION_MAX_ATTEMPTS);
55
            if ($this->isTestEnv()) {
56
                $question->setMaxAttempts(1);
57
            }
58
            $email = $helper->ask($input, $output, $question);
59
        }
60
        $user = $this->findUserByEmail($email);
61
        if (null === $user) {
62
            $io->error(sprintf('The user with the email "%s" doesnt exist', $email));
63
64
            return 1;
65
        }
66
67
        $role = null;
68
        if (null !== $roleName) {
69
            $role = $this->findRoleByName($roleName);
0 ignored issues
show
It seems like $roleName can also be of type string[]; however, parameter $name of Gbere\SimpleAuth\Command...mmand::findRoleByName() 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

69
            $role = $this->findRoleByName(/** @scrutinizer ignore-type */ $roleName);
Loading history...
70
            if (null === $role) {
71
                $io->warning(sprintf('The role "%s" doesnt exist. Please, select a valid role', $roleName));
0 ignored issues
show
It seems like $roleName 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

71
                $io->warning(sprintf('The role "%s" doesnt exist. Please, select a valid role', /** @scrutinizer ignore-type */ $roleName));
Loading history...
72
            }
73
        }
74
        if (null === $role) {
75
            $question = new ChoiceQuestion('Select a role', $allRoles);
76
            $question->setErrorMessage('The role %s is invalid.');
77
            $roleName = $helper->ask($input, $output, $question);
78
            $role = $this->findRoleByName($roleName);
79
        }
80
        $user->addRoleEntity($role);
0 ignored issues
show
It seems like $role can also be of type null; however, parameter $role of Gbere\SimpleAuth\Model\U...erface::addRoleEntity() does only seem to accept Gbere\SimpleAuth\Entity\Role, 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

80
        $user->addRoleEntity(/** @scrutinizer ignore-type */ $role);
Loading history...
81
        $this->getEntityManager()->persist($user);
82
        $this->getEntityManager()->flush();
83
84
        $io->success(sprintf('The role %s was added to the user %s', $roleName, $email));
85
86
        return 0;
87
    }
88
}
89