Completed
Pull Request — master (#479)
by Andrey
03:26
created

Foursquare   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

5 Methods

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