Yahoo   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A getRequestTokenEndpoint() 0 4 1
A getAuthorizationEndpoint() 0 4 1
A getAccessTokenEndpoint() 0 4 1
A refreshAccessToken() 0 23 1
A parseRequestTokenResponse() 0 12 5
A parseAccessTokenResponse() 0 28 5
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
use OAuth\OAuth1\Token\TokenInterface;
14
15
class Yahoo extends AbstractService
16
{
17
    public function __construct(
18
        CredentialsInterface $credentials,
19
        ClientInterface $httpClient,
20
        TokenStorageInterface $storage,
21
        SignatureInterface $signature,
22
        ?UriInterface $baseApiUri = null
23
    ) {
24
        parent::__construct($credentials, $httpClient, $storage, $signature, $baseApiUri);
25
26
        if (null === $baseApiUri) {
27
            $this->baseApiUri = new Uri('https://social.yahooapis.com/v1/');
28
        }
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function getRequestTokenEndpoint()
35
    {
36
        return new Uri('https://api.login.yahoo.com/oauth/v2/get_request_token');
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getAuthorizationEndpoint()
43
    {
44
        return new Uri('https://api.login.yahoo.com/oauth/v2/request_auth');
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getAccessTokenEndpoint()
51
    {
52
        return new Uri('https://api.login.yahoo.com/oauth/v2/get_token');
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function refreshAccessToken(TokenInterface $token)
59
    {
60
        $extraParams = $token->getExtraParams();
61
        $bodyParams = ['oauth_session_handle' => $extraParams['oauth_session_handle']];
62
63
        $authorizationHeader = [
64
            'Authorization' => $this->buildAuthorizationHeaderForAPIRequest(
65
                'POST',
66
                $this->getAccessTokenEndpoint(),
67
                $this->storage->retrieveAccessToken($this->service()),
0 ignored issues
show
Compatibility introduced by
$this->storage->retrieve...Token($this->service()) 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...
68
                $bodyParams
69
            ),
70
        ];
71
72
        $headers = array_merge($authorizationHeader, $this->getExtraOAuthHeaders(), []);
73
74
        $responseBody = $this->httpClient->retrieveResponse($this->getAccessTokenEndpoint(), $bodyParams, $headers);
75
76
        $token = $this->parseAccessTokenResponse($responseBody);
77
        $this->storage->storeAccessToken($this->service(), $token);
78
79
        return $token;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    protected function parseRequestTokenResponse($responseBody)
86
    {
87
        parse_str($responseBody, $data);
88
89
        if (null === $data || !is_array($data)) {
90
            throw new TokenResponseException('Unable to parse response.');
91
        } elseif (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true') {
92
            throw new TokenResponseException('Error in retrieving token.');
93
        }
94
95
        return $this->parseAccessTokenResponse($responseBody);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    protected function parseAccessTokenResponse($responseBody)
102
    {
103
        parse_str($responseBody, $data);
104
105
        if (null === $data || !is_array($data)) {
106
            throw new TokenResponseException('Unable to parse response.');
107
        } elseif (isset($data['error'])) {
108
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
109
        }
110
111
        $token = new StdOAuth1Token();
112
113
        $token->setRequestToken($data['oauth_token']);
114
        $token->setRequestTokenSecret($data['oauth_token_secret']);
115
        $token->setAccessToken($data['oauth_token']);
116
        $token->setAccessTokenSecret($data['oauth_token_secret']);
117
118
        if (isset($data['oauth_expires_in'])) {
119
            $token->setLifetime($data['oauth_expires_in']);
120
        } else {
121
            $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
122
        }
123
124
        unset($data['oauth_token'], $data['oauth_token_secret']);
125
        $token->setExtraParams($data);
126
127
        return $token;
128
    }
129
}
130