Completed
Pull Request — master (#452)
by
unknown
03:04
created

Flickr::requestRest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 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 Flickr extends AbstractService
15
{
16
    protected $format;
17
18
    public function __construct(
19
        CredentialsInterface $credentials,
20
        ClientInterface $httpClient,
21
        TokenStorageInterface $storage,
22
        SignatureInterface $signature,
23
        UriInterface $baseApiUri = null
24
    ) {
25
        parent::__construct($credentials, $httpClient, $storage, $signature, $baseApiUri);
26
        if ($baseApiUri === null) {
27
            $this->baseApiUri = new Uri('https://api.flickr.com/services/rest/');
28
        }
29
    }
30
31
    public function getRequestTokenEndpoint()
32
    {
33
        return new Uri('https://www.flickr.com/services/oauth/request_token');
34
    }
35
36
    public function getAuthorizationEndpoint()
37
    {
38
        return new Uri('https://www.flickr.com/services/oauth/authorize');
39
    }
40
41
    public function getAccessTokenEndpoint()
42
    {
43
        return new Uri('https://www.flickr.com/services/oauth/access_token');
44
    }
45
46
    protected function parseRequestTokenResponse($responseBody)
47
    {
48
        parse_str($responseBody, $data);
49
        if (null === $data || !is_array($data)) {
50
            throw new TokenResponseException('Unable to parse response.');
51
        } elseif (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] != 'true') {
52
            throw new TokenResponseException('Error in retrieving token.');
53
        }
54
        return $this->parseAccessTokenResponse($responseBody);
55
    }
56
57
    protected function parseAccessTokenResponse($responseBody)
58
    {
59
        parse_str($responseBody, $data);
60
        if ($data === null || !is_array($data)) {
61
            throw new TokenResponseException('Unable to parse response.');
62
        } elseif (isset($data['error'])) {
63
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
64
        }
65
66
        $token = new StdOAuth1Token();
67
        $token->setRequestToken($data['oauth_token']);
68
        $token->setRequestTokenSecret($data['oauth_token_secret']);
69
        $token->setAccessToken($data['oauth_token']);
70
        $token->setAccessTokenSecret($data['oauth_token_secret']);
71
        $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
72
        unset($data['oauth_token'], $data['oauth_token_secret']);
73
        $token->setExtraParams($data);
74
75
        return $token;
76
    }
77
78
    public function request($path, $method = 'GET', $body = null, array $extraHeaders = array())
79
    {
80
        $uri = $this->determineRequestUriFromPath('/', $this->baseApiUri);
81
        $uri->addToQuery('method', $path);
0 ignored issues
show
Bug introduced by
It seems like $path defined by parameter $path on line 78 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...
82
83
        if (!empty($this->format)) {
84
            $uri->addToQuery('format', $this->format);
85
86
            if ($this->format === 'json') {
87
                $uri->addToQuery('nojsoncallback', 1);
88
            }
89
        }
90
91
        $token = $this->storage->retrieveAccessToken($this->service());
92
        $extraHeaders = array_merge($this->getExtraApiHeaders(), $extraHeaders);
93
        $authorizationHeader = array(
94
            '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...
95
        );
96
        $headers = array_merge($authorizationHeader, $extraHeaders);
97
98
        return $this->httpClient->retrieveResponse($uri, $body, $headers, $method);
99
    }
100
101
    public function requestRest($path, $method = 'GET', $body = null, array $extraHeaders = array())
102
    {
103
        return $this->requestAPI($path, $method, $body, $extraHeaders);
104
    }
105
106
    public function requestXmlrpc($path, $method = 'GET', $body = null, array $extraHeaders = array())
107
    {
108
        $this->format = 'xmlrpc';
109
110
        return $this->requestAPI($path, $method, $body, $extraHeaders);
111
    }
112
113
    public function requestSoap($path, $method = 'GET', $body = null, array $extraHeaders = array())
114
    {
115
        $this->format = 'soap';
116
117
        return $this->requestAPI($path, $method, $body, $extraHeaders);
118
    }
119
120
    public function requestJson($path, $method = 'GET', $body = null, array $extraHeaders = array())
121
    {
122
        $this->format = 'json';
123
124
        return $this->requestAPI($path, $method, $body, $extraHeaders);
125
    }
126
127
    public function requestPhp($path, $method = 'GET', $body = null, array $extraHeaders = array())
128
    {
129
        $this->format = 'php_serial';
130
131
        return $this->requestAPI($path, $method, $body, $extraHeaders);
132
    }
133
    
134
    public function requestAPI(array $path = array(), $method = 'GET', $body = null, array $extraHeaders = array())
135
    {
136
        $uri = $this->determineRequestUriFromPath('/', $this->baseApiUri);
137
        $uri->addToQuery('method', $path['method']);
138
        // Revision 2
139
        unset($path['method']);
140
        if(!empty($path)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
141
142
            foreach ($path as $key => $value) {
143
                $uri->addToQuery($key, $value);
144
            }
145
        }
146
147
        if (!empty($this->format)) {
148
            $uri->addToQuery('format', $this->format);
149
150
            if ($this->format === 'json') {
151
                $uri->addToQuery('nojsoncallback', 1);
152
            }
153
        }
154
155
        $token = $this->storage->retrieveAccessToken($this->service());
156
        $extraHeaders = array_merge($this->getExtraApiHeaders(), $extraHeaders);
157
        $authorizationHeader = array(
158
            '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...
159
        );
160
        $headers = array_merge($authorizationHeader, $extraHeaders);
161
162
        return $this->httpClient->retrieveResponse($uri, $body, $headers, $method);
163
    }
164
}
165