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

Bitly::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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