1 | <?php |
||
17 | abstract class AbstractService extends BaseAbstractService implements ServiceInterface |
||
18 | { |
||
19 | /** @const OAUTH_VERSION */ |
||
20 | const OAUTH_VERSION = 2; |
||
21 | |||
22 | /** @var array */ |
||
23 | protected $scopes; |
||
24 | |||
25 | /** @var null|UriInterface */ |
||
26 | protected $baseApiUri; |
||
27 | |||
28 | /** @var bool */ |
||
29 | protected $stateParameterInAuthUrl; |
||
30 | |||
31 | /** @var string */ |
||
32 | protected $apiVersion; |
||
33 | |||
34 | /** |
||
35 | * @param array $scopes |
||
36 | * @param bool $stateParameterInAutUrl |
||
37 | * @param string $apiVersion |
||
38 | */ |
||
39 | public function __construct( |
||
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | public function getAuthorizationUri(array $additionalParameters = []) |
||
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | public function requestAccessToken($code, $state = null) |
||
125 | |||
126 | /** |
||
127 | * Sends an authenticated API request to the path provided. |
||
128 | * If the path provided is not an absolute URI, the base API Uri (must be passed into constructor) will be used. |
||
129 | * |
||
130 | * @param string|UriInterface $path |
||
131 | * @param string $method HTTP method |
||
132 | * @param array $body request body if applicable |
||
133 | * @param array $extraHeaders Extra headers if applicable. These will override service-specific |
||
134 | * any defaults. |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | public function request($path, $method = 'GET', $body = null, array $extraHeaders = []) |
||
177 | |||
178 | /** |
||
179 | * Accessor to the storage adapter to be able to retrieve tokens. |
||
180 | * |
||
181 | * @return TokenStorageInterface |
||
182 | */ |
||
183 | public function getStorage() |
||
187 | |||
188 | /** |
||
189 | * Refreshes an OAuth2 access token. |
||
190 | * |
||
191 | * @return TokenInterface $token |
||
192 | */ |
||
193 | public function refreshAccessToken(TokenInterface $token) |
||
222 | |||
223 | /** |
||
224 | * Return whether or not the passed scope value is valid. |
||
225 | * |
||
226 | * @param string $scope |
||
227 | * |
||
228 | * @return bool |
||
229 | */ |
||
230 | public function isValidScope($scope) |
||
236 | |||
237 | /** |
||
238 | * Check if the given service need to generate a unique state token to build the authorization url. |
||
239 | * |
||
240 | * @return bool |
||
241 | */ |
||
242 | public function needsStateParameterInAuthUrl() |
||
246 | |||
247 | /** |
||
248 | * Carry the refresh_token for next time getting refreshed |
||
249 | * |
||
250 | * @param $responseBody |
||
251 | * |
||
252 | * @param $refreshToken |
||
253 | * |
||
254 | * @return string |
||
255 | */ |
||
256 | protected function carryRefreshToken($responseBody, $refreshToken) |
||
266 | |||
267 | /** |
||
268 | * Validates the authorization state against a given one. |
||
269 | * |
||
270 | * @param string $state |
||
271 | */ |
||
272 | protected function validateAuthorizationState($state): void |
||
278 | |||
279 | /** |
||
280 | * Generates a random string to be used as state. |
||
281 | * |
||
282 | * @return string |
||
283 | */ |
||
284 | protected function generateAuthorizationState() |
||
288 | |||
289 | /** |
||
290 | * Retrieves the authorization state for the current service. |
||
291 | * |
||
292 | * @return string |
||
293 | */ |
||
294 | protected function retrieveAuthorizationState() |
||
298 | |||
299 | /** |
||
300 | * Stores a given authorization state into the storage. |
||
301 | * |
||
302 | * @param string $state |
||
303 | */ |
||
304 | protected function storeAuthorizationState($state): void |
||
308 | |||
309 | /** |
||
310 | * Return any additional headers always needed for this service implementation's OAuth calls. |
||
311 | * |
||
312 | * @return array |
||
313 | */ |
||
314 | protected function getExtraOAuthHeaders() |
||
318 | |||
319 | /** |
||
320 | * Return any additional headers always needed for this service implementation's API calls. |
||
321 | * |
||
322 | * @return array |
||
323 | */ |
||
324 | protected function getExtraApiHeaders() |
||
328 | |||
329 | /** |
||
330 | * Parses the access token response and returns a TokenInterface. |
||
331 | * |
||
332 | * @abstract |
||
333 | * |
||
334 | * @param string $responseBody |
||
335 | * |
||
336 | * @return TokenInterface |
||
337 | */ |
||
338 | abstract protected function parseAccessTokenResponse($responseBody); |
||
339 | |||
340 | /** |
||
341 | * Returns a class constant from ServiceInterface defining the authorization method used for the API |
||
342 | * Header is the sane default. |
||
343 | * |
||
344 | * @return int |
||
345 | */ |
||
346 | protected function getAuthorizationMethod() |
||
350 | |||
351 | /** |
||
352 | * Returns api version string if is set else retrun empty string. |
||
353 | * |
||
354 | * @return string |
||
355 | */ |
||
356 | protected function getApiVersionString() |
||
360 | |||
361 | /** |
||
362 | * Returns delimiter to scopes in getAuthorizationUri |
||
363 | * For services that do not fully respect the Oauth's RFC, |
||
364 | * and use scopes with commas as delimiter. |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | protected function getScopesDelimiter() |
||
372 | } |
||
373 |