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

Buffer   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 8
dl 0
loc 131
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 2
A getAuthorizationEndpoint() 0 4 1
A getAccessTokenEndpoint() 0 4 1
A getAuthorizationMethod() 0 4 1
A getAuthorizationUri() 0 19 2
A requestRequestToken() 0 15 1
A parseRequestTokenResponse() 0 11 4
A requestAccessToken() 0 20 1
A parseAccessTokenResponse() 0 19 4
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\OAuth2\Token\StdOAuth2Token;
6
use OAuth\Common\Http\Exception\TokenResponseException;
7
use OAuth\Common\Http\Uri\Uri;
8
9
/**
10
 * Buffer API.
11
 * @author  Sumukh Sridhara <@sumukhsridhara>
12
 * @link https://bufferapp.com/developers/api
13
 */
14
class Buffer extends AbstractService
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    protected function init()
20
    {
21
        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...
22
            $this->baseApiUri = new Uri('https://api.bufferapp.com/1/');
23
        }
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function getAuthorizationEndpoint()
30
    {
31
        return new Uri('https://bufferapp.com/oauth2/authorize');
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getAccessTokenEndpoint()
38
    {
39
        return new Uri('https://api.bufferapp.com/1/oauth2/token.json');
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    protected function getAuthorizationMethod()
46
    {
47
        return static::AUTHORIZATION_METHOD_QUERY_STRING;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getAuthorizationUri(array $additionalParameters = array())
54
    {
55
        $parameters = array_merge(
56
            $additionalParameters,
57
            array(
58
                'client_id'     => $this->credentials->getConsumerId(),
59
                'redirect_uri'  => $this->credentials->getCallbackUrl(),
60
                'response_type' => 'code',
61
            )
62
        );
63
64
        // Build the url
65
        $url = clone $this->getAuthorizationEndpoint();
66
        foreach ($parameters as $key => $val) {
67
            $url->addToQuery($key, $val);
68
        }
69
70
        return $url;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function requestRequestToken()
77
    {
78
        $responseBody = $this->httpClient->retrieveResponse(
79
            $this->getRequestTokenEndpoint(),
0 ignored issues
show
Bug introduced by
The method getRequestTokenEndpoint() does not exist on OAuth\OAuth2\Service\Buffer. Did you maybe mean request()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
80
            array(
81
                'client_key' => $this->credentials->getConsumerId(),
82
                'redirect_uri' => $this->credentials->getCallbackUrl(),
83
                'response_type' => 'code',
84
            )
85
        );
86
87
        $code = $this->parseRequestTokenResponse($responseBody);
88
89
        return $code;
90
    }
91
92
    protected function parseRequestTokenResponse($responseBody)
93
    {
94
        parse_str($responseBody, $data);
95
96
        if (null === $data || !is_array($data)) {
97
            throw new TokenResponseException('Unable to parse response.');
98
        } elseif (!isset($data['code'])) {
99
            throw new TokenResponseException('Error in retrieving code.');
100
        }
101
        return $data['code'];
102
    }
103
104
    public function requestAccessToken($code)
105
    {
106
        $bodyParams = array(
107
            'client_id'     => $this->credentials->getConsumerId(),
108
            'client_secret' => $this->credentials->getConsumerSecret(),
109
            'redirect_uri' => $this->credentials->getCallbackUrl(),
110
            'code'          => $code,
111
            'grant_type'    => 'authorization_code',
112
        );
113
114
        $responseBody = $this->httpClient->retrieveResponse(
115
            $this->getAccessTokenEndpoint(),
116
            $bodyParams,
117
            $this->getExtraOAuthHeaders()
118
        );
119
        $token = $this->parseAccessTokenResponse($responseBody);
120
        $this->storage->storeAccessToken($this->service(), $token, $this->account());
121
122
        return $token;
123
    }
124
125
    protected function parseAccessTokenResponse($responseBody)
126
    {
127
        $data = json_decode($responseBody, true);
128
129
        if ($data === null || !is_array($data)) {
130
            throw new TokenResponseException('Unable to parse response.');
131
        } elseif (isset($data['error'])) {
132
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
133
        }
134
135
        $token = new StdOAuth2Token();
136
        $token->setAccessToken($data['access_token']);
137
138
        $token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES);
139
        unset($data['access_token']);
140
        $token->setExtraParams($data);
141
142
        return $token;
143
    }
144
}
145