Completed
Pull Request — master (#479)
by Andrey
02:39
created

Dropbox   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 93
rs 10
c 0
b 0
f 0

6 Methods

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