ChangePasswordCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 44
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
B execute() 0 26 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