Completed
Push — master ( 01b890...0ab6a2 )
by Robbie
01:15
created

ChangePasswordCommand::execute()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

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