Completed
Push — master ( 2a70d4...e447f0 )
by Jan Philip
01:56
created

DooProvider::getDefaultHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is based on the Generic Provider from the league/oauth2-client library
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that is distributed with the league/oauth2-client library.
7
 */
8
9
namespace JPBernius\OAuth2\Client\Provider;
10
11
use League\OAuth2\Client\Grant\AbstractGrant;
12
use League\OAuth2\Client\Provider\AbstractProvider;
13
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
14
use League\OAuth2\Client\Provider\GenericResourceOwner;
15
use League\OAuth2\Client\Token\AccessToken;
16
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
17
use Psr\Http\Message\ResponseInterface;
18
19
/**
20
 * OAuth 2.0 service provider for interaction with doo.net API using Bearer token authentication.
21
 */
22
class DooProvider extends AbstractProvider
23
{
24
    use BearerAuthorizationTrait;
25
26
    /**
27
     * @var string
28
     */
29
    private $apiUrl = 'https://api.doo.net/v1/';
30
31
    /**
32
     * @var string
33
     */
34
    private $uriAuthorize = 'oauth';
35
36
    /**
37
     * @var string
38
     */
39
    private $uriAccessToken = 'oauth';
40
41
    /**
42
     * @var string
43
     */
44
    private $uriResourceOwnerDetails = 'organizers/current';
45
46
    /**
47
     * @var array|null
48
     */
49
    private $scopes = null;
50
51
    /**
52
     * @var string
53
     */
54
    private $scopeSeparator;
55
56
    /**
57
     * @var string
58
     */
59
    private $responseError = 'error';
60
61
    /**
62
     * @var string
63
     */
64
    private $responseCode;
65
66
    /**
67
     * @var string
68
     */
69
    private $responseResourceOwnerId = 'id';
70
71
    /**
72
     * @param array $options
73
     * @param array $collaborators
74
     */
75 15
    public function __construct(array $options = [], array $collaborators = [])
76
    {
77 15
        $possible   = $this->getConfigurableOptions();
78 15
        $configured = array_intersect_key($options, array_flip($possible));
79
80 15
        foreach ($configured as $key => $value) {
81
            $this->$key = $value;
82 10
        }
83
84
        // Remove all options that are only used locally
85 15
        $options = array_diff_key($options, $configured);
86
87 15
        parent::__construct($options, $collaborators);
88 15
    }
89
90
    /**
91
     * Returns all options that can be configured.
92
     *
93
     * @return array
94
     */
95 15
    protected function getConfigurableOptions()
96
    {
97
        return [
98 15
            'urlAuthorize',
99 10
            'urlAccessToken',
100 10
            'uriResourceOwnerDetails',
101 10
            'scopeSeparator',
102 10
            'responseError',
103 10
            'responseCode',
104 10
            'scopes',
105
            'apiUrl'
106 10
        ];
107
    }
108
109 15
    public function getBaseUri()
110
    {
111 15
        return $this->apiUrl;
112
    }
113
114
    /**
115
     * @inheritdoc
116
     */
117 6
    public function getBaseAuthorizationUrl()
118
    {
119 6
        return $this->getBaseUri() . $this->uriAuthorize;
120 2
    }
121
122
    /**
123
     * @inheritdoc
124
     */
125 9
    public function getBaseAccessTokenUrl(array $params)
126 2
    {
127 9
        return $this->getBaseUri() . $this->uriAccessToken;
128
    }
129
130
    /**
131
     * @inheritdoc
132
     */
133
    public function getResourceOwnerDetailsUrl(AccessToken $token)
134
    {
135
        return $this->getBaseUri() . $this->uriResourceOwnerDetails;
136
    }
137
138
    /**
139
     * @inheritdoc
140
     */
141 3
    public function getDefaultScopes()
142
    {
143 3
        return $this->scopes;
144
    }
145
146
    /**
147
     * @inheritdoc
148
     */
149 3
    protected function getScopeSeparator()
150
    {
151 3
        return $this->scopeSeparator ?: parent::getScopeSeparator();
152
    }
153
154
    /**
155
     * @inheritdoc
156
     */
157 6
    protected function checkResponse(ResponseInterface $response, $data)
158
    {
159 6
        if (!empty($data[$this->responseError])) {
160
            $error = $data[$this->responseError];
161
            $code  = $this->responseCode ? $data[$this->responseCode] : 0;
162
            throw new IdentityProviderException($error, $code, $data);
163
        }
164 6
    }
165
166
    /**
167
     * @inheritdoc
168
     */
169
    protected function createResourceOwner(array $response, AccessToken $token)
170
    {
171
        return new GenericResourceOwner($response, $this->responseResourceOwnerId);
172
    }
173
174
    /**
175
     * @param array $response
176
     * @param AbstractGrant $grant
177
     * @return AccessToken
178
     */
179 6
    protected function createAccessToken(array $response, AbstractGrant $grant)
180
    {
181 6
        if (isset($response['data'])) {
182 3
            $data = $response['data'];
183 3
            return new AccessToken($data);
184
        }
185
186 3
        throw new IdentityProviderException(
187 3
            'Bad Request',
188 3
            400,
189 1
            'Data provided for issuing access_token is not correct'
190 2
        );
191
        
192
    }
193
194
    /**
195
     * Returns the default headers used by this provider.
196
     *
197
     * Typically this is used to set 'Accept' or 'Content-Type' headers.
198
     *
199
     * @return array
200
     */
201 6
    protected function getDefaultHeaders()
202
    {
203
        return [
204
            'Accept' => 'application/hal+json'
205 6
        ];
206
    }
207
208
    /**
209
     * Builds request options used for requesting an access token.
210
     *
211
     * @param  array $params
212
     * @return array
213
     */
214 6
    protected function getAccessTokenOptions(array $params)
215
    {
216 6
        $options = ['headers' => ['content-type' => 'application/json']];
217
218 6
        if ($this->getAccessTokenMethod() === self::METHOD_POST) {
219 6
            $options['body'] = $this->getAccessTokenBody($params);
220 4
        }
221
222 6
        return $options;
223
    }
224
225
    /**
226
     * Returns the request body for requesting an access token.
227
     *
228
     * @param  array $params
229
     * @return string
230
     */
231 6
    protected function getAccessTokenBody(array $params)
232
    {
233 6
        return json_encode($params);
234
    }
235
}
236