Flickr   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

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
A parseRequestTokenResponse() 0 11 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\Common\Consumer\CredentialsInterface;
6
use OAuth\Common\Http\Client\ClientInterface;
7
use OAuth\Common\Http\Exception\TokenResponseException;
8
use OAuth\Common\Http\Uri\Uri;
9
use OAuth\Common\Http\Uri\UriInterface;
10
use OAuth\Common\Storage\TokenStorageInterface;
11
use OAuth\OAuth1\Signature\SignatureInterface;
12
use OAuth\OAuth1\Token\StdOAuth1Token;
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
55
        return $this->parseAccessTokenResponse($responseBody);
56
    }
57
58
    protected function parseAccessTokenResponse($responseBody)
59
    {
60
        parse_str($responseBody, $data);
61
        if ($data === null || !is_array($data)) {
62
            throw new TokenResponseException('Unable to parse response.');
63
        } elseif (isset($data['error'])) {
64
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
65
        }
66
67
        $token = new StdOAuth1Token();
68
        $token->setRequestToken($data['oauth_token']);
69
        $token->setRequestTokenSecret($data['oauth_token_secret']);
70
        $token->setAccessToken($data['oauth_token']);
71
        $token->setAccessTokenSecret($data['oauth_token_secret']);
72
        $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
73
        unset($data['oauth_token'], $data['oauth_token_secret']);
74
        $token->setExtraParams($data);
75
76
        return $token;
77
    }
78
79
    public function request($path, $method = 'GET', $body = null, array $extraHeaders = [])
80
    {
81
        $uri = $this->determineRequestUriFromPath('/', $this->baseApiUri);
82
        $uri->addToQuery('method', $path);
0 ignored issues
show
Bug introduced by
It seems like $path defined by parameter $path on line 79 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...
83
84
        if (!empty($this->format)) {
85
            $uri->addToQuery('format', $this->format);
86
87
            if ($this->format === 'json') {
88
                $uri->addToQuery('nojsoncallback', 1);
89
            }
90
        }
91
92
        $token = $this->storage->retrieveAccessToken($this->service());
93
        $extraHeaders = array_merge($this->getExtraApiHeaders(), $extraHeaders);
94
        $authorizationHeader = [
95
            '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...
96
        ];
97
        $headers = array_merge($authorizationHeader, $extraHeaders);
98
99
        return $this->httpClient->retrieveResponse($uri, $body, $headers, $method);
100
    }
101
102
    public function requestRest($path, $method = 'GET', $body = null, array $extraHeaders = [])
103
    {
104
        return $this->request($path, $method, $body, $extraHeaders);
105
    }
106
107
    public function requestXmlrpc($path, $method = 'GET', $body = null, array $extraHeaders = [])
108
    {
109
        $this->format = 'xmlrpc';
110
111
        return $this->request($path, $method, $body, $extraHeaders);
112
    }
113
114
    public function requestSoap($path, $method = 'GET', $body = null, array $extraHeaders = [])
115
    {
116
        $this->format = 'soap';
117
118
        return $this->request($path, $method, $body, $extraHeaders);
119
    }
120
121
    public function requestJson($path, $method = 'GET', $body = null, array $extraHeaders = [])
122
    {
123
        $this->format = 'json';
124
125
        return $this->request($path, $method, $body, $extraHeaders);
126
    }
127
128
    public function requestPhp($path, $method = 'GET', $body = null, array $extraHeaders = [])
129
    {
130
        $this->format = 'php_serial';
131
132
        return $this->request($path, $method, $body, $extraHeaders);
133
    }
134
}
135