Passed
Push — master ( 8dfc3d...34c1c4 )
by jelmer
10:43 queued 05:29
created

ResetPasswordCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 48
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
A execute() 0 37 4
1
<?php
2
3
namespace Backend\Modules\Users\Console;
4
5
use Backend\Modules\Users\Engine\Model as BackendUsersModel;
6
use Backend\Core\Engine\User as BackendUser;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Question\Question;
12
13
class ResetPasswordCommand extends Command
14
{
15
    protected function configure(): void
16
    {
17
        $this
18
            ->setName('forkcms:users:reset-password')
19
            ->setDescription('Reset a users password')
20
            ->addArgument('email', InputArgument::OPTIONAL, '(Optional) The users email-address')
21
            ->addArgument('password', InputArgument::OPTIONAL, '(Optional) The desired new password');
22
    }
23
24
    protected function execute(InputInterface $input, OutputInterface $output): void
25
    {
26
        $helper = $this->getHelper('question');
27
28
        $questionEmailAddress = new Question('Please provide the users email-address: ');
29
        $questionEmailAddressAnswer = $input->getArgument('email');
30
31
        if ($questionEmailAddressAnswer === null) {
32
            $questionEmailAddressAnswer = $helper->ask($input, $output, $questionEmailAddress);
33
        }
34
35
        $subjectUserId = BackendUsersModel::getIdByEmail(
36
            $questionEmailAddressAnswer
37
        );
38
39
        if ($subjectUserId === false) {
40
            $output->writeln(
41
                sprintf('<error>User not found with email-address "%s".</error>', $questionEmailAddressAnswer)
42
            );
43
44
            return;
45
        }
46
47
        $questionPassword = new Question('Please provide the users new desired password: ');
48
        $questionPassword->setHidden(true);
49
        $questionPasswordAnswer = $input->getArgument('password');
50
51
        if ($questionPasswordAnswer === null) {
52
            $questionPasswordAnswer = $helper->ask($input, $output, $questionPassword);
53
        }
54
55
        BackendUsersModel::updatePassword(
56
            new BackendUser($subjectUserId, $questionEmailAddressAnswer),
57
            $questionPasswordAnswer
58
        );
59
60
        $output->writeln('Password has been updated');
61
    }
62
}
63