Dropbox   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 30
dl 0
loc 91
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAccessTokenEndpoint() 0 3 1
A __construct() 0 11 2
A getAuthorizationUri() 0 20 2
A getAuthorizationMethod() 0 3 1
A getAuthorizationEndpoint() 0 3 1
A parseAccessTokenResponse() 0 23 5
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
/**
14
 * Dropbox service.
15
 *
16
 * @author Flávio Heleno <[email protected]>
17
 *
18
 * @see https://www.dropbox.com/developers/core/docs
19
 */
20
class Dropbox extends AbstractService
21
{
22
    public function __construct(
23
        CredentialsInterface $credentials,
24
        ClientInterface $httpClient,
25
        TokenStorageInterface $storage,
26
        $scopes = [],
27
        ?UriInterface $baseApiUri = null
28
    ) {
29
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
30
31
        if (null === $baseApiUri) {
32
            $this->baseApiUri = new Uri('https://api.dropbox.com/1/');
33
        }
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getAuthorizationUri(array $additionalParameters = [])
40
    {
41
        $parameters = array_merge(
42
            $additionalParameters,
43
            [
44
                'client_id' => $this->credentials->getConsumerId(),
45
                'redirect_uri' => $this->credentials->getCallbackUrl(),
46
                'response_type' => 'code',
47
            ]
48
        );
49
50
        $parameters['scope'] = implode(' ', $this->scopes);
51
52
        // Build the url
53
        $url = clone $this->getAuthorizationEndpoint();
54
        foreach ($parameters as $key => $val) {
55
            $url->addToQuery($key, $val);
56
        }
57
58
        return $url;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getAuthorizationEndpoint()
65
    {
66
        return new Uri('https://www.dropbox.com/1/oauth2/authorize');
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getAccessTokenEndpoint()
73
    {
74
        return new Uri('https://api.dropbox.com/1/oauth2/token');
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    protected function getAuthorizationMethod()
81
    {
82
        return static::AUTHORIZATION_METHOD_QUERY_STRING;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    protected function parseAccessTokenResponse($responseBody)
89
    {
90
        $data = json_decode($responseBody, true);
91
92
        if (null === $data || !is_array($data)) {
93
            throw new TokenResponseException('Unable to parse response.');
94
        } elseif (isset($data['error'])) {
95
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
96
        }
97
98
        $token = new StdOAuth2Token();
99
        $token->setAccessToken($data['access_token']);
100
101
        if (isset($data['refresh_token'])) {
102
            $token->setRefreshToken($data['refresh_token']);
103
            unset($data['refresh_token']);
104
        }
105
106
        unset($data['access_token']);
107
108
        $token->setExtraParams($data);
109
110
        return $token;
111
    }
112
}
113