Passed
Push — master ( e46bc3...5ffc5f )
by vistart
08:40
created

IdentityTrait   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.44%

Importance

Changes 0
Metric Value
wmc 45
lcom 1
cbo 2
dl 0
loc 243
ccs 76
cts 78
cp 0.9744
rs 8.3673
c 0
b 0
f 0

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 3
A setAuthKey() 0 5 3
A validateAuthKey() 0 4 1
A onInitAuthKey() 0 6 1
A getAccessToken() 0 5 3
A setAccessToken() 0 5 3
A getAuthKeyRules() 0 10 2
A setAuthKeyRules() 0 6 3
A getAccessTokenRules() 0 13 4
A setAccessTokenRules() 0 6 3
A onInitAccessToken() 0 6 1
A getStatus() 0 5 3
A setStatus() 0 5 3
A getStatusRules() 0 13 4
A setStatusRules() 0 6 3
A onInitStatusAttribute() 0 8 2

How to fix   Complexity   

Complex Class

Complex classes like IdentityTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use IdentityTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\base\models\traits;
14
15
use Yii;
16
use yii\base\ModelEvent;
17
18
/**
19
 * User features concerning identity.
20
 *
21
 * @property string $accessToken
22
 * @property array $accessTokenRules
23
 * @property string $authKey
24
 * @property array $authKeyRules
25
 * @property integer $status
26
 * @property array $statusRules
27
 * @version 1.0
28
 * @author vistart <[email protected]>
29
 */
30
trait IdentityTrait
31
{
32
33
    public static $statusActive = 1;
34
    public static $statusInactive = 0;
35
    public $statusAttribute = 'status';
36
    private $statusRules = [];
37
    public $authKeyAttribute = 'auth_key';
38
    private $authKeyRules = [];
39
    public $accessTokenAttribute = 'access_token';
40
    private $accessTokenRules = [];
41
42
    /**
43
     * Finds an identity by the given ID.
44
     * @param string|integer $identity
45
     * @return static
46
     */
47 3
    public static function findIdentity($identity)
48
    {
49 3
        $self = static::buildNoInitModel();
50 3
        return static::findOne([$self->idAttribute => $identity]);
51
    }
52
53
    /**
54
     * Finds an identity by the given GUID.
55
     * @param string $guid
56
     * @return static
57
     */
58 3
    public static function findIdentityByGuid($guid)
59
    {
60 3
        return static::findOne((string)$guid);
61
    }
62
63
    /**
64
     * Finds an identity by the given token.
65
     * @param string $token
66
     * @param mixed $type
67
     * @return static
68
     */
69 3
    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...
70
    {
71 3
        $self = static::buildNoInitModel();
72 3
        return static::findOne([$self->accessTokenAttribute => $token]);
73
    }
74
75
    /**
76
     * Get auth key.
77
     * @return string|null
78
     */
79 5
    public function getAuthKey()
80
    {
81 5
        $authKeyAttribute = $this->authKeyAttribute;
82 5
        return (is_string($authKeyAttribute) && !empty($authKeyAttribute)) ? $this->$authKeyAttribute : null;
83
    }
84
85
    /**
86
     * Set auth key.
87
     * @param string $key
88
     * @return string
89
     */
90 5
    public function setAuthKey($key)
91
    {
92 5
        $authKeyAttribute = $this->authKeyAttribute;
93 5
        return (is_string($authKeyAttribute) && !empty($authKeyAttribute)) ? $this->$authKeyAttribute = $key : null;
94
    }
95
96
    /**
97
     * Validate the auth key.
98
     * @param string $authKey
99
     * @return string
100
     */
101 3
    public function validateAuthKey($authKey)
102
    {
103 3
        return $this->getAuthKey() === $authKey;
104
    }
105
106
    /**
107
     * Get the rules associated with auth key attribute.
108
     * @return array
109
     */
110 288
    public function getAuthKeyRules()
111
    {
112 288
        if (empty($this->authKeyRules)) {
113 286
            $this->authKeyRules = [
114 286
                [[$this->authKeyAttribute], 'required'],
115 286
                [[$this->authKeyAttribute], 'string', 'max' => 40],
116
            ];
117
        }
118 288
        return $this->authKeyRules;
119
    }
120
121
    /**
122
     * Set the rules associated with auth key attribute.
123
     * @param array $rules
124
     */
125 2
    public function setAuthKeyRules($rules)
126
    {
127 2
        if (!empty($rules) && is_array($rules)) {
128 2
            $this->authKeyRules = $rules;
129
        }
130 2
    }
131
132
    /**
133
     * Initialize the auth key attribute.
134
     * This method is ONLY used for being triggered by event. DO NOT call,
135
     * override or modify it directly, unless you know the consequences.
136
     * @param ModelEvent $event
137
     */
138 300
    public function onInitAuthKey($event)
139
    {
140 300
        $sender = $event->sender;
141 300
        $authKeyAttribute = $sender->authKeyAttribute;
142 300
        $sender->$authKeyAttribute = sha1(Yii::$app->security->generateRandomString());
143 300
    }
144
145
    /**
146
     * Get access token.
147
     * @return string|null
148
     */
149 8
    public function getAccessToken()
150
    {
151 8
        $accessTokenAttribute = $this->accessTokenAttribute;
152 8
        return (is_string($accessTokenAttribute) && !empty($accessTokenAttribute)) ? $this->$accessTokenAttribute : null;
153
    }
154
155
    /**
156
     * Set access token.
157
     * @param string $token
158
     * @return string|null
159
     */
160 5
    public function setAccessToken($token)
161
    {
162 5
        $accessTokenAttribute = $this->accessTokenAttribute;
163 5
        return (is_string($accessTokenAttribute) && !empty($accessTokenAttribute)) ? $this->$accessTokenAttribute = $token : null;
164
    }
165
166
    /**
167
     * Get the rules associated with access token attribute.
168
     * @return array
169
     */
170 288
    public function getAccessTokenRules()
171
    {
172 288
        if (!is_string($this->accessTokenAttribute) || empty($this->accessTokenAttribute)) {
173
            return [];
174
        }
175 288
        if (empty($this->accessTokenRules)) {
176 287
            $this->accessTokenRules = [
177 287
                [[$this->accessTokenAttribute], 'required'],
178 287
                [[$this->accessTokenAttribute], 'string', 'max' => 40],
179
            ];
180
        }
181 288
        return $this->accessTokenRules;
182
    }
183
184
    /**
185
     * Set the rules associated with access token attribute.
186
     * @param array $rules
187
     */
188 2
    public function setAccessTokenRules($rules)
189
    {
190 2
        if (!empty($rules) && is_array($rules)) {
191 2
            $this->accessTokenRules = $rules;
192
        }
193 2
    }
194
195
    /**
196
     * Initialize the access token attribute.
197
     * This method is ONLY used for being triggered by event. DO NOT call,
198
     * override or modify it directly, unless you know the consequences.
199
     * @param ModelEvent $event
200
     */
201 300
    public function onInitAccessToken($event)
202
    {
203 300
        $sender = $event->sender;
204 300
        $accessTokenAttribute = $sender->accessTokenAttribute;
205 300
        $sender->$accessTokenAttribute = sha1(Yii::$app->security->generateRandomString());
206 300
    }
207
208
    /**
209
     * Get status.
210
     * @return integer
211
     */
212 3
    public function getStatus()
213
    {
214 3
        $statusAttribute = $this->statusAttribute;
215 3
        return (is_string($statusAttribute) && !empty($statusAttribute)) ? $this->$statusAttribute : null;
216
    }
217
218
    /**
219
     * Set status.
220
     * @param integer $status
221
     * @return integer|null
222
     */
223 1
    public function setStatus($status)
224
    {
225 1
        $statusAttribute = $this->statusAttribute;
226 1
        return (is_string($statusAttribute) && !empty($statusAttribute)) ? $this->$statusAttribute = $status : null;
227
    }
228
229
    /**
230
     * Get the rules associated with status attribute.
231
     * @return array
232
     */
233 288
    public function getStatusRules()
234
    {
235 288
        if (!is_string($this->statusAttribute) || empty($this->statusAttribute)) {
236
            return [];
237
        }
238 288
        if (empty($this->statusRules)) {
239 287
            $this->statusRules = [
240 287
                [[$this->statusAttribute], 'required'],
241 287
                [[$this->statusAttribute], 'number', 'integerOnly' => true, 'min' => 0],
242
            ];
243
        }
244 288
        return $this->statusRules;
245
    }
246
247
    /**
248
     * Set the rules associated with status attribute.
249
     * @param array $rules
250
     */
251 1
    public function setStatusRules($rules)
252
    {
253 1
        if (!empty($rules) && is_array($rules)) {
254 1
            $this->statusRules = $rules;
255
        }
256 1
    }
257
258
    /**
259
     * Initialize the status attribute.
260
     * This method is ONLY used for being triggered by event. DO NOT call,
261
     * override or modify it directly, unless you know the consequences.
262
     * @param ModelEvent $event
263
     */
264 300
    public function onInitStatusAttribute($event)
265
    {
266 300
        $sender = $event->sender;
267 300
        $statusAttribute = $sender->statusAttribute;
268 300
        if (empty($sender->$statusAttribute)) {
269 300
            $sender->$statusAttribute = self::$statusActive;
270
        }
271 300
    }
272
}
273