User   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 7
Bugs 1 Features 0
Metric Value
eloc 32
c 7
b 1
f 0
dl 0
loc 98
rs 10
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A findIdentityByAccessToken() 0 9 3
A getId() 0 3 1
A validatePassword() 0 3 1
A validateAuthKey() 0 3 1
A getAuthKey() 0 3 1
A findIdentity() 0 3 2
A findByUsername() 0 9 3
1
<?php
2
3
namespace app\models;
4
5
class User extends \yii\base\BaseObject implements \yii\web\IdentityInterface
6
{
7
    public $id;
8
    public $username;
9
    public $password;
10
    public $authKey;
11
    public $accessToken;
12
13
    private static $users = [
14
        '100' => [
15
            'id' => '100',
16
            'username' => 'admin',
17
            'password' => 'admin',
18
            'authKey' => 'test100key',
19
            'accessToken' => '100-token',
20
        ],
21
        '101' => [
22
            'id' => '101',
23
            'username' => 'demo',
24
            'password' => 'demo',
25
            'authKey' => 'test101key',
26
            'accessToken' => '101-token',
27
        ],
28
    ];
29
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public static function findIdentity($id)
35
    {
36
        return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public static function findIdentityByAccessToken($token, $type = null)
43
    {
44
        foreach (self::$users as $user) {
45
            if ($user['accessToken'] === $token) {
46
                return new static($user);
47
            }
48
        }
49
50
        return null;
51
    }
52
53
    /**
54
     * Finds user by username
55
     *
56
     * @param string $username
57
     * @return static|null
58
     */
59
    public static function findByUsername($username)
60
    {
61
        foreach (self::$users as $user) {
62
            if (strcasecmp($user['username'], $username) === 0) {
63
                return new static($user);
64
            }
65
        }
66
67
        return null;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getId()
74
    {
75
        return $this->id;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function getAuthKey()
82
    {
83
        return $this->authKey;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function validateAuthKey($authKey)
90
    {
91
        return $this->authKey === $authKey;
92
    }
93
94
    /**
95
     * Validates password
96
     *
97
     * @param string $password password to validate
98
     * @return bool if password provided is valid for current user
99
     */
100
    public function validatePassword($password)
101
    {
102
        return $this->password === $password;
103
    }
104
}
105