Completed
Push — master ( 9d1b93...59e655 )
by Misbahul D
02:36
created

ResetPassword::isPasswordResetTokenValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
c 0
b 0
f 0
rs 9.9332
cc 2
nc 2
nop 1
1
<?php
2
3
namespace mdm\admin\models\form;
4
5
use mdm\admin\components\UserStatus;
6
use mdm\admin\models\User;
7
use Yii;
8
use yii\base\InvalidParamException;
9
use yii\base\Model;
10
use yii\helpers\ArrayHelper;
11
12
/**
13
 * Password reset form
14
 */
15
class ResetPassword extends Model
16
{
17
    public $password;
18
    public $retypePassword;
19
    /**
20
     * @var User
21
     */
22
    private $_user;
23
24
    /**
25
     * Creates a form model given a token.
26
     *
27
     * @param  string $token
28
     * @param  array $config name-value pairs that will be used to initialize the object properties
29
     * @throws InvalidParamException if token is empty or not valid
30
     */
31
    public function __construct($token, $config = [])
32
    {
33
        if (empty($token) || !is_string($token)) {
34
            throw new InvalidParamException('Password reset token cannot be blank.');
35
        }
36
        // check token
37
        $class = Yii::$app->getUser()->identityClass ?: 'mdm\admin\models\User';
0 ignored issues
show
Bug introduced by
The method getUser does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
38
        if (static::isPasswordResetTokenValid($token)) {
39
            $this->_user = $class::findOne([
40
                    'password_reset_token' => $token,
41
                    'status' => UserStatus::ACTIVE
42
            ]);
43
        }
44
        if (!$this->_user) {
45
            throw new InvalidParamException('Wrong password reset token.');
46
        }
47
        parent::__construct($config);
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function rules()
54
    {
55
        return [
56
            [['password', 'retypePassword'], 'required'],
57
            ['password', 'string', 'min' => 6],
58
            ['retypePassword', 'compare', 'compareAttribute' => 'password']
59
        ];
60
    }
61
62
    /**
63
     * Resets password.
64
     *
65
     * @return boolean if password was reset.
66
     */
67
    public function resetPassword()
68
    {
69
        $user = $this->_user;
70
        $user->setPassword($this->password);
71
        $user->removePasswordResetToken();
72
73
        return $user->save(false);
74
    }
75
76
    /**
77
     * Finds out if password reset token is valid
78
     *
79
     * @param string $token password reset token
80
     * @return boolean
81
     */
82 View Code Duplication
    public static function isPasswordResetTokenValid($token)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        if (empty($token)) {
85
            return false;
86
        }
87
        $expire = ArrayHelper::getValue(Yii::$app->params, 'user.passwordResetTokenExpire', 24 * 3600);
88
        $parts = explode('_', $token);
89
        $timestamp = (int) end($parts);
90
        return $timestamp + $expire >= time();
91
    }
92
}
93