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) |
||
219 | |||
220 | /** |
||
221 | * Return whether or not the passed scope value is valid. |
||
222 | * |
||
223 | * @param string $scope |
||
224 | * |
||
225 | * @return bool |
||
226 | */ |
||
227 | public function isValidScope($scope) |
||
233 | |||
234 | /** |
||
235 | * Check if the given service need to generate a unique state token to build the authorization url. |
||
236 | * |
||
237 | * @return bool |
||
238 | */ |
||
239 | public function needsStateParameterInAuthUrl() |
||
243 | |||
244 | /** |
||
245 | * Validates the authorization state against a given one. |
||
246 | * |
||
247 | * @param string $state |
||
248 | */ |
||
249 | protected function validateAuthorizationState($state): void |
||
255 | |||
256 | /** |
||
257 | * Generates a random string to be used as state. |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | protected function generateAuthorizationState() |
||
265 | |||
266 | /** |
||
267 | * Retrieves the authorization state for the current service. |
||
268 | * |
||
269 | * @return string |
||
270 | */ |
||
271 | protected function retrieveAuthorizationState() |
||
275 | |||
276 | /** |
||
277 | * Stores a given authorization state into the storage. |
||
278 | * |
||
279 | * @param string $state |
||
280 | */ |
||
281 | protected function storeAuthorizationState($state): void |
||
285 | |||
286 | /** |
||
287 | * Return any additional headers always needed for this service implementation's OAuth calls. |
||
288 | * |
||
289 | * @return array |
||
290 | */ |
||
291 | protected function getExtraOAuthHeaders() |
||
295 | |||
296 | /** |
||
297 | * Return any additional headers always needed for this service implementation's API calls. |
||
298 | * |
||
299 | * @return array |
||
300 | */ |
||
301 | protected function getExtraApiHeaders() |
||
305 | |||
306 | /** |
||
307 | * Parses the access token response and returns a TokenInterface. |
||
308 | * |
||
309 | * @abstract |
||
310 | * |
||
311 | * @param string $responseBody |
||
312 | * |
||
313 | * @return TokenInterface |
||
314 | */ |
||
315 | abstract protected function parseAccessTokenResponse($responseBody); |
||
316 | |||
317 | /** |
||
318 | * Returns a class constant from ServiceInterface defining the authorization method used for the API |
||
319 | * Header is the sane default. |
||
320 | * |
||
321 | * @return int |
||
322 | */ |
||
323 | protected function getAuthorizationMethod() |
||
327 | |||
328 | /** |
||
329 | * Returns api version string if is set else retrun empty string. |
||
330 | * |
||
331 | * @return string |
||
332 | */ |
||
333 | protected function getApiVersionString() |
||
337 | |||
338 | /** |
||
339 | * Returns delimiter to scopes in getAuthorizationUri |
||
340 | * For services that do not fully respect the Oauth's RFC, |
||
341 | * and use scopes with commas as delimiter. |
||
342 | * |
||
343 | * @return string |
||
344 | */ |
||
345 | protected function getScopesDelimiter() |
||
349 | } |
||
350 |