Buffer   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
eloc 53
dl 0
loc 133
c 1
b 0
f 0
rs 10

9 Methods

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
            $this->/** @scrutinizer ignore-call */ 
89
                   getRequestTokenEndpoint(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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