Completed
Push — master ( d67956...a656c9 )
by Piotr
11s
created

PromoteUserCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 10
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace FSi\Bundle\AdminSecurityBundle\Command;
11
12
use FSi\Bundle\AdminSecurityBundle\Event\AdminSecurityEvents;
13
use FSi\Bundle\AdminSecurityBundle\Event\UserEvent;
14
use FSi\Bundle\AdminSecurityBundle\Security\User\UserInterface;
15
use FSi\Bundle\AdminSecurityBundle\Security\User\UserRepositoryInterface;
16
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
class PromoteUserCommand extends ContainerAwareCommand
22
{
23
    /**
24
     * @see Command
25
     */
26
    protected function configure()
27
    {
28
        $this
29
            ->setName('fsi:user:promote')
30
            ->setDescription('Promote a user.')
31
            ->setDefinition([
32
                new InputArgument('email', InputArgument::REQUIRED, 'The email'),
33
                new InputArgument('role', InputArgument::REQUIRED, 'The role'),
34
            ])
35
            ->setHelp(<<<EOT
36
The <info>fsi:user:promote</info> command promotes the user
37
38
  <info>php app/console fsi:user:promote [email protected] ROLE_ADMIN</info>
39
40
EOT
41
            );
42
    }
43
44
    /**
45
     * @see Command
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $email = $input->getArgument('email');
50
        $role = $input->getArgument('role');
51
52
        /** @var UserRepositoryInterface $userRepository */
53
        $userRepository = $this->getContainer()->get('admin_security.repository.user');
54
        $user = $userRepository->findUserByEmail($email);
55
        if (!($user instanceof UserInterface)) {
56
            throw new \InvalidArgumentException(sprintf('User with email "%s" cannot be found', $email));
57
        }
58
59
        $user->addRole($role);
60
61
        $this->getContainer()->get('event_dispatcher')->dispatch(
62
            AdminSecurityEvents::PROMOTE_USER,
63
            new UserEvent($user)
64
        );
65
66
        $output->writeln(sprintf('User <comment>%s</comment> has been promoted', $email));
67
    }
68
69
    /**
70
     * @see Command
71
     */
72
    protected function interact(InputInterface $input, OutputInterface $output)
73
    {
74
        if (!$input->getArgument('email')) {
75
            $email = $this->getHelper('dialog')->askAndValidate(
76
                $output,
77
                'Please choose an email:',
78
                function($email) {
79
                    if (empty($email)) {
80
                        throw new \Exception('Email can not be empty');
81
                    }
82
83
                    return $email;
84
                }
85
            );
86
            $input->setArgument('email', $email);
87
        }
88
89
        if (!$input->getArgument('role')) {
90
            $email = $this->getHelper('dialog')->askAndValidate(
91
                $output,
92
                'Please choose a role:',
93
                function($email) {
94
                    if (empty($email)) {
95
                        throw new \Exception('Role can not be empty');
96
                    }
97
98
                    return $email;
99
                }
100
            );
101
            $input->setArgument('role', $email);
102
        }
103
    }
104
}
105