Completed
Pull Request — master (#479)
by Andrey
03:22
created

Hubic   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 2 Features 1
Metric Value
wmc 14
lcom 1
cbo 4
dl 0
loc 129
rs 10
c 3
b 2
f 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAccessTokenEndpoint() 0 4 1
A getAuthorizationMethod() 0 4 1
A __construct() 0 20 2
A getAuthorizationEndpoint() 0 5 1
B parseAccessTokenResponse() 0 28 5
B getAuthorizationUri() 0 31 4
1
<?php
2
/**
3
 * Hubic service.
4
 *
5
 * @author  Pedro Amorim <[email protected]>
6
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
7
 * @link    https://api.hubic.com/docs/
8
 */
9
10
namespace OAuth\OAuth2\Service;
11
12
use OAuth\OAuth2\Token\StdOAuth2Token;
13
use OAuth\Common\Http\Exception\TokenResponseException;
14
use OAuth\Common\Http\Uri\Uri;
15
use OAuth\Common\Consumer\CredentialsInterface;
16
use OAuth\Common\Http\Client\ClientInterface;
17
use OAuth\Common\Storage\TokenStorageInterface;
18
use OAuth\Common\Http\Uri\UriInterface;
19
20
/**
21
 * Hubic service.
22
 *
23
 * @author  Pedro Amorim <[email protected]>
24
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
25
 * @link    https://api.hubic.com/docs/
26
 */
27
class Hubic extends AbstractService
28
{
29
30
    // Scopes
31
    const SCOPE_USAGE_GET       = 'usage.r';
32
    const SCOPE_ACCOUNT_GET     = 'account.r';
33
    const SCOPE_GETALLLINKS_GET = 'getAllLinks.r';
34
    const SCOPE_CREDENTIALS_GET = 'credentials.r';
35
    const SCOPE_SPONSORCODE_GET = 'sponsorCode.r';
36
    const SCOPE_ACTIVATE_POST   = 'activate.w';
37
    const SCOPE_SPONSORED_GET   = 'sponsored.r';
38
    const SCOPE_LINKS_GET       = 'links.r';
39
    const SCOPE_LINKS_POST      = 'links.rw';
40
    const SCOPE_LINKS_ALL       = 'links.drw';
41
42
    public function __construct(
43
        CredentialsInterface $credentials,
44
        ClientInterface $httpClient,
45
        TokenStorageInterface $storage,
46
        $scopes = array(),
47
        UriInterface $baseApiUri = null
48
    ) {
49
        parent::__construct(
50
            $credentials,
51
            $httpClient,
52
            $storage,
53
            $scopes,
54
            $baseApiUri,
55
            true
56
        );
57
58
        if (null === $baseApiUri) {
59
            $this->baseApiUri = new Uri('https://api.hubic.com/');
60
        }
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getAuthorizationEndpoint()
67
    {
68
        return new Uri('https://api.hubic.com/oauth/auth');
69
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getAccessTokenEndpoint()
76
    {
77
        return new Uri('https://api.hubic.com/oauth/token');
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    protected function getAuthorizationMethod()
84
    {
85
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    protected function parseAccessTokenResponse($responseBody)
92
    {
93
        $data = json_decode($responseBody, true);
94
95
        if (null === $data || !is_array($data)) {
96
            throw new TokenResponseException('Unable to parse response.');
97
        } elseif (isset($data['error'])) {
98
            throw new TokenResponseException(
99
                'Error in retrieving token: "' . $data['error'] . '"'
100
            );
101
        }
102
103
        $token = new StdOAuth2Token();
104
        $token->setAccessToken($data['access_token']);
105
        $token->setLifetime($data['expires_in']);
106
107
        if (isset($data['refresh_token'])) {
108
            $token->setRefreshToken($data['refresh_token']);
109
            unset($data['refresh_token']);
110
        }
111
112
        unset($data['access_token']);
113
        unset($data['expires_in']);
114
115
        $token->setExtraParams($data);
116
117
        return $token;
118
    }
119
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function getAuthorizationUri(array $additionalParameters = array())
125
    {
126
        $parameters = array_merge(
127
            $additionalParameters,
128
            array(
129
                'type'          => 'web_server',
130
                'client_id'     => $this->credentials->getConsumerId(),
131
                'redirect_uri'  => $this->credentials->getCallbackUrl(),
132
                'response_type' => 'code',
133
            )
134
        );
135
136
        // special, hubic use a param scope with commas
137
        // between scopes instead of spaces
138
        $parameters['scope'] = implode(',', $this->scopes);
139
140
        if ($this->needsStateParameterInAuthUrl()) {
141
            if (!isset($parameters['state'])) {
142
                $parameters['state'] = $this->generateAuthorizationState();
143
            }
144
            $this->storeAuthorizationState($parameters['state']);
145
        }
146
147
        // Build the url
148
        $url = clone $this->getAuthorizationEndpoint();
149
        foreach ($parameters as $key => $val) {
150
            $url->addToQuery($key, $val);
151
        }
152
153
        return $url;
154
    }
155
}
156