Completed
Pull Request — master (#479)
by Andrey
03:26
created

DeviantArt   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 0
cbo 4
dl 0
loc 86
rs 10
c 1
b 0
f 0

4 Methods

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