Completed
Pull Request — master (#479)
by Andrey
02:39
created

Flickr   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 7
dl 0
loc 120
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A getRequestTokenEndpoint() 0 4 1
A getAuthorizationEndpoint() 0 4 1
A getAccessTokenEndpoint() 0 4 1
B parseRequestTokenResponse() 0 10 5
A parseAccessTokenResponse() 0 20 4
A request() 0 22 3
A requestRest() 0 4 1
A requestXmlrpc() 0 6 1
A requestSoap() 0 6 1
A requestJson() 0 6 1
A requestPhp() 0 6 1
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->request($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->request($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->request($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->request($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->request($path, $method, $body, $extraHeaders);
132
    }
133
}
134