Completed
Push — master ( 7371c1...368126 )
by Alex
03:03
created

StackExchange::getDefaultScopes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace AlexMasterov\OAuth2\Client\Provider;
4
5
use AlexMasterov\OAuth2\Client\Provider\StackExchangeException;
6
use GuzzleHttp\Exception\BadResponseException;
7
use League\OAuth2\Client\{
8
    Provider\AbstractProvider,
9
    Token\AccessToken,
10
    Tool\BearerAuthorizationTrait
11
};
12
use Psr\Http\Message\{
13
    RequestInterface,
14
    ResponseInterface
15
};
16
use UnexpectedValueException;
17
18
class StackExchange extends AbstractProvider
19
{
20
    use BearerAuthorizationTrait;
21
22
    /**
23
     * @var string
24
     */
25
    protected $urlApi = 'https://api.stackexchange.com/2.2/';
26
27
    /**
28
     * @var string
29
     */
30
    protected $urlAuthorize = 'https://stackexchange.com/oauth';
31
32
    /**
33
     * @var string
34
     */
35
    protected $urlAccessToken = 'https://stackexchange.com/oauth/access_token';
36
37
    /**
38
     * @var string
39
     */
40
    protected $scope;
41
42
    /**
43
     * @var string
44
     */
45
    protected $key;
46
47
    /**
48
     * @var string
49
     */
50
    protected $site = 'stackoverflow';
51
52
    /**
53
     * @var string
54
     */
55
    protected $state;
56
57
    /**
58
     * @var string
59
     */
60
    protected $redirectUri;
61
62
    /**
63
     * @inheritDoc
64
     */
65 1
    protected function getDefaultScopes()
66
    {
67 1
        return [];
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73 1
    public function getBaseAuthorizationUrl()
74
    {
75 1
        return $this->urlAuthorize;
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81 1
    public function getBaseAccessTokenUrl(array $params)
82
    {
83 1
        if (empty($params['code'])) {
84 1
            $params['code'] = '';
85
        }
86
87 1
        return $this->urlAccessToken . '?' .
88 1
            $this->buildQueryString($params);
89
    }
90
91
    /**
92
     * @inheritDoc
93
     */
94 1
    public function getResourceOwnerDetailsUrl(AccessToken $token)
95
    {
96 1
        return $this->urlApi . 'me?' .
97 1
            $this->buildQueryString([
98 1
                'access_token' => (string) $token,
99 1
                'key'          => $this->key,
100 1
                'site'         => $this->site,
101
            ]);
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107 1
    protected function getAuthorizationParameters(array $options)
108
    {
109 1
        $options['response_type'] = 'code';
110 1
        $options['client_id'] = $this->clientId;
111
112 1
        if (empty($options['state'])) {
113 1
            $options['state'] = $this->state;
114
        }
115
116 1
        if (empty($options['scope'])) {
117 1
            $options['scope'] = $this->scope;
118
        }
119
120 1
        if (empty($options['redirect_uri'])) {
121 1
            $options['redirect_uri'] = $this->redirectUri;
122
        }
123
124 1
        return $options;
125
    }
126
127
    /**
128
     * @inheritDoc
129
     */
130 1
    protected function parseResponse(ResponseInterface $response)
131
    {
132 1
        $type = $this->getContentType($response);
133
134 1
        if (\strpos($type, 'plain') !== false) {
135 1
            $content = (string) $response->getBody();
136 1
            \parse_str($content, $parsed);
137
138 1
            return $parsed;
139
        }
140
141 1
        return parent::parseResponse($response);
142
    }
143
144
    /**
145
     * @inheritDoc
146
     */
147 1
    protected function checkResponse(ResponseInterface $response, $data)
148
    {
149 1
        if (isset($data['error'])) {
150 1
            throw StackExchangeException::errorResponse($response, $data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by parameter $data on line 147 can also be of type string; however, AlexMasterov\OAuth2\Clie...eption::errorResponse() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
151
        }
152
    }
153
154
    /**
155
     * @inheritDoc
156
     */
157 1
    protected function createResourceOwner(array $response, AccessToken $token)
158
    {
159 1
        return new StackExchangeResourceOwner($response);
160
    }
161
}
162