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

Foursquare   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 65
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 2
A getAuthorizationEndpoint() 0 4 1
A getAccessTokenEndpoint() 0 4 1
A parseAccessTokenResponse() 0 20 4
A request() 0 7 1
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
class Foursquare extends AbstractService
10
{
11
    private $apiVersionDate = '20130829';
12
13
    /**
14
     * {@inheritdoc}
15
     */
16
    protected function init()
17
    {
18
        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...
19
            $this->baseApiUri = new Uri('https://api.foursquare.com/v2/');
20
        }
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function getAuthorizationEndpoint()
27
    {
28
        return new Uri('https://foursquare.com/oauth2/authenticate');
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function getAccessTokenEndpoint()
35
    {
36
        return new Uri('https://foursquare.com/oauth2/access_token');
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function parseAccessTokenResponse($responseBody)
43
    {
44
        $data = json_decode($responseBody, true);
45
46
        if (null === $data || !is_array($data)) {
47
            throw new TokenResponseException('Unable to parse response.');
48
        } elseif (isset($data['error'])) {
49
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
50
        }
51
52
        $token = new StdOAuth2Token();
53
        $token->setAccessToken($data['access_token']);
54
        // Foursquare tokens evidently never expire...
55
        $token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES);
56
        unset($data['access_token']);
57
58
        $token->setExtraParams($data);
59
60
        return $token;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function request($path, $method = 'GET', $body = null, array $extraHeaders = array())
67
    {
68
        $uri = $this->determineRequestUriFromPath($path, $this->baseApiUri);
69
        $uri->addToQuery('v', $this->apiVersionDate);
70
71
        return parent::request($uri, $method, $body, $extraHeaders);
72
    }
73
}
74