ResetPasswordCommand::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 2
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
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 Jitamin\Console;
13
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Question\Question;
18
19
/**
20
 * Reset password command class.
21
 */
22
class ResetPasswordCommand extends BaseCommand
23
{
24
    /**
25
     * Configure the console command.
26
     *
27
     * @return void
28
     */
29
    protected function configure()
30
    {
31
        $this
32
            ->setName('user:reset-password')
33
            ->setDescription('Change user password')
34
            ->addArgument('username', InputArgument::REQUIRED, 'Username');
35
    }
36
37
    /**
38
     * Execute the console command.
39
     *
40
     * @param InputInterface  $output
41
     * @param OutputInterface $output
42
     *
43
     * @return void
44
     */
45
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47
        $helper = $this->getHelper('question');
48
        $username = $input->getArgument('username');
49
50
        $passwordQuestion = new Question('What is the new password for '.$username.'? (characters are not printed)'.PHP_EOL);
51
        $passwordQuestion->setHidden(true);
52
        $passwordQuestion->setHiddenFallback(false);
53
54
        $password = $helper->ask($input, $output, $passwordQuestion);
55
56
        $confirmationQuestion = new Question('Confirmation:'.PHP_EOL);
57
        $confirmationQuestion->setHidden(true);
58
        $confirmationQuestion->setHiddenFallback(false);
59
60
        $confirmation = $helper->ask($input, $output, $confirmationQuestion);
61
62
        if ($this->validatePassword($output, $password, $confirmation)) {
63
            $this->resetPassword($output, $username, $password);
64
        }
65
    }
66
67
    /**
68
     * Validate the given password.
69
     *
70
     * @param OutputInterface $output
71
     * @param string          $password
72
     * @param string          $confirmation
73
     *
74
     * @return bool
75
     */
76
    private function validatePassword(OutputInterface $output, $password, $confirmation)
77
    {
78
        list($valid, $errors) = $this->passwordResetValidator->validateModification([
0 ignored issues
show
Documentation introduced by
The property passwordResetValidator does not exist on object<Jitamin\Console\ResetPasswordCommand>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
79
            'password'     => $password,
80
            'confirmation' => $confirmation,
81
        ]);
82
83
        if (!$valid) {
84
            foreach ($errors as $error_list) {
85
                foreach ($error_list as $error) {
86
                    $output->writeln('<error>'.$error.'</error>');
87
                }
88
            }
89
        }
90
91
        return $valid;
92
    }
93
94
    /**
95
     * Reset the password.
96
     *
97
     * @param OutputInterface $output
98
     * @param string          $username
99
     * @param string          $password
100
     *
101
     * @return bool
102
     */
103
    private function resetPassword(OutputInterface $output, $username, $password)
104
    {
105
        $userId = $this->userModel->getIdByUsername($username);
0 ignored issues
show
Documentation introduced by
The property userModel does not exist on object<Jitamin\Console\ResetPasswordCommand>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
106
107
        if (empty($userId)) {
108
            $output->writeln('<error>User not found</error>');
109
110
            return false;
111
        }
112
113
        if (!$this->userModel->update(['id' => $userId, 'password' => $password])) {
0 ignored issues
show
Documentation introduced by
The property userModel does not exist on object<Jitamin\Console\ResetPasswordCommand>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
114
            $output->writeln('<error>Unable to update password</error>');
115
116
            return false;
117
        }
118
119
        $output->writeln('<info>Password updated successfully</info>');
120
121
        return true;
122
    }
123
}
124