LoginForm::validatePassword()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7.3329

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 15
ccs 8
cts 12
cp 0.6667
rs 9.2222
cc 6
nc 5
nop 1
crap 7.3329
1
<?php
2
namespace App\Http\Form;
3
4
use App\Form\BaseForm;
5
use App\Model\User;
6
use Yii;
7
8
/**
9
 * Login form
10
 */
11
class LoginForm extends BaseForm
12
{
13
    public $username;
14
    public $password;
15
    public $rememberMe = true;
16
17
    private $user;
18
19
    /**
20
     * {@inheritdoc}
21
     */
22 3
    public function rules()
23
    {
24
        return [
25
            // username and password are both required
26 3
            [['username', 'password'], 'required'],
27
            // rememberMe must be a boolean value
28
            ['rememberMe', 'boolean'],
29
            // password is validated by validatePassword()
30
            ['password', 'validatePassword'],
31
        ];
32
    }
33
34
    public function attributeLabels()
35
    {
36
        return [
37
            'username' => Yii::t('app', 'Username'),
38
            'password' => Yii::t('app', 'Password'),
39
            'rememberMe' => Yii::t('app', 'Remember Me'),
40
        ];
41
    }
42
43
    /**
44
     * Validates the password.
45
     * This method serves as the inline validation for password.
46
     *
47
     * @param string $attribute the attribute currently being validated
48
     */
49 3
    public function validatePassword($attribute)
50
    {
51 3
        if (!$this->hasErrors()) {
52 3
            $user = $this->getUser();
53 3
            if (!$user || !$user->validatePassword($this->password)) {
54 2
                $this->addError($attribute, Yii::t('app', 'Incorrect username or password'));
55 2
                return;
56
            }
57 1
            if (!$user->isActive()) {
58
                $this->addError($attribute, Yii::t('app', 'Your account is inactive'));
59
                return;
60
            }
61 1
            if ($user->isDeleted()) {
62
                $this->addError($attribute, Yii::t('app', 'Your account was deleted'));
63
                return;
64
            }
65
        }
66 1
    }
67
68
    protected function handleInternal()
69
    {
70
        return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
0 ignored issues
show
Bug introduced by
It seems like $this->getUser() can also be of type null; however, parameter $identity of yii\web\User::login() does only seem to accept yii\web\IdentityInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
        return Yii::$app->user->login(/** @scrutinizer ignore-type */ $this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
Loading history...
Bug introduced by
The method login() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        return Yii::$app->user->/** @scrutinizer ignore-call */ login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
    }
72
73
    /**
74
     * Logs in a user using the provided username and password.
75
     *
76
     * @return bool whether the user is logged in successfully
77
     */
78
    public function login()
79
    {
80
        if ($this->validate()) {
81
            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
0 ignored issues
show
Bug introduced by
It seems like $this->getUser() can also be of type null; however, parameter $identity of yii\web\User::login() does only seem to accept yii\web\IdentityInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
            return Yii::$app->user->login(/** @scrutinizer ignore-type */ $this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
Loading history...
82
        }
83
84
        return false;
85
    }
86
87
    /**
88
     * Finds user by [[username]]
89
     *
90
     * @return User|null
91
     */
92 3
    protected function getUser()
93
    {
94 3
        if ($this->user === null) {
95 3
            $this->user = User::findByUsername($this->username);
96
        }
97
98 3
        return $this->user;
99
    }
100
}
101