Docusign   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 33
c 3
b 0
f 0
dl 0
loc 105
ccs 27
cts 27
cp 1
rs 10
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getScopeSeparator() 0 3 1
A getBasicAuth() 0 3 1
A getDefaultHeaders() 0 3 1
A getDefaultScopes() 0 3 1
A getBaseAuthorizationUrl() 0 3 1
A createResourceOwner() 0 3 1
A checkResponse() 0 7 2
A getBaseAccessTokenUrl() 0 3 1
A getResourceOwnerDetailsUrl() 0 3 1
A getUrl() 0 6 2
1
<?php
2
3
namespace Sarhan\OAuth2\Client\Provider;
4
5
use League\OAuth2\Client\Provider\AbstractProvider;
6
use League\OAuth2\Client\Token\AccessToken;
7
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
8
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
9
use Psr\Http\Message\ResponseInterface;
10
11
class Docusign extends AbstractProvider
12
{
13
    use BearerAuthorizationTrait {
14
        getAuthorizationHeaders as getTokenBearerAuthorizationHeaders;
15
    }
16
17
    const URL_ROOT = 'https://account.docusign.com/oauth';
18
    const URL_ROOT_SANDBOX = 'https://account-d.docusign.com/oauth';
19
20
    const ENDPOINT_AUTHORIZTION = 'auth';
21
    const ENDPOINT_ACCESS_TOKEN = 'token';
22
    const ENDPOINT_RESOURCE_OWNER_DETAILS = 'userinfo';
23
24
    const SCOPE_SIGNATURE = 'signature';
25
    const SCOPE_EXTENDED = 'extended';
26
    const SCOPE_IMPERSONATION = 'impersonation';
27
    const SCOPES_DEFAULT = [
28
        self::SCOPE_SIGNATURE,
29
        self::SCOPE_EXTENDED
30
    ];
31
    const SCOPES_SEPARATOR = ' ';
32
33
    protected $sandbox = false;
34
35
    /**
36
     * @inheritDoc
37
     */
38 2
    public function getBaseAuthorizationUrl()
39
    {
40 2
        return $this->getUrl(self::ENDPOINT_AUTHORIZTION);
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46 1
    public function getBaseAccessTokenUrl(array $params)
47
    {
48 1
        return $this->getUrl(self::ENDPOINT_ACCESS_TOKEN);
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54 2
    public function getResourceOwnerDetailsUrl(AccessToken $token)
55
    {
56 2
        return $this->getUrl(self::ENDPOINT_RESOURCE_OWNER_DETAILS);
57
    }
58
59
    /**
60
     * Returns a full url for the given path, with the appropriate docusign
61
     * backennd.
62
     *
63
     * It can be used in combination of getRequest and getResponse methods
64
     * to make further calls to docusign endpoint using the given token.
65
     *
66
     * @param string $path
67
     *
68
     * @return string
69
     *
70
     * @see Docusign::getRequest
71
     * @see Docusign::getResponse
72
     */
73 7
    public function getUrl($path)
74
    {
75 7
        return sprintf(
76 7
            '%s/%s',
77 7
            $this->sandbox ? self::URL_ROOT_SANDBOX : self::URL_ROOT,
78
            $path
79
        );
80
    }
81
82 1
    protected function getDefaultScopes()
83
    {
84 1
        return self::SCOPES_DEFAULT;
85
    }
86
87 3
    protected function checkResponse(ResponseInterface $response, $data)
88
    {
89 3
        if ($response->getStatusCode() >= 400) {
90 1
            throw new IdentityProviderException(
91 1
                $response->getReasonPhrase(),
92 1
                $response->getStatusCode(),
93 1
                \json_decode((string) $response->getBody(), true)
94
            );
95
        }
96 2
    }
97
98 1
    protected function createResourceOwner(array $response, AccessToken $token)
99
    {
100 1
        return new DocusignUser($response, $token);
101
    }
102
103 1
    protected function getScopeSeparator()
104
    {
105 1
        return self::SCOPES_SEPARATOR;
106
    }
107
108 2
    protected function getDefaultHeaders()
109
    {
110 2
        return ['Authorization' => 'Basic ' . $this->getBasicAuth()];
111
    }
112
113 2
    private function getBasicAuth()
114
    {
115 2
        return base64_encode(sprintf('%s:%s', $this->clientId, $this->clientSecret));
116
    }
117
}
118