StackExchange   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 97.37%

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 5
dl 0
loc 144
ccs 37
cts 38
cp 0.9737
rs 10
c 0
b 0
f 0

8 Methods

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