Imgur   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 14
c 0
b 0
f 0
dl 0
loc 27
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A me() 0 15 3
1
<?php
2
/**
3
 * Class Imgur
4
 *
5
 * @created      28.07.2019
6
 * @author       smiley <[email protected]>
7
 * @copyright    2019 smiley
8
 * @license      MIT
9
 */
10
11
namespace chillerlan\OAuth\Providers;
12
13
use chillerlan\HTTP\Utils\MessageUtil;
14
use chillerlan\OAuth\Core\{CSRFToken, OAuth2Provider, ProviderException, TokenRefresh};
15
use Psr\Http\Message\ResponseInterface;
16
use function sprintf;
17
18
/**
19
 * Note: imgur sends an "expires_in" of 315360000 (10 years!) for access tokens,
20
 *       but states in the docs that tokens expire after one month.
21
 *       Either manually saving the expiry with the token to trigger auto refresh
22
 *       or manually refreshing via the refreshAccessToken() method is required.
23
 *
24
 * @see https://apidocs.imgur.com/
25
 */
26
class Imgur extends OAuth2Provider implements CSRFToken, TokenRefresh{
27
28
	protected string  $authURL        = 'https://api.imgur.com/oauth2/authorize';
29
	protected string  $accessTokenURL = 'https://api.imgur.com/oauth2/token';
30
	protected string  $apiURL         = 'https://api.imgur.com';
31
	protected ?string $userRevokeURL  = 'https://imgur.com/account/settings/apps';
32
	protected ?string $apiDocs        = 'https://apidocs.imgur.com';
33
	protected ?string $applicationURL = 'https://api.imgur.com/oauth2/addclient';
34
35
	/**
36
	 * @inheritDoc
37
	 */
38
	public function me():ResponseInterface{
39
		$response = $this->request('/3/account/me');
40
		$status   = $response->getStatusCode();
41
42
		if($status === 200){
43
			return $response;
44
		}
45
46
		$json = MessageUtil::decodeJSON($response);
47
48
		if(isset($json->data, $json->data->error)){
49
			throw new ProviderException($json->data->error);
50
		}
51
52
		throw new ProviderException(sprintf('user info error error HTTP/%s', $status));
53
	}
54
55
}
56