ChangePasswordCommand::execute()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 12
cts 12
cp 1
rs 8.8817
c 0
b 0
f 0
cc 6
nc 5
nop 2
crap 6
1
<?php
2
3
namespace SilverLeague\Console\Command\Member;
4
5
use SilverStripe\ORM\ValidationResult;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * Change a member's password
12
 *
13
 * @package silverstripe-console
14
 * @author  Robbie Averill <[email protected]>
15
 */
16
class ChangePasswordCommand extends AbstractMemberCommand
17
{
18
    /**
19
     * {@inheritDoc}
20
     */
21
    protected function configure()
22 3
    {
23
        $this
24
            ->setName('member:change-password')
25 3
            ->setDescription("Change a member's password")
26 3
            ->addArgument('email', InputArgument::OPTIONAL, 'Email address')
27 3
            ->addArgument('password', InputArgument::OPTIONAL, 'New password');
28 3
    }
29 3
30
    /**
31
     * {@inheritDoc}
32
     */
33
    protected function execute(InputInterface $input, OutputInterface $output)
34 2
    {
35
        $email = $this->getOrAskForArgument($input, $output, 'email', 'Enter email address: ');
36 2
        $password = $this->getOrAskForArgument($input, $output, 'password', 'Enter password: ');
37 2
        if (empty($email) || empty($password)) {
38 2
            $output->writeln('<error>Please enter an email address and a new password.</error>');
39
            return;
40
        }
41
42
        $member = $this->getMember($input, $output);
43 2
        if (!$member) {
44 2
            return;
45 1
        }
46 1
47
        /** @var ValidationResult $result */
48
        $result = $member->changePassword($password);
49
        if ($result->isValid()) {
50 1
            $output->writeln('<info>Password updated.</info>');
51 1
            return;
52
        }
53
54 1
        $output->writeln('<error>Failed to save the new password.</error>');
55 1
        foreach ($result->getMessages() as $messageDetails) {
56
            $output->writeln('<error> * ' . $messageDetails['message'] . '</error>');
57
        }
58
    }
59
}
60