Completed
Pull Request — master (#462)
by
unknown
02:54
created

Goodreads   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 5
Bugs 0 Features 3
Metric Value
wmc 13
c 5
b 0
f 3
lcom 0
cbo 4
dl 0
loc 88
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A getRequestTokenEndpoint() 0 4 1
A getAuthorizationEndpoint() 0 4 1
A getAccessTokenEndpoint() 0 4 1
A parseRequestTokenResponse() 0 17 4
B parseAccessTokenResponse() 0 23 4
1
<?php
2
3
namespace OAuth\OAuth1\Service;
4
5
use OAuth\OAuth1\Signature\SignatureInterface;
6
use OAuth\OAuth1\Token\StdOAuth1Token;
7
use OAuth\Common\Http\Exception\TokenResponseException;
8
use OAuth\Common\Http\Uri\Uri;
9
use OAuth\Common\Consumer\CredentialsInterface;
10
use OAuth\Common\Http\Uri\UriInterface;
11
use OAuth\Common\Storage\TokenStorageInterface;
12
use OAuth\Common\Http\Client\ClientInterface;
13
14
class Goodreads extends AbstractService
15
{
16
    public function __construct(
17
        CredentialsInterface $credentials,
18
        ClientInterface $httpClient,
19
        TokenStorageInterface $storage,
20
        SignatureInterface $signature,
21
        UriInterface $baseApiUri = null
22
    ) {
23
        parent::__construct($credentials, $httpClient, $storage, $signature, $baseApiUri);
24
25
        if (null === $baseApiUri) {
26
            $this->baseApiUri = new Uri('https://www.goodreads.com/');
27
        }
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getRequestTokenEndpoint()
34
    {
35
        return new Uri('https://www.goodreads.com/oauth/request_token');
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getAuthorizationEndpoint()
42
    {
43
        return new Uri('https://www.goodreads.com/oauth/authorize');
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getAccessTokenEndpoint()
50
    {
51
        return new Uri('https://www.goodreads.com/oauth/access_token');
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected function parseRequestTokenResponse($responseBody)
58
    {
59
        parse_str($responseBody, $data);
60
        if (null === $data || !is_array($data)) {
61
            throw new TokenResponseException('Unable to parse response.');
62
        }
63
        //Goodreads currently isn't adhering to this standard. See ticket here: https://www.goodreads.com/topic/show/18023905-oauth1-standards-not-met
64
        // elseif (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true') {
65
        //     throw new TokenResponseException('Error in retrieving token.');
66
        // }
67
68
        elseif (!isset($data['oauth_token'])) {
69
          throw new TokenResponseException('Error in retrieving token.');
0 ignored issues
show
Coding Style introduced by
It seems like the identation of this line is off (expected at least 12 spaces, but found 10).
Loading history...
70
        }
71
72
        return $this->parseAccessTokenResponse($responseBody);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    protected function parseAccessTokenResponse($responseBody)
79
    {
80
        parse_str($responseBody, $data);
81
82
        if (null === $data || !is_array($data)) {
83
            throw new TokenResponseException('Unable to parse response.');
84
        } elseif (isset($data['error'])) {
85
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
86
        }
87
88
        $token = new StdOAuth1Token();
89
90
        $token->setRequestToken($data['oauth_token']);
91
        $token->setRequestTokenSecret($data['oauth_token_secret']);
92
        $token->setAccessToken($data['oauth_token']);
93
        $token->setAccessTokenSecret($data['oauth_token_secret']);
94
95
        $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
96
        unset($data['oauth_token'], $data['oauth_token_secret']);
97
        $token->setExtraParams($data);
98
99
        return $token;
100
    }
101
}
102