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

Twitter::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A Twitter::getRequestTokenEndpoint() 0 4 1
1
<?php
2
3
namespace OAuth\OAuth1\Service;
4
5
use OAuth\OAuth1\Token\StdOAuth1Token;
6
use OAuth\Common\Http\Exception\TokenResponseException;
7
use OAuth\Common\Http\Uri\Uri;
8
use OAuth\Common\Exception\Exception;
9
10
class Twitter extends AbstractService
11
{
12
    const ENDPOINT_AUTHENTICATE = "https://api.twitter.com/oauth/authenticate";
13
    const ENDPOINT_AUTHORIZE    = "https://api.twitter.com/oauth/authorize";
14
15
    protected $authorizationEndpoint   = self::ENDPOINT_AUTHENTICATE;
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function init()
21
    {
22
        if (null === $this->baseApiUri) {
23
            $this->baseApiUri = new Uri('https://api.twitter.com/1.1/');
24
        }
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function getRequestTokenEndpoint()
31
    {
32
        return new Uri('https://api.twitter.com/oauth/request_token');
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getAuthorizationEndpoint()
39
    {
40
        if ($this->authorizationEndpoint != self::ENDPOINT_AUTHENTICATE
41
        && $this->authorizationEndpoint != self::ENDPOINT_AUTHORIZE) {
42
            $this->authorizationEndpoint = self::ENDPOINT_AUTHENTICATE;
43
        }
44
        return new Uri($this->authorizationEndpoint);
45
    }
46
47
    /**
48
     * @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...
49
     *
50
     * @throws Exception
51
     */
52
    public function setAuthorizationEndpoint($endpoint)
53
    {
54
        if ($endpoint != self::ENDPOINT_AUTHENTICATE && $endpoint != self::ENDPOINT_AUTHORIZE) {
55
            throw new Exception(
56
                sprintf("'%s' is not a correct Twitter authorization endpoint.", $endpoint)
57
            );
58
        }
59
        $this->authorizationEndpoint = $endpoint;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getAccessTokenEndpoint()
66
    {
67
        return new Uri('https://api.twitter.com/oauth/access_token');
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    protected function parseRequestTokenResponse($responseBody)
74
    {
75
        parse_str($responseBody, $data);
76
77
        if (null === $data || !is_array($data)) {
78
            throw new TokenResponseException('Unable to parse response.');
79
        } elseif (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true') {
80
            throw new TokenResponseException('Error in retrieving token.');
81
        }
82
83
        return $this->parseAccessTokenResponse($responseBody);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    protected function parseAccessTokenResponse($responseBody)
90
    {
91
        parse_str($responseBody, $data);
92
93
        if (null === $data || !is_array($data)) {
94
            throw new TokenResponseException('Unable to parse response: ' . $responseBody);
95
        } elseif (isset($data['error'])) {
96
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
97
        } elseif (!isset($data["oauth_token"]) || !isset($data["oauth_token_secret"])) {
98
            throw new TokenResponseException('Invalid response. OAuth Token data not set: ' . $responseBody);
99
        }
100
101
        $token = new StdOAuth1Token();
102
103
        $token->setRequestToken($data['oauth_token']);
104
        $token->setRequestTokenSecret($data['oauth_token_secret']);
105
        $token->setAccessToken($data['oauth_token']);
106
        $token->setAccessTokenSecret($data['oauth_token_secret']);
107
108
        $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
109
        unset($data['oauth_token'], $data['oauth_token_secret']);
110
        $token->setExtraParams($data);
111
112
        return $token;
113
    }
114
}
115