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.

Weibo::call()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 8
rs 10
1
<?php
2
3
namespace anerg\OAuth2\Gateways;
4
5
use anerg\OAuth2\Connector\Gateway;
6
7
class Weibo extends Gateway
8
{
9
10
    const API_BASE            = 'https://api.weibo.com/2/';
11
    protected $AuthorizeURL   = 'https://api.weibo.com/oauth2/authorize';
12
    protected $AccessTokenURL = 'https://api.weibo.com/oauth2/access_token';
13
14
    /**
15
     * 得到跳转地址
16
     */
17
    public function getRedirectUrl()
18
    {
19
        $this->switchAccessTokenURL();
20
        $params = [
21
            'client_id'    => $this->config['app_id'],
22
            'redirect_uri' => $this->config['callback'],
23
            'scope'        => $this->config['scope'],
24
            'state'        => $this->config['state'],
25
            'display'      => $this->display,
26
        ];
27
        return $this->AuthorizeURL . '?' . http_build_query($params);
28
    }
29
30
    /**
31
     * 获取当前授权用户的openid标识
32
     */
33
    public function openid()
34
    {
35
        $this->getToken();
36
37
        if (isset($this->token['openid'])) {
38
            return $this->token['openid'];
39
        } else {
40
            throw new \Exception('没有获取到新浪微博用户ID!');
41
        }
42
    }
43
44
    /**
45
     * 获取格式化后的用户信息
46
     */
47
    public function userinfo()
48
    {
49
        $rsp = $this->userinfoRaw();
50
51
        $userinfo = [
52
            'openid'  => $this->openid(),
53
            'channel' => 'weibo',
54
            'nick'    => $rsp['screen_name'],
55
            'gender'  => $rsp['gender'],
56
            'avatar'  => $rsp['avatar_hd'],
57
        ];
58
        return $userinfo;
59
    }
60
61
    /**
62
     * 获取原始接口返回的用户信息
63
     */
64
    public function userinfoRaw()
65
    {
66
        $this->getToken();
67
68
        return $this->call('users/show.json', ['uid' => $this->openid()]);
69
    }
70
71
    /**
72
     * 发起请求
73
     *
74
     * @param string $api
75
     * @param array $params
76
     * @param string $method
77
     * @return array
78
     */
79
    private function call($api, $params = [], $method = 'GET')
80
    {
81
        $method = strtoupper($method);
82
83
        $params['access_token'] = $this->token['access_token'];
84
85
        $data = $this->$method(self::API_BASE . $api, $params);
86
        return json_decode($data, true);
87
    }
88
89
    /**
90
     * 根据第三方授权页面样式切换跳转地址
91
     *
92
     * @return void
93
     */
94
    private function switchAccessTokenURL()
95
    {
96
        if ($this->display == 'mobile') {
97
            $this->AuthorizeURL = 'https://open.weibo.cn/oauth2/authorize';
98
        }
99
    }
100
101
    /**
102
     * 解析access_token方法请求后的返回值
103
     * @param string $token 获取access_token的方法的返回值
104
     */
105
    protected function parseToken($token)
106
    {
107
        $data = json_decode($token, true);
108
        if (isset($data['access_token'])) {
109
            $data['openid'] = $data['uid'];
110
            unset($data['uid']);
111
            return $data;
112
        } else {
113
            throw new \Exception("获取新浪微博ACCESS_TOKEN出错:{$data['error']}");
114
        }
115
    }
116
}
117