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

CreateUserCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 110
rs 10
c 2
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
C interact() 0 45 7
A configure() 0 13 1
B execute() 0 25 3
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 Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
class CreateUserCommand extends ContainerAwareCommand
22
{
23
    /**
24
     * @see Command
25
     */
26
    protected function configure()
27
    {
28
        $this
29
            ->setName('fsi:user:create')
30
            ->setDescription('Create a user.')
31
            ->setDefinition([
32
                new InputArgument('email', InputArgument::REQUIRED, 'The email'),
33
                new InputArgument('password', InputArgument::REQUIRED, 'The password'),
34
                new InputArgument('role', InputArgument::REQUIRED, 'Role'),
35
                new InputOption('inactive', null, InputOption::VALUE_NONE, 'Set the user as inactive'),
36
                new InputOption('enforce-password-change', null, InputOption::VALUE_NONE, 'Enforce user to change password during next login'),
37
            ])
38
            ->setHelp(<<<EOT
39
The <info>fsi:user:create</info> command creates a user:
40
41
  <info>php app/console fsi:user:create</info>
42
43
This interactive shell will ask you for an email and then a password.
44
45
You can alternatively specify the email, password and role as the first, second and third arguments:
46
47
  <info>php app/console fsi:user:create [email protected] mypassword ROLE_ADMIN</info>
48
49
EOT
50
            );
51
    }
52
53
    /**
54
     * @see Command
55
     */
56
    protected function execute(InputInterface $input, OutputInterface $output)
57
    {
58
        $email = $input->getArgument('email');
59
        $password = $input->getArgument('password');
60
        $role = $input->getArgument('role');
61
62
        $userClass = $this->getContainer()->getParameter('admin_security.model.user');
63
        /** @var UserInterface $user */
64
        $user = new $userClass();
65
        $user->setEmail($email);
66
        $user->setPlainPassword($password);
67
        $user->addRole($role);
68
        if (!$input->getOption('inactive')) {
69
            $user->setEnabled(true);
70
        }
71
        if ($input->getOption('enforce-password-change')) {
72
            $user->enforcePasswordChange(true);
73
        }
74
75
        $this->getContainer()->get('event_dispatcher')->dispatch(
76
            AdminSecurityEvents::USER_CREATED,
77
            new UserEvent($user)
78
        );
79
80
        $output->writeln(sprintf('Created user <comment>%s</comment>', $email));
81
    }
82
83
    /**
84
     * @see Command
85
     */
86
    protected function interact(InputInterface $input, OutputInterface $output)
87
    {
88
        if (!$input->getArgument('email')) {
89
            $email = $this->getHelper('dialog')->askAndValidate(
90
                $output,
91
                'Please choose an email:',
92
                function($email) {
93
                    if (empty($email)) {
94
                        throw new \Exception('Email can not be empty');
95
                    }
96
97
                    return $email;
98
                }
99
            );
100
            $input->setArgument('email', $email);
101
        }
102
103
        if (!$input->getArgument('password')) {
104
            $password = $this->getHelper('dialog')->askAndValidate(
105
                $output,
106
                'Please choose a password:',
107
                function($password) {
108
                    if (empty($password)) {
109
                        throw new \Exception('Password can not be empty');
110
                    }
111
112
                    return $password;
113
                }
114
            );
115
            $input->setArgument('password', $password);
116
        }
117
118
        if (!$input->getArgument('role')) {
119
            $role = $this->getHelper('dialog')->askAndValidate(
120
                $output,
121
                'Please choose a role:',
122
                function($role) {
123
                    if (empty($role)) {
124
                        throw new \Exception('Role can not be empty');
125
                    }
126
127
                    return $role;
128
                }
129
            );
130
            $input->setArgument('role', $role);
131
        }
132
    }
133
}
134