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.

Google::getGender()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 12
rs 9.9666
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
            'email'   => isset($rsp['email']) ? $rsp['email'] : '',
71
            'gender'  => isset($rsp['gender']) ? $this->getGender($rsp['gender']) : 'n',
72
            'avatar'  => $rsp['picture'],
73
        ];
74
        return $userinfo;
75
    }
76
77
    /**
78
     * 获取原始接口返回的用户信息
79
     */
80
    public function userinfoRaw()
81
    {
82
        $this->getToken();
83
84
        return $this->call('oauth2/v2/userinfo');
85
    }
86
87
    /**
88
     * 发起请求
89
     *
90
     * @param string $api
91
     * @param array $params
92
     * @param string $method
93
     * @return array
94
     */
95
    private function call($api, $params = [], $method = 'GET')
96
    {
97
        $method  = strtoupper($method);
98
        $headers = ['Authorization' => 'Bearer ' . $this->token['access_token']];
99
100
        $data = $this->$method(self::API_BASE . $api, $params, $headers);
101
        return json_decode($data, true);
102
    }
103
104
    /**
105
     * 解析access_token方法请求后的返回值
106
     * @param string $result 获取access_token的方法的返回值
107
     */
108
    protected function parseToken($token)
109
    {
110
        $data = json_decode($token, true);
111
        if (isset($data['access_token'])) {
112
            return $data;
113
        } else {
114
            throw new \Exception("获取谷歌 ACCESS_TOKEN 出错:{$token}");
115
        }
116
    }
117
118
    /**
119
     * 格式化性别
120
     *
121
     * @param string $gender
122
     * @return string
123
     */
124
    private function getGender($gender)
125
    {
126
        $return = 'n';
127
        switch ($gender) {
128
            case 'male':
129
                $return = 'm';
130
                break;
131
            case 'female':
132
                $return = 'f';
133
                break;
134
        }
135
        return $return;
136
    }
137
}
138