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 ( feda6f...a8fc77 )
by Coeus
01:50
created

Facebook::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
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
use anerg\OAuth2\Helper\Str;
7
8
class Facebook extends Gateway
9
{
10
    const API_BASE            = 'https://graph.facebook.com/v3.1/';
11
    protected $AuthorizeURL   = 'https://www.facebook.com/v3.1/dialog/oauth';
12
    protected $AccessTokenURL = 'https://graph.facebook.com/v3.1/oauth/access_token';
13
14
    /**
15
     * 构造函数
16
     *
17
     * @param array $config
18
     */
19
    public function __construct($config = null)
20
    {
21
        parent::__construct($config);
22
        $this->clientParams();
23
    }
24
25
    /**
26
     * 设置客户端请求的参数
27
     *
28
     * @return void
29
     */
30
    private function clientParams()
31
    {
32
        if (isset($this->config['access_token']) && !empty($this->config['access_token'])) {
33
            $this->token['access_token'] = $this->config['access_token'];
34
        }
35
    }
36
37
    /**
38
     * 得到跳转地址
39
     */
40
    public function getRedirectUrl()
41
    {
42
        $params = [
43
            'response_type' => $this->config['response_type'],
44
            'client_id'     => $this->config['app_id'],
45
            'redirect_uri'  => $this->config['callback'],
46
            'scope'         => $this->config['scope'],
47
            'state'         => $this->config['state'] ?: Str::random(),
48
        ];
49
        return $this->AuthorizeURL . '?' . http_build_query($params);
50
    }
51
52
    /**
53
     * 获取当前授权用户的openid标识
54
     */
55
    public function openid()
56
    {
57
        $userinfo = $this->userinfo();
58
        return $userinfo['openid'];
59
    }
60
61
    /**
62
     * 获取格式化后的用户信息
63
     */
64
    public function userinfo()
65
    {
66
        $rsp = $this->userinfoRaw();
67
68
        $userinfo = [
69
            'openid'  => $rsp['id'],
70
            'channel' => 'facebook',
71
            'nick'    => $rsp['name'],
72
            'gender'  => $this->getGender($rsp), //不一定会返回
73
            'avatar'  => $this->getAvatar($rsp),
74
        ];
75
        return $userinfo;
76
    }
77
78
    /**
79
     * 获取原始接口返回的用户信息
80
     */
81
    public function userinfoRaw()
82
    {
83
        $this->getToken();
84
        $fields = isset($this->config['fields']) ? $this->config['fields'] : 'id,name,gender,picture.width(400)';
85
        return $this->call('me', ['access_token' => $this->token['access_token'], 'fields' => $fields], 'GET');
86
    }
87
88
    /**
89
     * 发起请求
90
     *
91
     * @param string $api
92
     * @param array $params
93
     * @param string $method
94
     * @return array
95
     */
96
    private function call($api, $params = [], $method = 'GET')
97
    {
98
        $method  = strtoupper($method);
99
        $request = [
100
            'method' => $method,
101
            'uri'    => self::API_BASE . $api,
102
        ];
103
104
        $data = $this->$method($request['uri'], $params);
105
106
        return json_decode($data, true);
107
    }
108
109
    /**
110
     * Facebook的AccessToken请求参数
111
     * @return array
112
     */
113
    protected function accessTokenParams()
114
    {
115
        $params = [
116
            'client_id'     => $this->config['app_id'],
117
            'client_secret' => $this->config['app_secret'],
118
            'code'          => isset($_GET['code']) ? $_GET['code'] : '',
119
            'redirect_uri'  => $this->config['callback'],
120
        ];
121
        return $params;
122
    }
123
124
    /**
125
     * 解析access_token方法请求后的返回值
126
     * @param string $token 获取access_token的方法的返回值
127
     */
128
    protected function parseToken($token)
129
    {
130
        $token = json_decode($token, true);
131
        if (isset($token['error'])) {
132
            throw new \Exception($token['error']['message']);
133
        }
134
        return $token;
135
    }
136
137
    /**
138
     * 格式化性别
139
     *
140
     * @param array $rsp
141
     * @return string
142
     */
143
    private function getGender($rsp)
144
    {
145
        $gender = isset($rsp['gender']) ? $rsp['gender'] : null;
146
        $return = 'n';
147
        switch ($gender) {
148
            case 'male':
149
                $return = 'm';
150
                break;
151
            case 'female':
152
                $return = 'f';
153
                break;
154
        }
155
        return $return;
156
    }
157
158
    /**
159
     * 获取用户头像
160
     *
161
     * @param array $rsp
162
     * @return string
163
     */
164
    private function getAvatar($rsp)
165
    {
166
        if (isset($rsp['picture']['data']['url'])) {
167
            return $rsp['picture']['data']['url'];
168
        }
169
        return '';
170
    }
171
}
172