Completed
Push — master ( ffb468...9f4772 )
by vistart
03:55
created

ID   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 28
rs 10
ccs 0
cts 10
cp 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getUser() 0 8 2
A validate() 0 7 2
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\user\models\LoginMethod;
14
15
use rhosocial\user\User;
16
use Yii;
17
18
/**
19
 * Class ID
20
 * @package rhosocial\user\models\LoginMethod
21
 * @version 1.0
22
 * @author vistart <[email protected]>
23
 */
24
class ID implements MethodInterface
25
{
26
    /**
27
     * @param mixed $attribute
28
     * @return User|null
29
     */
30
    public static function getUser($attribute)
31
    {
32
        if (!static::validate($attribute)) {
33
            return null;
34
        }
35
        $userClass = Yii::$app->user->identityClass;
36
        return $userClass::find()->id($attribute)->one();
37
    }
38
39
    /**
40
     * Validate whether the attribute is valid.
41
     * @param mixed $attribute
42
     * @return bool
43
     */
44
    public static function validate($attribute)
45
    {
46
        $userClass = Yii::$app->user->identityClass;
47
        $regex = $userClass::$idRegex;
48
        $result = preg_match($regex, $attribute);
49
        return is_int($result) && $result > 0;
50
    }
51
}
52