Completed
Pull Request — master (#2737)
by
unknown
08:55
created

PromoteUserCommand::executeRoleCommand()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 4
nc 4
nop 4
1
<?php
2
3
namespace Kunstmaan\AdminBundle\Command;
4
5
use Symfony\Component\Console\Output\OutputInterface;
6
7
final class PromoteUserCommand extends RoleCommand
8
{
9
    protected static $defaultName = 'kuma:user:promote';
10
11
    protected function configure()
12
    {
13
        parent::configure();
14
15
        $this
16
            ->setDescription('Promotes a user by adding a role')
17
            ->setHelp(<<<'EOT'
18
The <info>kuma:user:promote</info> command promotes a user by adding a role
19
20
  <info>php %command.full_name% matthieu ROLE_CUSTOM</info>
21
  <info>php %command.full_name% --super matthieu</info>
22
EOT
23
            );
24
    }
25
26
    protected function executeRoleCommand(OutputInterface $output, string $username, bool $super, string $role): int
27
    {
28
        $user = $this->userManager->findUserByUsername($username);
29
30
        if (!$user) {
31
            throw new InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
32
        }
33
34
        if ($super) {
35
            $user->setSuperAdmin(true);
36
            $output->writeln(sprintf('User "%s" has been promoted as a super administrator. This change will not apply until the user logs out and back in again.', $username));
37
        } else {
38
            if ($user->hasRole($role)) {
39
                $output->writeln(sprintf('User "%s" did already have "%s" role.', $username, $role));
40
41
                return;
42
            }
43
44
            $user->addRole($role);
45
            $output->writeln(sprintf('Role "%s" has been added to user "%s". This change will not apply until the user logs out and back in again.', $role, $username));
46
        }
47
48
        $this->userManager->updateUser($user);
49
50
        return 0;
51
    }
52
}
53