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

ChangePasswordCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 85
rs 10
c 1
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 1
A execute() 0 18 2
B interact() 0 30 5
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\ChangePasswordEvent;
14
use FSi\Bundle\AdminSecurityBundle\Security\User\ChangeablePasswordInterface;
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\Output\OutputInterface;
19
20
class ChangePasswordCommand extends ContainerAwareCommand
21
{
22
    /**
23
     * @see Command
24
     */
25
    protected function configure()
26
    {
27
        $this
28
            ->setName('fsi:user:change-password')
29
            ->setDescription('Change password of a user.')
30
            ->setDefinition([
31
                new InputArgument('email', InputArgument::REQUIRED, 'The email'),
32
                new InputArgument('password', InputArgument::REQUIRED, 'The password'),
33
            ])
34
            ->setHelp(<<<EOT
35
The <info>fsi:user:change-password</info> command changes user's password:
36
37
  <info>php app/console fsi:user:change-password [email protected]</info>
38
39
This interactive shell will first ask you for a password.
40
41
You can alternatively specify the new password as a second argument:
42
43
  <info>php app/console fsi:user:change-password [email protected] mynewpassword</info>
44
45
EOT
46
            );
47
    }
48
49
    /**
50
     * @see Command
51
     */
52
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54
        $email = $input->getArgument('email');
55
        $password = $input->getArgument('password');
56
57
        $userRepository = $this->getContainer()->get('admin_security.repository.user');
58
        $user = $userRepository->findUserByEmail($email);
59
        if (!($user instanceof ChangeablePasswordInterface)) {
60
            throw new \InvalidArgumentException(sprintf('User with email "%s" cannot be found', $email));
61
        }
62
        $user->setPlainPassword($password);
63
64
        $this->getContainer()->get('event_dispatcher')->dispatch(
65
            AdminSecurityEvents::CHANGE_PASSWORD,
66
            new ChangePasswordEvent($user)
67
        );
68
69
        $output->writeln(sprintf('Changed password of user <comment>%s</comment>', $email));
70
    }
71
72
    /**
73
     * @see Command
74
     */
75
    protected function interact(InputInterface $input, OutputInterface $output)
76
    {
77
        if (!$input->getArgument('email')) {
78
            $email = $this->getHelper('dialog')->askAndValidate(
79
                $output,
80
                'Please choose an email:',
81
                function($email) {
82
                    if (empty($email)) {
83
                        throw new \Exception('Email can not be empty');
84
                    }
85
86
                    return $email;
87
                }
88
            );
89
            $input->setArgument('email', $email);
90
        }
91
92
        if (!$input->getArgument('password')) {
93
            $password = $this->getHelper('dialog')->askAndValidate(
94
                $output,
95
                'Please choose a password:',
96
                function($password) {
97
                    if (empty($password)) {
98
                        throw new \Exception('Password can not be empty');
99
                    }
100
101
                    return $password;
102
                }
103
            );
104
            $input->setArgument('password', $password);
105
        }
106
    }
107
}
108