Completed
Pull Request — master (#498)
by Dragonqos
02:30
created

Dropbox::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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