Completed
Pull Request — master (#498)
by Dragonqos
02:30
created

Harvest::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\Common\Http\Exception\TokenResponseException;
6
use OAuth\Common\Http\Uri\Uri;
7
use OAuth\Common\Token\TokenInterface;
8
use OAuth\OAuth2\Token\StdOAuth2Token;
9
10
class Harvest extends AbstractService
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    protected function init()
16
    {
17
        if( $this->baseApiUri === null ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
18
            $this->baseApiUri = new Uri('https://api.harvestapp.com/');
19
        }
20
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function getAuthorizationUri(array $additionalParameters = array())
26
    {
27
        $parameters = array_merge(
28
            $additionalParameters,
29
            array(
30
                'client_id'     => $this->credentials->getConsumerId(),
31
                'redirect_uri'  => $this->credentials->getCallbackUrl(),
32
                'state' => 'optional-csrf-token',
33
                'response_type' => 'code',
34
            )
35
        );
36
37
        // Build the url
38
        $url = clone $this->getAuthorizationEndpoint();
39
        foreach ($parameters as $key => $val) {
40
            $url->addToQuery($key, $val);
41
        }
42
43
        return $url;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getAuthorizationEndpoint()
50
    {
51
        return new Uri('https://api.harvestapp.com/oauth2/authorize');
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getAccessTokenEndpoint()
58
    {
59
        return new Uri('https://api.harvestapp.com/oauth2/token');
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    protected function getAuthorizationMethod()
66
    {
67
        return static::AUTHORIZATION_METHOD_QUERY_STRING;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    protected function parseAccessTokenResponse($responseBody)
74
    {
75
        $data = json_decode($responseBody, true);
76
77
        if (null === $data || ! is_array($data)) {
78
            throw new TokenResponseException('Unable to parse response.');
79
        } elseif (isset($data['error'])) {
80
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
81
        }
82
83
        $token = new StdOAuth2Token();
84
        $token->setAccessToken($data['access_token']);
85
        $token->setLifetime($data['expires_in']);
86
        $token->setRefreshToken($data['refresh_token']);
87
88
        unset($data['access_token']);
89
90
        $token->setExtraParams($data);
91
92
        return $token;
93
    }
94
95
    /**
96
     * Refreshes an OAuth2 access token.
97
     *
98
     * @param TokenInterface $token
99
     *
100
     * @return TokenInterface $token
101
     *
102
     * @throws MissingRefreshTokenException
103
     */
104
    public function refreshAccessToken(TokenInterface $token)
105
    {
106
        $refreshToken = $token->getRefreshToken();
107
108
        if (empty($refreshToken)) {
109
            throw new MissingRefreshTokenException();
110
        }
111
112
        $parameters = array(
113
            'grant_type'    => 'refresh_token',
114
            'type'          => 'web_server',
115
            'client_id'     => $this->credentials->getConsumerId(),
116
            'client_secret' => $this->credentials->getConsumerSecret(),
117
            'refresh_token' => $refreshToken,
118
        );
119
120
        $responseBody = $this->httpClient->retrieveResponse(
121
            $this->getAccessTokenEndpoint(),
122
            $parameters,
123
            $this->getExtraOAuthHeaders()
124
        );
125
        $token = $this->parseAccessTokenResponse($responseBody);
126
        $this->storage->storeAccessToken($this->service(), $token, $this->account());
127
128
        return $token;
129
    }
130
131
    /**
132
     * @return array
133
     */
134
    protected function getExtraOAuthHeaders()
135
    {
136
        return array('Accept' => 'application/json');
137
    }
138
139
    /**
140
     * Return any additional headers always needed for this service implementation's API calls.
141
     *
142
     * @return array
143
     */
144
    protected function getExtraApiHeaders()
145
    {
146
        return array('Accept' => 'application/json');
147
    }
148
}
149