Completed
Push — master ( 55daec...2d65d3 )
by Kamil
18:26
created

ChangePasswordCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9
Metric Value
wmc 11
lcom 1
cbo 9
dl 0
loc 107
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 18 1
A execute() 0 17 2
B interact() 0 32 5
A changePassword() 0 5 1
A getEntityManager() 0 4 1
A getUserRepository() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\UserBundle\Command;
13
14
use Doctrine\Common\Persistence\ObjectManager;
15
use Sylius\Component\User\Model\UserInterface;
16
use Sylius\Component\User\Repository\UserRepositoryInterface;
17
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
/**
23
 * @author Loïc Frémont <[email protected]>
24
 */
25
class ChangePasswordCommand extends ContainerAwareCommand
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function configure()
31
    {
32
        $this
33
            ->setName('sylius:user:change-password')
34
            ->setDescription('Change the password of a user.')
35
            ->setDefinition(array(
36
                new InputArgument('email', InputArgument::REQUIRED, 'The email'),
37
                new InputArgument('password', InputArgument::REQUIRED, 'The password'),
38
            ))
39
            ->setHelp(<<<EOT
40
The <info>sylius:user:change-password</info> command changes the password of a user:
41
  <info>php app/console sylius:user:change-password [email protected]</info>
42
This interactive shell will first ask you for a password.
43
You can alternatively specify the password as a second argument:
44
  <info>php app/console fos:user:change-password [email protected] mypassword</info>
45
EOT
46
            );
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54
        $email = $input->getArgument('email');
55
        $password = $input->getArgument('password');
56
57
        /** @var UserInterface $user */
58
        $user = $this->getUserRepository()->findOneByEmail($email);
59
60
        if (null === $user) {
61
            throw new \InvalidArgumentException(sprintf('Could not find user identified by email "%s"', $email));
62
        }
63
64
        $this->changePassword($user, $password);
65
        $this->getEntityManager()->flush();
66
67
        $output->writeln(sprintf('Change password user <comment>%s</comment>', $email));
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    protected function interact(InputInterface $input, OutputInterface $output)
74
    {
75
        if (!$input->getArgument('email')) {
76
            $email = $this->getHelper('dialog')->askAndValidate(
77
                $output,
78
                'Please enter an email:',
79
                function($email) {
80
                    if (empty($email)) {
81
                        throw new \Exception('Email can not be empty');
82
                    }
83
                    return $email;
84
                }
85
            );
86
87
            $input->setArgument('email', $email);
88
        }
89
90
        if (!$input->getArgument('password')) {
91
            $password = $this->getHelper('dialog')->askHiddenResponseAndValidate(
92
                $output,
93
                'Please choose a password:',
94
                function($password) {
95
                    if (empty($password)) {
96
                        throw new \Exception('Password can not be empty');
97
                    }
98
                    return $password;
99
                }
100
            );
101
102
            $input->setArgument('password', $password);
103
        }
104
    }
105
106
    /**
107
     * @param UserInterface $user
108
     * @param string $password
109
     */
110
    protected function changePassword(UserInterface $user, $password)
111
    {
112
        $user->setPlainPassword($password);
113
        $this->getContainer()->get('sylius.user.password_updater')->updatePassword($user);
114
    }
115
116
    /**
117
     * @return ObjectManager
118
     */
119
    protected function getEntityManager()
120
    {
121
        return $this->getContainer()->get('sylius.manager.user');
122
    }
123
124
    /**
125
     * @return UserRepositoryInterface
126
     */
127
    protected function getUserRepository()
128
    {
129
        return $this->getContainer()->get('sylius.repository.user');
130
    }
131
}
132