LoginForm::login()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 0
1
<?php
2
3
namespace yii2mod\user\models;
4
5
use Yii;
6
use yii\base\Model;
7
use yii2mod\user\models\enums\UserStatus;
8
9
/**
10
 * Login Form
11
 *
12
 * @package yii2mod\user\models
13
 */
14
class LoginForm extends Model
15
{
16
    /**
17
     * @var string email
18
     */
19
    public $email;
20
21
    /**
22
     * @var string password
23
     */
24
    public $password;
25
26
    /**
27
     * @var bool remember me
28
     */
29
    public $rememberMe = true;
30
31
    /**
32
     * @var bool|UserModel
33
     */
34
    protected $user = false;
35
36
    /**
37
     * @inheritdoc
38
     */
39
    public function rules()
40
    {
41
        return [
42
            [['email', 'password'], 'required'],
43
            ['email', 'email'],
44
            ['password', 'validatePassword'],
45
            ['rememberMe', 'boolean'],
46
        ];
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52 View Code Duplication
    public function attributeLabels()
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...
53
    {
54
        return [
55
            'email' => Yii::t('yii2mod.user', 'Email'),
56
            'password' => Yii::t('yii2mod.user', 'Password'),
57
            'rememberMe' => Yii::t('yii2mod.user', 'Remember Me'),
58
        ];
59
    }
60
61
    /**
62
     * Validates the password.
63
     * This method serves as the inline validation for password.
64
     *
65
     * @param $attribute
66
     * @param $params
67
     */
68
    public function validatePassword($attribute, $params)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
    {
70
        if (!$this->hasErrors()) {
71
            $user = $this->getUser();
72
            if ($user && $user->status === UserStatus::DELETED) {
73
                $this->addError($attribute, Yii::t('yii2mod.user', 'Your account has been deactivated, please contact support for details.'));
74
            } elseif (!$user || !$user->validatePassword($this->password)) {
75
                $this->addError($attribute, Yii::t('yii2mod.user', 'Incorrect email or password.'));
76
            }
77
        }
78
    }
79
80
    /**
81
     * Logs in a user using the provided username and password.
82
     *
83
     * @return bool whether the user is logged in successfully
84
     */
85
    public function login()
86
    {
87
        if ($this->validate()) {
88
            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
89
        } else {
90
            return false;
91
        }
92
    }
93
94
    /**
95
     * Finds user by [[email]]
96
     *
97
     * @return UserModel|null
98
     */
99
    public function getUser()
100
    {
101
        if ($this->user === false) {
102
            $this->user = UserModel::findByEmail($this->email);
0 ignored issues
show
Documentation Bug introduced by
It seems like \yii2mod\user\models\Use...ndByEmail($this->email) can also be of type object<yii\db\ActiveRecordInterface> or array. However, the property $user is declared as type boolean|object<yii2mod\user\models\UserModel>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
103
        }
104
105
        return $this->user;
106
    }
107
}
108