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

Flickr::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\OAuth1\Service;
4
5
use OAuth\OAuth1\Token\StdOAuth1Token;
6
use OAuth\Common\Http\Exception\TokenResponseException;
7
use OAuth\Common\Http\Uri\Uri;
8
9
class Flickr extends AbstractService
10
{
11
    protected $format;
12
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function init()
17
    {
18
        if (null === $this->baseApiUri) {
19
            $this->baseApiUri = new Uri('https://api.flickr.com/services/rest/');
20
        }
21
    }
22
23
    public function getRequestTokenEndpoint()
24
    {
25
        return new Uri('https://www.flickr.com/services/oauth/request_token');
26
    }
27
28
    public function getAuthorizationEndpoint()
29
    {
30
        return new Uri('https://www.flickr.com/services/oauth/authorize');
31
    }
32
33
    public function getAccessTokenEndpoint()
34
    {
35
        return new Uri('https://www.flickr.com/services/oauth/access_token');
36
    }
37
38
    protected function parseRequestTokenResponse($responseBody)
39
    {
40
        parse_str($responseBody, $data);
41
        if (null === $data || !is_array($data)) {
42
            throw new TokenResponseException('Unable to parse response.');
43
        } elseif (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] != 'true') {
44
            throw new TokenResponseException('Error in retrieving token.');
45
        }
46
        return $this->parseAccessTokenResponse($responseBody);
47
    }
48
49
    protected function parseAccessTokenResponse($responseBody)
50
    {
51
        parse_str($responseBody, $data);
52
        if ($data === null || !is_array($data)) {
53
            throw new TokenResponseException('Unable to parse response.');
54
        } elseif (isset($data['error'])) {
55
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
56
        }
57
58
        $token = new StdOAuth1Token();
59
        $token->setRequestToken($data['oauth_token']);
60
        $token->setRequestTokenSecret($data['oauth_token_secret']);
61
        $token->setAccessToken($data['oauth_token']);
62
        $token->setAccessTokenSecret($data['oauth_token_secret']);
63
        $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
64
        unset($data['oauth_token'], $data['oauth_token_secret']);
65
        $token->setExtraParams($data);
66
67
        return $token;
68
    }
69
70
    public function request($path, $method = 'GET', $body = null, array $extraHeaders = array())
71
    {
72
        $uri = $this->determineRequestUriFromPath('/', $this->baseApiUri);
73
        $uri->addToQuery('method', $path);
0 ignored issues
show
Bug introduced by
It seems like $path defined by parameter $path on line 70 can also be of type object<OAuth\Common\Http\Uri\UriInterface>; however, OAuth\Common\Http\Uri\UriInterface::addToQuery() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
74
75
        if (!empty($this->format)) {
76
            $uri->addToQuery('format', $this->format);
77
78
            if ($this->format === 'json') {
79
                $uri->addToQuery('nojsoncallback', 1);
80
            }
81
        }
82
83
        $token = $this->storage->retrieveAccessToken($this->service(), $this->account());
84
        $extraHeaders = array_merge($this->getExtraApiHeaders(), $extraHeaders);
85
        $authorizationHeader = array(
86
            'Authorization' => $this->buildAuthorizationHeaderForAPIRequest($method, $uri, $token, $body)
0 ignored issues
show
Compatibility introduced by
$token of type object<OAuth\Common\Token\TokenInterface> is not a sub-type of object<OAuth\OAuth1\Token\TokenInterface>. It seems like you assume a child interface of the interface OAuth\Common\Token\TokenInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
87
        );
88
        $headers = array_merge($authorizationHeader, $extraHeaders);
89
90
        return $this->httpClient->retrieveResponse($uri, $body, $headers, $method);
91
    }
92
93
    public function requestRest($path, $method = 'GET', $body = null, array $extraHeaders = array())
94
    {
95
        return $this->request($path, $method, $body, $extraHeaders);
96
    }
97
98
    public function requestXmlrpc($path, $method = 'GET', $body = null, array $extraHeaders = array())
99
    {
100
        $this->format = 'xmlrpc';
101
102
        return $this->request($path, $method, $body, $extraHeaders);
103
    }
104
105
    public function requestSoap($path, $method = 'GET', $body = null, array $extraHeaders = array())
106
    {
107
        $this->format = 'soap';
108
109
        return $this->request($path, $method, $body, $extraHeaders);
110
    }
111
112
    public function requestJson($path, $method = 'GET', $body = null, array $extraHeaders = array())
113
    {
114
        $this->format = 'json';
115
116
        return $this->request($path, $method, $body, $extraHeaders);
117
    }
118
119
    public function requestPhp($path, $method = 'GET', $body = null, array $extraHeaders = array())
120
    {
121
        $this->format = 'php_serial';
122
123
        return $this->request($path, $method, $body, $extraHeaders);
124
    }
125
}
126