Completed
Pull Request — master (#467)
by
unknown
03:05
created

Bitly   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 99
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuthorizationEndpoint() 0 4 1
A getAccessTokenEndpoint() 0 4 1
A getAuthorizationMethod() 0 4 1
A parseAccessTokenResponse() 0 20 4
A __construct() 0 13 2
B requestAccessToken() 0 31 2
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 Bitly extends AbstractService
14
{
15
    public function __construct(
16
        CredentialsInterface $credentials,
17
        ClientInterface $httpClient,
18
        TokenStorageInterface $storage,
19
        $scopes = array(),
20
        UriInterface $baseApiUri = null
21
    ) {
22
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
23
24
        if (null === $baseApiUri) {
25
            $this->baseApiUri = new Uri('https://api-ssl.bitly.com/v3/');
26
        }
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getAuthorizationEndpoint()
33
    {
34
        return new Uri('https://bitly.com/oauth/authorize');
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getAccessTokenEndpoint()
41
    {
42
        return new Uri('https://api-ssl.bitly.com/oauth/access_token');
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function getAuthorizationMethod()
49
    {
50
        return static::AUTHORIZATION_METHOD_QUERY_STRING;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected function parseAccessTokenResponse($responseBody)
57
    {
58
        $data = json_decode($responseBody, true);
59
60
        if (null === $data || !is_array($data)) {
61
            throw new TokenResponseException('Unable to parse response.');
62
        } elseif (isset($data['error'])) {
63
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
64
        }
65
66
        $token = new StdOAuth2Token();
67
        $token->setAccessToken($data['access_token']);
68
        // I'm invincible!!!
69
        $token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES);
70
        unset($data['access_token']);
71
72
        $token->setExtraParams($data);
73
74
        return $token;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function requestAccessToken($code, $state = null)
81
    {
82
        if (null !== $state) {
83
            $this->validateAuthorizationState($state);
84
        }
85
86
        $bodyParams = array(
87
            'code'          => $code,
88
            'client_id'     => $this->credentials->getConsumerId(),
89
            'client_secret' => $this->credentials->getConsumerSecret(),
90
            'redirect_uri'  => $this->credentials->getCallbackUrl(),
91
            'grant_type'    => 'authorization_code',
92
        );
93
94
        $responseBody = $this->httpClient->retrieveResponse(
95
            $this->getAccessTokenEndpoint(),
96
            $bodyParams,
97
            $this->getExtraOAuthHeaders()
98
        );
99
100
        // we can scream what we want that we want bitly to return a json encoded string (format=json), but the
101
        // WOAH WATCH YOUR LANGUAGE ;) service doesn't seem to like screaming, hence we need to manually
102
        // parse the result
103
        $parsedResult = array();
104
        parse_str($responseBody, $parsedResult);
105
106
        $token = $this->parseAccessTokenResponse(json_encode($parsedResult));
107
        $this->storage->storeAccessToken($this->service(), $token);
108
109
        return $token;
110
    }
111
}
112