Harvest::getAccessTokenEndpoint()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
c 2
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\Common\Consumer\CredentialsInterface;
6
use OAuth\Common\Http\Client\ClientInterface;
7
use OAuth\Common\Http\Exception\TokenResponseException;
8
use OAuth\Common\Http\Uri\Uri;
9
use OAuth\Common\Http\Uri\UriInterface;
10
use OAuth\Common\Storage\TokenStorageInterface;
11
use OAuth\Common\Token\TokenInterface;
12
use OAuth\OAuth2\Token\StdOAuth2Token;
13
14
class Harvest extends AbstractService
15
{
16
    public function __construct(
17
        CredentialsInterface $credentials,
18
        ClientInterface $httpClient,
19
        TokenStorageInterface $storage,
20
        $scopes = [],
21
        ?UriInterface $baseApiUri = null
22
    ) {
23
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
24
25
        if (null === $baseApiUri) {
26
            $this->baseApiUri = new Uri('https://api.harvestapp.com/');
27
        }
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getAuthorizationUri(array $additionalParameters = [])
34
    {
35
        $parameters = array_merge(
36
            $additionalParameters,
37
            [
38
                'client_id' => $this->credentials->getConsumerId(),
39
                'redirect_uri' => $this->credentials->getCallbackUrl(),
40
                'state' => 'optional-csrf-token',
41
                'response_type' => 'code',
42
            ]
43
        );
44
45
        // Build the url
46
        $url = clone $this->getAuthorizationEndpoint();
47
        foreach ($parameters as $key => $val) {
48
            $url->addToQuery($key, $val);
49
        }
50
51
        return $url;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getAuthorizationEndpoint()
58
    {
59
        return new Uri('https://api.harvestapp.com/oauth2/authorize');
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getAccessTokenEndpoint()
66
    {
67
        return new Uri('https://api.harvestapp.com/oauth2/token');
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    protected function getAuthorizationMethod()
74
    {
75
        return static::AUTHORIZATION_METHOD_QUERY_STRING;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    protected function parseAccessTokenResponse($responseBody)
82
    {
83
        $data = json_decode($responseBody, true);
84
85
        if (null === $data || !is_array($data)) {
86
            throw new TokenResponseException('Unable to parse response.');
87
        } elseif (isset($data['error'])) {
88
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
89
        }
90
91
        $token = new StdOAuth2Token();
92
        $token->setAccessToken($data['access_token']);
93
        $token->setLifetime($data['expires_in']);
94
        $token->setRefreshToken($data['refresh_token']);
95
96
        unset($data['access_token']);
97
98
        $token->setExtraParams($data);
99
100
        return $token;
101
    }
102
103
    /**
104
     * Refreshes an OAuth2 access token.
105
     *
106
     * @return TokenInterface $token
107
     */
108
    public function refreshAccessToken(TokenInterface $token)
109
    {
110
        $refreshToken = $token->getRefreshToken();
111
112
        if (empty($refreshToken)) {
113
            throw new MissingRefreshTokenException();
0 ignored issues
show
Bug introduced by
The type OAuth\OAuth2\Service\MissingRefreshTokenException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
114
        }
115
116
        $parameters = [
117
            'grant_type' => 'refresh_token',
118
            'type' => 'web_server',
119
            'client_id' => $this->credentials->getConsumerId(),
120
            'client_secret' => $this->credentials->getConsumerSecret(),
121
            'refresh_token' => $refreshToken,
122
        ];
123
124
        $responseBody = $this->httpClient->retrieveResponse(
125
            $this->getAccessTokenEndpoint(),
126
            $parameters,
127
            $this->getExtraOAuthHeaders()
128
        );
129
        $token = $this->parseAccessTokenResponse($responseBody);
130
        $this->storage->storeAccessToken($this->service(), $token);
131
132
        return $token;
133
    }
134
135
    /**
136
     * @return array
137
     */
138
    protected function getExtraOAuthHeaders()
139
    {
140
        return ['Accept' => 'application/json'];
141
    }
142
143
    /**
144
     * Return any additional headers always needed for this service implementation's API calls.
145
     *
146
     * @return array
147
     */
148
    protected function getExtraApiHeaders()
149
    {
150
        return ['Accept' => 'application/json'];
151
    }
152
}
153