Completed
Pull Request — master (#485)
by
unknown
04:42
created

Twitter   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A getRequestTokenEndpoint() 0 4 1
A getAuthorizationEndpoint() 0 8 3
A setAuthorizationEndpoint() 0 9 3
A getAccessTokenEndpoint() 0 4 1
B parseRequestTokenResponse() 0 12 5
B parseAccessTokenResponse() 0 25 6
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
use OAuth\Common\Exception\Exception;
14
15
class Twitter extends AbstractService
16
{
17
    const ENDPOINT_AUTHENTICATE = "https://api.twitter.com/oauth/authenticate";
18
    const ENDPOINT_AUTHORIZE    = "https://api.twitter.com/oauth/authorize";
19
20
    protected $authorizationEndpoint   = self::ENDPOINT_AUTHENTICATE;
21
22
    public function __construct(
23
        CredentialsInterface $credentials,
24
        ClientInterface $httpClient,
25
        TokenStorageInterface $storage,
26
        SignatureInterface $signature,
27
        UriInterface $baseApiUri = null
28
    ) {
29
        parent::__construct($credentials, $httpClient, $storage, $signature, $baseApiUri);
30
31
        if (null === $baseApiUri) {
32
            $this->baseApiUri = new Uri('https://api.twitter.com/1.1/');
33
        }
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getRequestTokenEndpoint()
40
    {
41
        return new Uri('https://api.twitter.com/oauth/request_token');
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getAuthorizationEndpoint()
48
    {
49
        if ($this->authorizationEndpoint != self::ENDPOINT_AUTHENTICATE
50
        && $this->authorizationEndpoint != self::ENDPOINT_AUTHORIZE) {
51
            $this->authorizationEndpoint = self::ENDPOINT_AUTHENTICATE;
52
        }
53
        return new Uri($this->authorizationEndpoint);
54
    }
55
56
    /**
57
     * @param string $authorizationEndpoint
0 ignored issues
show
Documentation introduced by
There is no parameter named $authorizationEndpoint. Did you maybe mean $endpoint?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
58
     *
59
     * @throws Exception
60
     */
61
    public function setAuthorizationEndpoint($endpoint)
62
    {
63
        if ($endpoint != self::ENDPOINT_AUTHENTICATE && $endpoint != self::ENDPOINT_AUTHORIZE) {
64
            throw new Exception(
65
                sprintf("'%s' is not a correct Twitter authorization endpoint.", $endpoint)
66
            );
67
        }
68
        $this->authorizationEndpoint = $endpoint;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getAccessTokenEndpoint()
75
    {
76
        return new Uri('https://api.twitter.com/oauth/access_token');
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    protected function parseRequestTokenResponse($responseBody)
83
    {
84
        parse_str($responseBody, $data);
85
86
        if (null === $data || !is_array($data)) {
87
            throw new TokenResponseException('Unable to parse response.');
88
        } elseif (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true') {
89
            throw new TokenResponseException('Error in retrieving token.');
90
        }
91
92
        return $this->parseAccessTokenResponse($responseBody);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    protected function parseAccessTokenResponse($responseBody)
99
    {
100
        parse_str($responseBody, $data);
101
102
        if (null === $data || !is_array($data)) {
103
            throw new TokenResponseException('Unable to parse response: ' . $responseBody);
104
        } elseif (isset($data['error'])) {
105
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
106
        } elseif (!isset($data["oauth_token"]) || !isset($data["oauth_token_secret"])) {
107
            throw new TokenResponseException('Invalid response. OAuth Token data not set: ' . $responseBody);
108
        }
109
110
        $token = new StdOAuth1Token();
111
112
        $token->setRequestToken($data['oauth_token']);
113
        $token->setRequestTokenSecret($data['oauth_token_secret']);
114
        $token->setAccessToken($data['oauth_token']);
115
        $token->setAccessTokenSecret($data['oauth_token_secret']);
116
117
        $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
118
        unset($data['oauth_token'], $data['oauth_token_secret']);
119
        $token->setExtraParams($data);
120
121
        return $token;
122
    }
123
}
124