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.

Gateway::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 4
b 0
f 0
nc 2
nop 1
dl 0
loc 17
rs 9.8666
1
<?php
2
namespace anerg\OAuth2\Connector;
3
4
use anerg\OAuth2\Connector\GatewayInterface;
5
6
/**
7
 * 所有第三方登录必须继承的抽象类
8
 */
9
abstract class Gateway implements GatewayInterface
10
{
11
    /**
12
     * 配置参数
13
     * @var array
14
     */
15
    protected $config;
16
17
    /**
18
     * 当前时间戳
19
     * @var int
20
     */
21
    protected $timestamp;
22
23
    /**
24
     * 默认第三方授权页面样式
25
     * @var string
26
     */
27
    protected $display = 'default';
28
29
    /**
30
     * 第三方Token信息
31
     * @var array
32
     */
33
    protected $token = null;
34
35
    /**
36
     * 是否验证回跳地址中的state参数
37
     * @var boolean
38
     */
39
    protected $checkState = false;
40
41
    public function __construct($config = null)
42
    {
43
        if (!$config) {
44
            throw new \Exception('传入的配置不能为空');
45
        }
46
        //默认参数
47
        $_config = [
48
            'app_id'        => '',
49
            'app_secret'    => '',
50
            'callback'      => '',
51
            'response_type' => 'code',
52
            'grant_type'    => 'authorization_code',
53
            'proxy'         => '',
54
            'state'         => '',
55
        ];
56
        $this->config    = array_merge($_config, $config);
57
        $this->timestamp = time();
58
    }
59
60
    /**
61
     * 设置授权页面样式
62
     *
63
     * @param string $display
64
     * @return self
65
     */
66
    public function setDisplay($display)
67
    {
68
        $this->display = $display;
69
        return $this;
70
    }
71
72
    /**
73
     * 强制验证回跳地址中的state参数
74
     *
75
     * @return self
76
     */
77
    public function mustCheckState()
78
    {
79
        $this->checkState = true;
80
        return $this;
81
    }
82
83
    /**
84
     * 执行GET请求操作
85
     *
86
     * @param string $url
87
     * @param array $params
88
     * @param array $headers
89
     * @return string
90
     */
91
    protected function GET($url, $params = [], $headers = [])
92
    {
93
        $client   = new \GuzzleHttp\Client();
94
        $response = $client->request('GET', $url, ['proxy' => $this->config['proxy'], 'headers' => $headers, 'query' => $params]);
95
        return $response->getBody()->getContents();
96
    }
97
98
    /**
99
     * 执行POST请求操作
100
     *
101
     * @param string $url
102
     * @param array $params
103
     * @param array $headers
104
     * @return string
105
     */
106
    protected function POST($url, $params = [], $headers = [])
107
    {
108
        $client   = new \GuzzleHttp\Client();
109
        $response = $client->request('POST', $url, ['proxy' => $this->config['proxy'], 'headers' => $headers, 'form_params' => $params, 'http_errors' => false]);
110
        return $response->getBody()->getContents();
111
    }
112
113
    /**
114
     * 默认的AccessToken请求参数
115
     * @return array
116
     */
117
    protected function accessTokenParams()
118
    {
119
        $params = [
120
            'client_id'     => $this->config['app_id'],
121
            'client_secret' => $this->config['app_secret'],
122
            'grant_type'    => $this->config['grant_type'],
123
            'code'          => isset($_REQUEST['code']) ? $_REQUEST['code'] : '',
124
            'redirect_uri'  => $this->config['callback'],
125
        ];
126
        return $params;
127
    }
128
129
    /**
130
     * 获取AccessToken
131
     *
132
     * @return string
133
     */
134
    protected function getAccessToken()
135
    {
136
        if ($this->checkState === true) {
137
            if (!isset($_GET['state']) || $_GET['state'] != $this->config['state']) {
138
                throw new \Exception('传递的STATE参数不匹配!');
139
            }
140
        }
141
        $params = $this->accessTokenParams();
142
        return $this->POST($this->AccessTokenURL, $params);
143
    }
144
145
    /**
146
     * 获取token信息
147
     *
148
     * @return void
149
     */
150
    protected function getToken()
151
    {
152
        if (empty($this->token)) {
153
            $token = $this->getAccessToken();
154
            /** @scrutinizer ignore-call */
155
            $this->token = $this->parseToken($token);
156
        }
157
    }
158
}
159