Completed
Push — master ( 5e7dd6...051933 )
by ma
28s queued 27s
created

Gitee::userInfo()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 15
rs 9.8666
cc 3
nc 4
nop 0
1
<?php
2
/**
3
 * Gitee
4
 * api接口文档
5
 */
6
namespace ZhuoSheGuaMo\YxOAuth\Gateways;
7
8
use ZhuoSheGuaMo\YxOAuth\Connector\Gateway;
0 ignored issues
show
Bug introduced by
The type ZhuoSheGuaMo\YxOAuth\Connector\Gateway was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use ZhuoSheGuaMo\YxOAuth\Plug\ConstCode;
0 ignored issues
show
Bug introduced by
The type ZhuoSheGuaMo\YxOAuth\Plug\ConstCode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
/**
12
 * Class Gitee
13
 */
14
class Gitee extends Gateway
15
{
16
    protected $AuthorizeURL   = 'https://gitee.com/oauth/authorize';
17
    protected $AccessTokenURL = 'https://gitee.com/oauth/token/';
18
    protected $UserInfoURL = 'https://gitee.com/api/v5/user';
19
20
    /**
21
     * Description:  得到跳转地址
22
     * Updater:
23
     * @return string
24
     */
25
    public function getRedirectUrl()
26
    {
27
        //存储state
28
        $this->saveState();
29
        //登录参数
30
        $params = [
31
            'response_type' => $this->config['response_type'],
32
            'client_id'     => $this->config['app_id'],
33
            'redirect_uri'  => $this->config['callback'],
34
            'state'         => $this->config['state'],
35
            'scope'         => $this->config['scope'],
36
        ];
37
        return $this->AuthorizeURL . '?' . http_build_query($params);
38
    }
39
40
    /**
41
     * Description:  获取格式化后的用户信息
42
     * @return array
43
     * @throws \Exception
44
     */
45
    public function userInfo()
46
    {
47
        $result = $this->getUserInfo();
48
        $userInfo = [
49
            'open_id' => isset($result['id']) ? $result['id'] : '',
50
            'union_id'=> isset($result['login']) ? $result['login'] : '',
51
            'channel' => ConstCode::TYPE_GITEE,
52
            'nickname'=> $result['name'],
53
            'gender'  => ConstCode::GENDER,
54
            'avatar'  => $result['avatar_url'],
55
            'birthday'=> '',
56
            'access_token'=> $this->token['access_token'] ?? '',
57
            'native'=> $result,
58
        ];
59
        return $userInfo;
60
    }
61
62
    /**
63
     * Description:  获取原始接口返回的用户信息
64
     * @return array
65
     * @throws \Exception
66
     */
67
    public function getUserInfo()
68
    {
69
        /** 获取用户信息 */
70
        $this->openid();
71
72
//        $headers = ['Authorization: Bearer '.$this->token['access_token']];
73
        $params = [
74
            'access_token'=>$this->token['access_token'],
75
        ];
76
        $data = $this->get($this->UserInfoURL,$params);
77
        return json_decode($data, true);
78
    }
79
80
    /**
81
     * Description:  获取当前授权用户的openid标识
82
     * @return mixed
83
     * @throws \Exception
84
     */
85
    public function openid()
86
    {
87
        $this->getToken();
88
    }
89
90
91
    /**
92
     * Description:  获取AccessToken
93
     */
94
    protected function getToken(){
95
        if (empty($this->token)) {
96
            /** 验证state参数 */
97
            $this->CheckState();
98
99
            /** 获取参数 */
100
            $params = $this->accessTokenParams();
101
102
            /** 获取access_token */
103
            $this->AccessTokenURL = $this->AccessTokenURL . '?' . http_build_query($params);
104
            $token =  $this->post($this->AccessTokenURL);
105
            /** 解析token值(子类实现此方法) */
106
            $this->token = $this->parseToken($token);
0 ignored issues
show
Bug Best Practice introduced by
The property token does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
107
        }
108
    }
109
110
    /**
111
     * Description:  解析access_token方法请求后的返回值
112
     * @param $token
113
     * @return mixed
114
     * @throws \Exception
115
     */
116
    protected function parseToken($token)
117
    {
118
        $data = json_decode($token, true);
119
        if (isset($data['access_token'])) {
120
            return $data;
121
        } else {
122
            throw new \Exception("获取Gitee ACCESS_TOKEN出错:{$data['error']}");
123
        }
124
    }
125
126
}