Completed
Push — master ( c67749...e01f26 )
by Alex
01:55
created

StackExchange::getAuthorizationParameters()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

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