Completed
Pull Request — master (#498)
by Dragonqos
04:53 queued 02:07
created

Delicious   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 7
dl 0
loc 104
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 2
A getAuthorizationEndpoint() 0 5 1
A getAccessTokenEndpoint() 0 4 1
A getAuthorizationMethod() 0 4 1
B parseAccessTokenResponse() 0 30 6
B requestAccessToken() 0 25 2
1
<?php
2
/**
3
 * Delicious service.
4
 *
5
 * @author  Pedro Amorim <[email protected]>
6
 * @license http://www.opensource.org/licenses/mit-license.html  MIT License
7
 * @link    https://github.com/SciDevs/delicious-api/blob/master/api/oauth.md
8
 */
9
10
namespace OAuth\OAuth2\Service;
11
12
use OAuth\OAuth2\Token\StdOAuth2Token;
13
use OAuth\Common\Http\Exception\TokenResponseException;
14
use OAuth\Common\Http\Uri\Uri;
15
16
/**
17
 * Delicious service.
18
 *
19
 * @author  Pedro Amorim <[email protected]>
20
 * @license http://www.opensource.org/licenses/mit-license.html  MIT License
21
 * @link    https://github.com/SciDevs/delicious-api/blob/master/api/oauth.md
22
 */
23
class Delicious extends AbstractService
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function init()
29
    {
30
        $this->stateParameterInAuthUrl = true;
31
32
        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...
33
            $this->baseApiUri = new Uri('https://api.del.icio.us/v1/');
34
        }
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getAuthorizationEndpoint()
41
    {
42
        return new Uri('https://delicious.com/auth/authorize');
43
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getAccessTokenEndpoint()
50
    {
51
        return new Uri('https://avosapi.delicious.com/api/v1/oauth/token');
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected function getAuthorizationMethod()
58
    {
59
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    protected function parseAccessTokenResponse($responseBody)
66
    {
67
        $data = json_decode($responseBody, true);
68
69
        if (null === $data || !is_array($data)) {
70
            throw new TokenResponseException('Unable to parse response.');
71
        } elseif (isset($data['error'])) {
72
            throw new TokenResponseException(
73
                'Error in retrieving token: "' . $data['error'] . '"'
74
            );
75
        }
76
77
        $token = new StdOAuth2Token();
78
        $token->setAccessToken($data['access_token']);
79
80
        if (isset($data['expires_in'])) {
81
            $token->setLifetime($data['expires_in']);
82
            unset($data['expires_in']);
83
        }
84
        if (isset($data['refresh_token'])) {
85
            $token->setRefreshToken($data['refresh_token']);
86
            unset($data['refresh_token']);
87
        }
88
89
        unset($data['access_token']);
90
91
        $token->setExtraParams($data);
92
93
        return $token;
94
    }
95
96
97
    // Special, delicious didn't respect the oauth2 RFC and need a grant_type='code'
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function requestAccessToken($code, $state = null)
102
    {
103
        if (null !== $state) {
104
            $this->validateAuthorizationState($state);
105
        }
106
107
        $bodyParams = array(
108
            'code'          => $code,
109
            'client_id'     => $this->credentials->getConsumerId(),
110
            'client_secret' => $this->credentials->getConsumerSecret(),
111
            'redirect_uri'  => $this->credentials->getCallbackUrl(),
112
            'grant_type'    => 'code',
113
        );
114
115
        $responseBody = $this->httpClient->retrieveResponse(
116
            $this->getAccessTokenEndpoint(),
117
            $bodyParams,
118
            $this->getExtraOAuthHeaders()
119
        );
120
121
        $token = $this->parseAccessTokenResponse($responseBody);
122
        $this->storage->storeAccessToken($this->service(), $token, $this->account());
123
124
        return $token;
125
    }
126
}
127