Completed
Pull Request — master (#498)
by Dragonqos
04:00
created

DeviantArt::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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