LoginForm::getUser()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
c 1
b 0
f 1
dl 0
loc 6
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace modules\users\models;
4
5
use Yii;
6
use yii\base\Model;
7
use modules\users\Module;
8
9
/**
10
 * Class LoginForm
11
 * @package modules\users\models
12
 *
13
 * @property string $email Email
14
 * @property string $password Password
15
 * @property-read null|array|User $user
16
 * @property bool $rememberMe Remember Me
17
 */
18
class LoginForm extends Model
19
{
20
    public $email;
21
    public $password;
22
    public $rememberMe = false;
23
24
    private $user;
25
26
    /**
27
     * @inheritdoc
28
     * @return array
29
     */
30
    public function rules()
31
    {
32
        return [
33
            // username and password are both required
34
            [['email', 'password'], 'required'],
35
            [['email'], 'email'],
36
            // rememberMe must be a boolean value
37
            ['rememberMe', 'boolean'],
38
            // password is validated by validatePassword()
39
            ['password', 'validatePassword'],
40
        ];
41
    }
42
43
    /**
44
     * @inheritdoc
45
     * @return array
46
     */
47
    public function attributeLabels()
48
    {
49
        return [
50
            'username' => Module::t('module', 'Username'),
51
            'email' => Module::t('module', 'Email'),
52
            'password' => Module::t('module', 'Password'),
53
            'rememberMe' => Module::t('module', 'Remember Me'),
54
        ];
55
    }
56
57
    /**
58
     * Validates the password.
59
     * This method serves as the inline validation for password.
60
     *
61
     * @param string $attribute the attribute currently being validated
62
     */
63
    public function validatePassword($attribute)
64
    {
65
        if (!$this->hasErrors()) {
66
            $user = $this->getUser();
67
            if (!$user || !$user->validatePassword($this->password)) {
68
                $this->addError($attribute, Module::t('module', 'Invalid email or password.'));
69
            }
70
        }
71
    }
72
73
    /**
74
     * Logs in a users using the provided username and password.
75
     *
76
     * @return bool whether the users 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
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

81
            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...
Bug introduced by
It seems like $this->getUser() can also be of type array; 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
        } else {
83
            return false;
84
        }
85
    }
86
87
    /**
88
     * Logout users
89
     * @return bool
90
     */
91
    public function logout()
92
    {
93
        return Yii::$app->user->logout();
94
    }
95
96
    /**
97
     * Finds users by [[username]]
98
     *
99
     * @return array|null|User
100
     */
101
    protected function getUser()
102
    {
103
        if ($this->user === null) {
104
            $this->user = User::findByUsernameEmail($this->email);
0 ignored issues
show
Bug introduced by
The property user is declared read-only in modules\users\models\LoginForm.
Loading history...
105
        }
106
        return $this->user;
107
    }
108
}
109