Completed
Pull Request — master (#2737)
by Jeroen
10:25
created

PromoteUserCommand::executeRoleCommand()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 14
cp 0
rs 9.536
c 0
b 0
f 0
cc 4
nc 4
nop 4
crap 20
1
<?php
2
3
namespace Kunstmaan\AdminBundle\Command;
4
5
use Symfony\Component\Console\Output\OutputInterface;
6
7
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
            ->setName('kuma:user:promote')
17
            ->setDescription('Promotes a user by adding a role')
18
            ->setHelp(<<<'EOT'
19
The <info>kuma:user:promote</info> command promotes a user by adding a role
20
21
  <info>php %command.full_name% matthieu ROLE_CUSTOM</info>
22
  <info>php %command.full_name% --super matthieu</info>
23
EOT
24
            );
25
    }
26
27
    protected function executeRoleCommand(OutputInterface $output, $username, $super, $role)
28
    {
29
        $user = $this->userManager->findUserByUsername($username);
30
31
        if (!$user) {
32
            throw new InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
33
        }
34
35
        if ($super) {
36
            $user->setSuperAdmin(true);
37
            $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));
38
        } else {
39
            if ($user->hasRole($role)) {
40
                $output->writeln(sprintf('User "%s" did already have "%s" role.', $username, $role));
41
42
                return;
43
            }
44
45
            $user->addRole($role);
46
            $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));
47
        }
48
49
        $this->userManager->updateUser($user);
50
    }
51
}
52