OauthClients   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Test Coverage

Coverage 89.19%

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 9
dl 0
loc 116
ccs 33
cts 37
cp 0.8919
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A tableName() 0 4 1
A rules() 0 17 1
A beforeValidate() 0 8 2
A attributeLabels() 0 14 1
A fields() 0 13 1
A extraFields() 0 6 1
A behaviors() 0 6 1
A beforeSave() 0 14 3
A find() 0 4 1
A getUserCount() 0 4 1
1
<?php
2
3
namespace zacksleo\yii2\oauth2\common\models;
4
5
use yii;
6
use zacksleo\yii2\oauth2\common\behaviors\OauthClientsDeleteBehavior;
7
use Ramsey\Uuid\Uuid;
8
use filsh\yii2\oauth2server\models\OauthClients as Clients;
9
use zacksleo\yii2\oauth2\common\queries\OauthClientsQuery;
10
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
11
12
/**
13
 * This is the model class for table "{{%oauth_clients}}".
14
 *
15
 * @inheritdoc
16
 * @property string $client_name
17
 * @property string $client_icon
18
 * @property boolean $one_token_per_user
19
 */
20
class OauthClients extends Clients
21
{
22
23
    /**
24
     * @inheritdoc
25
     */
26 6
    public static function tableName()
27
    {
28 6
        return '{{%oauth_clients}}';
29
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34 1
    public function rules()
35
    {
36
        return [
37 1
            ['client_name', 'required'],
38
            ['redirect_uri', 'default', 'value' => ''],
39
            [['grant_types'], 'required'],
40
            [['user_id'], 'integer'],
41
            [['redirect_uri'], 'string', 'max' => 1000],
42
            [['scope'], 'string', 'max' => 2000],
43
            ['user_id', 'default', 'value' => 1],
44
            ['client_id', 'string', 'max' => 36],
45
            ['client_secret', 'string', 'max' => 36],
46
            ['client_icon', 'safe'],
47
            ['one_token_per_user', 'boolean'],
48
            [['client_name', 'user_id'], 'unique', 'targetAttribute' => ['client_name', 'user_id']],
49
        ];
50
    }
51
52 1
    public function beforeValidate()
53
    {
54 1
        $model = Yii::$app->request->post('OauthClients');
55 1
        if (isset($model['grant_types'])) {
56
            $this->grant_types = implode(' ', $model['grant_types']);
57
        }
58 1
        return parent::beforeValidate();
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64 1
    public function attributeLabels()
65
    {
66
        return [
67 1
            'client_id' => '应用ID',
68
            'client_name' => '应用名称',
69
            'client_secret' => '应用密钥',
70
            'redirect_uri' => '回跳URL',
71
            'grant_types' => '授权类型',
72
            'scope' => '权限范围',
73
            'user_id' => '用户',
74
            'client_icon' => '图标',
75
            'one_token_per_user' => '单设备登录',
76
        ];
77
    }
78
79 1
    public function fields()
80
    {
81 1
        $fields = parent::fields();
82
        unset(
83 1
            $fields['redirect_uri'],
84 1
            $fields['grant_types'],
85 1
            $fields['scope'],
86 1
            $fields['user_id'],
87 1
            $fields['client_id'],
88 1
            $fields['client_secret']
89
        );
90 1
        return $fields;
91
    }
92
93 1
    public function extraFields()
94
    {
95
        return [
96 1
            'client_id', 'client_secret', 'userCount'
97
        ];
98
    }
99
100 7
    public function behaviors()
101
    {
102
        return [
103 7
            OauthClientsDeleteBehavior::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
104
        ];
105
    }
106
107 1
    public function beforeSave($insert)
108
    {
109 1
        if ($this->isNewRecord) {
110
            try {
111 1
                $uuid = Uuid::uuid5(Uuid::NAMESPACE_DNS, $this->client_name . $this->user_id);
112 1
                $this->client_id = $uuid->toString();
113
            } catch (UnsatisfiedDependencyException $e) {
114
                $this->addError('app_name', $e->getMessage());
115
                return false;
116
            }
117 1
            $this->client_secret = Yii::$app->security->generateRandomString();
118
        }
119 1
        return parent::beforeSave($insert);
120
    }
121
122
    /**
123
     * @inheritdoc
124
     * @return OauthClientsQuery the active query used by this AR class.
125
     */
126 6
    public static function find()
127
    {
128 6
        return new OauthClientsQuery(get_called_class());
129
    }
130
131 1
    public function getUserCount()
132
    {
133 1
        return $this->hasMany(OauthClientsSignupUser::className(), ['client_id' => 'client_id'])->count();
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
134
    }
135
}
136