Hubic::getAccessTokenEndpoint()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
c 1
b 0
f 0
rs 10
cc 1
nc 1
nop 0
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
 *
8
 * @see    https://api.hubic.com/docs/
9
 */
10
11
namespace OAuth\OAuth2\Service;
12
13
use OAuth\Common\Consumer\CredentialsInterface;
14
use OAuth\Common\Http\Client\ClientInterface;
15
use OAuth\Common\Http\Exception\TokenResponseException;
16
use OAuth\Common\Http\Uri\Uri;
17
use OAuth\Common\Http\Uri\UriInterface;
18
use OAuth\Common\Storage\TokenStorageInterface;
19
use OAuth\OAuth2\Token\StdOAuth2Token;
20
21
/**
22
 * Hubic service.
23
 *
24
 * @author  Pedro Amorim <[email protected]>
25
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
26
 *
27
 * @see    https://api.hubic.com/docs/
28
 */
29
class Hubic extends AbstractService
30
{
31
    // Scopes
32
    const SCOPE_USAGE_GET = 'usage.r';
33
    const SCOPE_ACCOUNT_GET = 'account.r';
34
    const SCOPE_GETALLLINKS_GET = 'getAllLinks.r';
35
    const SCOPE_CREDENTIALS_GET = 'credentials.r';
36
    const SCOPE_SPONSORCODE_GET = 'sponsorCode.r';
37
    const SCOPE_ACTIVATE_POST = 'activate.w';
38
    const SCOPE_SPONSORED_GET = 'sponsored.r';
39
    const SCOPE_LINKS_GET = 'links.r';
40
    const SCOPE_LINKS_POST = 'links.rw';
41
    const SCOPE_LINKS_ALL = 'links.drw';
42
43
    public function __construct(
44
        CredentialsInterface $credentials,
45
        ClientInterface $httpClient,
46
        TokenStorageInterface $storage,
47
        $scopes = [],
48
        ?UriInterface $baseApiUri = null
49
    ) {
50
        parent::__construct(
51
            $credentials,
52
            $httpClient,
53
            $storage,
54
            $scopes,
55
            $baseApiUri,
56
            true
57
        );
58
59
        if (null === $baseApiUri) {
60
            $this->baseApiUri = new Uri('https://api.hubic.com/');
61
        }
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getAuthorizationEndpoint()
68
    {
69
        return new Uri('https://api.hubic.com/oauth/auth');
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'], $data['expires_in']);
113
114
        $token->setExtraParams($data);
115
116
        return $token;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function getAuthorizationUri(array $additionalParameters = [])
123
    {
124
        $parameters = array_merge(
125
            $additionalParameters,
126
            [
127
                'type' => 'web_server',
128
                'client_id' => $this->credentials->getConsumerId(),
129
                'redirect_uri' => $this->credentials->getCallbackUrl(),
130
                'response_type' => 'code',
131
            ]
132
        );
133
134
        // special, hubic use a param scope with commas
135
        // between scopes instead of spaces
136
        $parameters['scope'] = implode(',', $this->scopes);
137
138
        if ($this->needsStateParameterInAuthUrl()) {
139
            if (!isset($parameters['state'])) {
140
                $parameters['state'] = $this->generateAuthorizationState();
141
            }
142
            $this->storeAuthorizationState($parameters['state']);
143
        }
144
145
        // Build the url
146
        $url = clone $this->getAuthorizationEndpoint();
147
        foreach ($parameters as $key => $val) {
148
            $url->addToQuery($key, $val);
149
        }
150
151
        return $url;
152
    }
153
}
154