DeviantArt::getAccessTokenEndpoint()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
c 1
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\Common\Consumer\CredentialsInterface;
6
use OAuth\Common\Http\Client\ClientInterface;
7
use OAuth\Common\Http\Exception\TokenResponseException;
8
use OAuth\Common\Http\Uri\Uri;
9
use OAuth\Common\Http\Uri\UriInterface;
10
use OAuth\Common\Storage\TokenStorageInterface;
11
use OAuth\OAuth2\Token\StdOAuth2Token;
12
13
class DeviantArt extends AbstractService
14
{
15
    /**
16
     * DeviantArt www url - used to build dialog urls.
17
     */
18
    const WWW_URL = 'https://www.deviantart.com/';
19
20
    /**
21
     * Defined scopes.
22
     *
23
     * If you don't think this is scary you should not be allowed on the web at all
24
     *
25
     * @see https://www.deviantart.com/developers/authentication
26
     * @see https://www.deviantart.com/developers/http/v1/20150217
27
     */
28
    const SCOPE_FEED = 'feed';
29
    const SCOPE_BROWSE = 'browse';
30
    const SCOPE_COMMENT = 'comment.post';
31
    const SCOPE_STASH = 'stash';
32
    const SCOPE_USER = 'user';
33
    const SCOPE_USERMANAGE = 'user.manage';
34
35
    public function __construct(
36
        CredentialsInterface $credentials,
37
        ClientInterface $httpClient,
38
        TokenStorageInterface $storage,
39
        $scopes = [],
40
        ?UriInterface $baseApiUri = null
41
    ) {
42
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
43
44
        if (null === $baseApiUri) {
45
            $this->baseApiUri = new Uri('https://www.deviantart.com/api/v1/oauth2/');
46
        }
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getAuthorizationEndpoint()
53
    {
54
        return new Uri('https://www.deviantart.com/oauth2/authorize');
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getAccessTokenEndpoint()
61
    {
62
        return new Uri('https://www.deviantart.com/oauth2/token');
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    protected function parseAccessTokenResponse($responseBody)
69
    {
70
        $data = json_decode($responseBody, true);
71
72
        if (null === $data || !is_array($data)) {
73
            throw new TokenResponseException('Unable to parse response.');
74
        } elseif (isset($data['error'])) {
75
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
76
        }
77
78
        $token = new StdOAuth2Token();
79
        $token->setAccessToken($data['access_token']);
80
81
        if (isset($data['expires_in'])) {
82
            $token->setLifeTime($data['expires_in']);
83
        }
84
85
        if (isset($data['refresh_token'])) {
86
            $token->setRefreshToken($data['refresh_token']);
87
            unset($data['refresh_token']);
88
        }
89
90
        unset($data['access_token'], $data['expires_in']);
91
92
        $token->setExtraParams($data);
93
94
        return $token;
95
    }
96
}
97