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

ID::getUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
ccs 0
cts 5
cp 0
cc 2
eloc 5
nc 2
nop 1
crap 6
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