Completed
Push — master ( df46a8...ddc7c0 )
by Misbahul D
03:24
created

User   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 5

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 19
c 2
b 0
f 2
lcom 4
cbo 5
dl 0
loc 170
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A tableName() 0 4 1
A behaviors() 0 6 1
A rules() 0 7 1
A findIdentity() 0 4 1
A findIdentityByAccessToken() 0 4 1
A findByUsername() 0 4 1
A findByPasswordResetToken() 0 11 2
A isPasswordResetTokenValid() 0 10 2
A getId() 0 4 1
A getAuthKey() 0 4 1
A validateAuthKey() 0 4 1
A validatePassword() 0 4 1
A setPassword() 0 4 1
A generateAuthKey() 0 4 1
A generatePasswordResetToken() 0 4 1
A removePasswordResetToken() 0 4 1
A getDb() 0 4 1
1
<?php
2
3
namespace mdm\admin\models;
4
5
use Yii;
6
use yii\base\NotSupportedException;
7
use yii\behaviors\TimestampBehavior;
8
use yii\db\ActiveRecord;
9
use yii\web\IdentityInterface;
10
use mdm\admin\components\Configs;
11
12
/**
13
 * User model
14
 *
15
 * @property integer $id
16
 * @property string $username
17
 * @property string $password_hash
18
 * @property string $password_reset_token
19
 * @property string $email
20
 * @property string $auth_key
21
 * @property integer $status
22
 * @property integer $created_at
23
 * @property integer $updated_at
24
 * @property string $password write-only password
25
 *
26
 * @property UserProfile $profile
27
 */
28
class User extends ActiveRecord implements IdentityInterface
29
{
30
    const STATUS_INACTIVE = 0;
31
    const STATUS_ACTIVE = 10;
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public static function tableName()
37
    {
38
        return Configs::instance()->userTable;
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function behaviors()
45
    {
46
        return [
47
            TimestampBehavior::className(),
48
        ];
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function rules()
55
    {
56
        return [
57
            ['status', 'default', 'value' => self::STATUS_ACTIVE],
58
            ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_INACTIVE]],
59
        ];
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public static function findIdentity($id)
66
    {
67
        return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return static::findOne(a... self::STATUS_ACTIVE)); (array|boolean) is incompatible with the return type declared by the interface yii\web\IdentityInterface::findIdentity of type yii\web\IdentityInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public static function findIdentityByAccessToken($token, $type = null)
74
    {
75
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
76
    }
77
78
    /**
79
     * Finds user by username
80
     *
81
     * @param string $username
82
     * @return static|null
83
     */
84
    public static function findByUsername($username)
85
    {
86
        return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
87
    }
88
89
    /**
90
     * Finds user by password reset token
91
     *
92
     * @param string $token password reset token
93
     * @return static|null
94
     */
95
    public static function findByPasswordResetToken($token)
96
    {
97
        if (!static::isPasswordResetTokenValid($token)) {
98
            return null;
99
        }
100
101
        return static::findOne([
102
                'password_reset_token' => $token,
103
                'status' => self::STATUS_ACTIVE,
104
        ]);
105
    }
106
107
    /**
108
     * Finds out if password reset token is valid
109
     *
110
     * @param string $token password reset token
111
     * @return boolean
112
     */
113
    public static function isPasswordResetTokenValid($token)
114
    {
115
        if (empty($token)) {
116
            return false;
117
        }
118
        $expire = Yii::$app->params['user.passwordResetTokenExpire'];
119
        $parts = explode('_', $token);
120
        $timestamp = (int) end($parts);
121
        return $timestamp + $expire >= time();
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127
    public function getId()
128
    {
129
        return $this->getPrimaryKey();
130
    }
131
132
    /**
133
     * @inheritdoc
134
     */
135
    public function getAuthKey()
136
    {
137
        return $this->auth_key;
138
    }
139
140
    /**
141
     * @inheritdoc
142
     */
143
    public function validateAuthKey($authKey)
144
    {
145
        return $this->getAuthKey() === $authKey;
146
    }
147
148
    /**
149
     * Validates password
150
     *
151
     * @param string $password password to validate
152
     * @return boolean if password provided is valid for current user
153
     */
154
    public function validatePassword($password)
155
    {
156
        return Yii::$app->security->validatePassword($password, $this->password_hash);
157
    }
158
159
    /**
160
     * Generates password hash from password and sets it to the model
161
     *
162
     * @param string $password
163
     */
164
    public function setPassword($password)
165
    {
166
        $this->password_hash = Yii::$app->security->generatePasswordHash($password);
167
    }
168
169
    /**
170
     * Generates "remember me" authentication key
171
     */
172
    public function generateAuthKey()
173
    {
174
        $this->auth_key = Yii::$app->security->generateRandomString();
175
    }
176
177
    /**
178
     * Generates new password reset token
179
     */
180
    public function generatePasswordResetToken()
181
    {
182
        $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
183
    }
184
185
    /**
186
     * Removes password reset token
187
     */
188
    public function removePasswordResetToken()
189
    {
190
        $this->password_reset_token = null;
191
    }
192
193
    public static function getDb()
194
    {
195
        return Configs::userDb();
196
    }
197
}
198