IdentityTrait   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 237
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 20.99%

Importance

Changes 9
Bugs 2 Features 0
Metric Value
wmc 35
c 9
b 2
f 0
lcom 1
cbo 2
dl 0
loc 237
ccs 17
cts 81
cp 0.2099
rs 9

19 Methods

Rating   Name   Duplication   Size   Complexity  
A findIdentity() 0 5 1
A findIdentityByGuid() 0 4 1
A findIdentityByAccessToken() 0 5 1
A getAuthKey() 0 5 2
A setAuthKey() 0 5 2
A validateAuthKey() 0 4 1
A getAuthKeyRules() 0 10 2
A setAuthKeyRules() 0 6 3
A onInitAuthKey() 0 6 1
A getAccessToken() 0 5 2
A setAccessToken() 0 5 2
A getAccessTokenRules() 0 10 2
A setAccessTokenRules() 0 6 3
A onInitAccessToken() 0 6 1
A getStatus() 0 5 2
A setStatus() 0 5 2
A getStatusRules() 0 10 2
A setStatusRules() 0 6 3
A onInitStatusAttribute() 0 8 2
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link http://vistart.name/
9
 * @copyright Copyright (c) 2016 vistart
10
 * @license http://vistart.name/license/
11
 */
12
13
namespace vistart\Models\traits;
14
15
use Yii;
16
use yii\base\ModelEvent;
17
18
/**
19
 * User features concerning identity.
20
 *
21
 * @property-read string $authKey
22
 * @property array $statusRules
23
 * @property array $authKeyRules
24
 * @property array $accessTokenRules
25
 * @version 2.0
26
 * @author vistart <[email protected]>
27
 */
28
trait IdentityTrait
29
{
30
31
    public static $statusActive = 1;
32
    public static $statusInactive = 0;
33
    public $statusAttribute = 'status';
34
    private $statusRules = [];
35
    public $authKeyAttribute = 'auth_key';
36
    private $authKeyRules = [];
37
    public $accessTokenAttribute = 'access_token';
38
    private $accessTokenRules = [];
39
40
    /**
41
     * Finds an identity by the given ID.
42
     * @param string|integer $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
43
     * @return type
44
     */
45
    public static function findIdentity($identity)
46
    {
47
        $self = static::buildNoInitModel();
48
        return static::findOne([$self->idAttribute => $identity]);
49
    }
50
51
    /**
52
     * Finds an identity by the given GUID.
53
     * @param string $guid
54
     * @return type
55
     */
56
    public static function findIdentityByGuid($guid)
57
    {
58
        return static::findOne($guid);
59
    }
60
61
    /**
62
     * Finds an identity by the given token.
63
     * @param string $token
64
     * @param type $type
65
     * @return type
66
     */
67
    public static function findIdentityByAccessToken($token, $type = null)
0 ignored issues
show
Unused Code introduced by
The parameter $type 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...
68
    {
69
        $self = static::buildNoInitModel();
70
        return static::findOne([$self->accessTokenAttribute => $token]);
71
    }
72
73
    /**
74
     * Get auth key.
75
     * @return string|null
76
     */
77
    public function getAuthKey()
78
    {
79
        $authKeyAttribute = $this->authKeyAttribute;
80
        return is_string($authKeyAttribute) ? $this->$authKeyAttribute : null;
81
    }
82
83
    /**
84
     * Set auth key.
85
     * @param string $key
86
     * @return string
87
     */
88
    public function setAuthKey($key)
89
    {
90
        $authKeyAttribute = $this->authKeyAttribute;
91
        return is_string($authKeyAttribute) ? $this->$authKeyAttribute = $key : null;
92
    }
93
94
    /**
95
     * Validate the auth key.
96
     * @param string $authKey
97
     * @return string
98
     */
99
    public function validateAuthKey($authKey)
100
    {
101
        return $this->getAuthKey() === $authKey;
102
    }
103
104
    /**
105
     * Get the rules associated with auth key attribute.
106
     * @return array
107
     */
108
    public function getAuthKeyRules()
109
    {
110
        if (empty($this->authKeyRules)) {
111
            $this->authKeyRules = [
112
                [[$this->authKeyAttribute], 'required'],
113
                [[$this->authKeyAttribute], 'string', 'max' => 40],
114
            ];
115
        }
116
        return $this->authKeyRules;
117
    }
118
119
    /**
120
     * Set the rules associated with auth key attribute.
121
     * @param array $rules
122
     */
123
    public function setAuthKeyRules($rules)
124
    {
125
        if (!empty($rules) && is_array($rules)) {
126
            $this->authKeyRules = $rules;
127
        }
128
    }
129
130
    /**
131
     * Initialize the auth key attribute.
132
     * This method is ONLY used for being triggered by event. DO NOT call,
133
     * override or modify it directly, unless you know the consequences.
134
     * @param ModelEvent $event
135
     */
136 60
    public function onInitAuthKey($event)
137
    {
138 60
        $sender = $event->sender;
139 60
        $authKeyAttribute = $sender->authKeyAttribute;
140 60
        $sender->$authKeyAttribute = sha1(Yii::$app->security->generateRandomString());
141 60
    }
142
143
    /**
144
     * Get access token.
145
     * @return string|null
146
     */
147
    public function getAccessToken()
148
    {
149
        $accessTokenAttribute = $this->accessTokenAttribute;
150
        return is_string($accessTokenAttribute) ? $this->$accessTokenAttribute : null;
151
    }
152
153
    /**
154
     * Set access token.
155
     * @param string $token
156
     * @return string|null
157
     */
158
    public function setAccessToken($token)
159
    {
160
        $accessTokenAttribute = $this->accessTokenAttribute;
161
        return is_string($accessTokenAttribute) ? $this->$accessTokenAttribute = $token : null;
162
    }
163
164
    /**
165
     * Get the rules associated with access token attribute.
166
     * @return array
167
     */
168
    public function getAccessTokenRules()
169
    {
170
        if (empty($this->accessTokenRules)) {
171
            $this->accessTokenRules = [
172
                [[$this->accessTokenAttribute], 'required'],
173
                [[$this->accessTokenAttribute], 'string', 'max' => 40],
174
            ];
175
        }
176
        return $this->accessTokenRules;
177
    }
178
179
    /**
180
     * Set the rules associated with access token attribute.
181
     * @param array $rules
182
     */
183
    public function setAccessTokenRules($rules)
184
    {
185
        if (!empty($rules) && is_array($rules)) {
186
            $this->accessTokenRules = $rules;
187
        }
188
    }
189
190
    /**
191
     * Initialize the access token attribute.
192
     * This method is ONLY used for being triggered by event. DO NOT call,
193
     * override or modify it directly, unless you know the consequences.
194
     * @param ModelEvent $event
195
     */
196 60
    public function onInitAccessToken($event)
197
    {
198 60
        $sender = $event->sender;
199 60
        $accessTokenAttribute = $sender->accessTokenAttribute;
200 60
        $sender->$accessTokenAttribute = sha1(Yii::$app->security->generateRandomString());
201 60
    }
202
203
    /**
204
     * Get status.
205
     * @return integer
206
     */
207
    public function getStatus()
208
    {
209
        $statusAttribute = $this->statusAttribute;
210
        return is_string($statusAttribute) ? $this->$statusAttribute : null;
211
    }
212
213
    /**
214
     * Set status.
215
     * @param integer $status
216
     * @return integer|null
217
     */
218
    public function setStatus($status)
219
    {
220
        $statusAttribute = $this->statusAttribute;
221
        return is_string($statusAttribute) ? $this->$statusAttribute = $status : null;
222
    }
223
224
    /**
225
     * Get the rules associated with status attribute.
226
     * @return array
227
     */
228
    public function getStatusRules()
229
    {
230
        if (empty($this->statusRules)) {
231
            $this->statusRules = [
232
                [[$this->statusAttribute], 'required'],
233
                [[$this->statusAttribute], 'number', 'integerOnly' => true, 'min' => 0],
234
            ];
235
        }
236
        return $this->statusRules;
237
    }
238
239
    /**
240
     * Set the rules associated with status attribute.
241
     * @param array $rules
242
     */
243
    public function setStatusRules($rules)
244
    {
245
        if (!empty($rules) && is_array($rules)) {
246
            $this->statusRules = $rules;
247
        }
248
    }
249
250
    /**
251
     * Initialize the status attribute.
252
     * This method is ONLY used for being triggered by event. DO NOT call,
253
     * override or modify it directly, unless you know the consequences.
254
     * @param ModelEvent $event
255
     */
256 60
    public function onInitStatusAttribute($event)
257
    {
258 60
        $sender = $event->sender;
259 60
        $statusAttribute = $sender->statusAttribute;
260 60
        if (empty($sender->$statusAttribute)) {
261 60
            $sender->$statusAttribute = self::$statusActive;
262 60
        }
263 60
    }
264
}
265