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

PromoteUserCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 2
dl 0
loc 45
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 15 1
A executeRoleCommand() 0 24 4
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