Completed
Pull Request — master (#519)
by
unknown
03:25
created

Docusign   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 0
loc 105
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 2
A getAuthorizationUri() 0 21 2
A getAuthorizationEndpoint() 0 4 1
A getAccessTokenEndpoint() 0 4 1
A getAuthorizationMethod() 0 4 1
B parseAccessTokenResponse() 0 28 6
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
 * Docusign service.
15
 *
16
 * @author Naveen Gopala <[email protected]>
17
 * @link https://images-na.ssl-images-docusign.com/images/G/01/lwa/dev/docs/website-developer-guide._TTH_.pdf
18
 */
19
class Docusign extends AbstractService
20
{
21
    /**
22
     * Defined scopes
23
     * @link https://images-na.ssl-images-docusign.com/images/G/01/lwa/dev/docs/website-developer-guide._TTH_.pdf
24
     */
25
    const SCOPE_SIGNATURE     = 'signature';
26
27
    public function __construct(
28
        CredentialsInterface $credentials,
29
        ClientInterface $httpClient,
30
        TokenStorageInterface $storage,
31
        $scopes = array(),
32
        UriInterface $baseApiUri = null
33
    ) {
34
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
35
36
        if (null === $baseApiUri) {
37
            // account-d.docusign.com for the developer sandbox
38
            // account.docusign.com for the production platform.
39
            $this->baseApiUri = new Uri('https://account-d.docusign.com');
40
        }
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getAuthorizationUri(array $additionalParameters = array())
47
    {
48
        $parameters = array_merge(
49
            $additionalParameters,
50
            array(
51
                'client_id'     => $this->credentials->getConsumerId(),
52
                'redirect_uri'  => $this->credentials->getCallbackUrl(),
53
                'response_type' => 'code',
54
            )
55
        );
56
57
        $parameters['scope'] = implode(' ', $this->scopes);
58
59
        // Build the url
60
        $url = clone $this->getAuthorizationEndpoint();
61
        foreach ($parameters as $key => $val) {
62
            $url->addToQuery($key, $val);
63
        }
64
65
        return $url;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function getAuthorizationEndpoint()
72
    {
73
        return new Uri('https://account-d.docusign.com/oauth/auth');
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getAccessTokenEndpoint()
80
    {
81
        return new Uri('https://account-d.docusign.com/oauth/token');
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    protected function getAuthorizationMethod()
88
    {
89
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    protected function parseAccessTokenResponse($responseBody)
96
    {
97
        $data = json_decode($responseBody, true);
98
99
        if (null === $data || !is_array($data)) {
100
            throw new TokenResponseException('Unable to parse response.');
101
        } elseif (isset($data['error_description'])) {
102
            throw new TokenResponseException('Error in retrieving token: "' . $data['error_description'] . '"');
103
        } elseif (isset($data['error'])) {
104
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
105
        }
106
107
        $token = new StdOAuth2Token();
108
        $token->setAccessToken($data['access_token']);
109
        $token->setLifeTime($data['expires_in']);
110
111
        if (isset($data['refresh_token'])) {
112
            $token->setRefreshToken($data['refresh_token']);
113
            unset($data['refresh_token']);
114
        }
115
116
        unset($data['access_token']);
117
        unset($data['expires_in']);
118
119
        $token->setExtraParams($data);
120
121
        return $token;
122
    }
123
}
124