GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( a77882...7abee1 )
by Coeus
02:27
created

Google::clientParams()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace anerg\OAuth2\Gateways;
4
5
use anerg\OAuth2\Connector\Gateway;
6
7
class Google extends Gateway
8
{
9
    const API_BASE            = 'https://www.googleapis.com/';
10
    const AUTHORIZE_URL       = 'https://accounts.google.com/o/oauth2/v2/auth';
11
    protected $AccessTokenURL = 'https://www.googleapis.com/oauth2/v4/token';
12
13
    /**
14
     * 构造函数
15
     *
16
     * @param array $config
17
     */
18
    public function __construct($config = null)
19
    {
20
        parent::__construct($config);
21
        $this->clientParams();
22
    }
23
24
    /**
25
     * 设置客户端请求的参数
26
     *
27
     * @return void
28
     */
29
    private function clientParams()
30
    {
31
        if (isset($this->config['access_token']) && !empty($this->config['access_token'])) {
32
            $this->token['access_token'] = $this->config['access_token'];
33
        }
34
    }
35
36
    /**
37
     * 得到跳转地址
38
     */
39
    public function getRedirectUrl()
40
    {
41
        $params = [
42
            'client_id'     => $this->config['app_id'],
43
            'redirect_uri'  => $this->config['callback'],
44
            'response_type' => $this->config['response_type'],
45
            'scope'         => $this->config['scope'],
46
            'state'         => $this->config['state'],
47
        ];
48
        return self::AUTHORIZE_URL . '?' . http_build_query($params);
49
    }
50
51
    /**
52
     * 获取当前授权用户的openid标识
53
     */
54
    public function openid()
55
    {
56
        $userinfo = $this->userinfoRaw();
57
        return $userinfo['id'];
58
    }
59
60
    /**
61
     * 获取格式化后的用户信息
62
     */
63
    public function userinfo()
64
    {
65
        $rsp      = $this->userinfoRaw();
66
        $userinfo = [
67
            'openid'  => $rsp['id'],
68
            'channel' => 'google',
69
            'nick'    => $rsp['name'],
70
            'gender'  => $this->getGender($rsp['gender']),
71
            'avatar'  => $rsp['picture'],
72
        ];
73
        return $userinfo;
74
    }
75
76
    /**
77
     * 获取原始接口返回的用户信息
78
     */
79
    public function userinfoRaw()
80
    {
81
        $this->getToken();
82
83
        return $this->call('oauth2/v2/userinfo');
84
    }
85
86
    /**
87
     * 发起请求
88
     *
89
     * @param string $api
90
     * @param array $params
91
     * @param string $method
92
     * @return array
93
     */
94
    private function call($api, $params = [], $method = 'GET')
95
    {
96
        $method  = strtoupper($method);
97
        $headers = ['Authorization' => 'Bearer ' . $this->token['access_token']];
98
99
        $data = $this->$method(self::API_BASE . $api, $params, $headers);
100
        return json_decode($data, true);
101
    }
102
103
    /**
104
     * 解析access_token方法请求后的返回值
105
     * @param string $result 获取access_token的方法的返回值
106
     */
107
    protected function parseToken($token)
108
    {
109
        $data = json_decode($token, true);
110
        if (isset($data['access_token'])) {
111
            return $data;
112
        } else {
113
            throw new \Exception("获取谷歌 ACCESS_TOKEN 出错:{$token}");
114
        }
115
    }
116
117
    /**
118
     * 格式化性别
119
     *
120
     * @param string $gender
121
     * @return string
122
     */
123
    private function getGender($gender)
124
    {
125
        $return = 'n';
126
        switch ($gender) {
127
            case 'male':
128
                $return = 'm';
129
                break;
130
            case 'female':
131
                $return = 'f';
132
                break;
133
        }
134
        return $return;
135
    }
136
}
137