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 Etsy extends AbstractService |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
protected $scopes = array(); |
18
|
|
|
|
19
|
|
|
public function __construct( |
20
|
|
|
CredentialsInterface $credentials, |
21
|
|
|
ClientInterface $httpClient, |
22
|
|
|
TokenStorageInterface $storage, |
23
|
|
|
SignatureInterface $signature, |
24
|
|
|
UriInterface $baseApiUri = null |
25
|
|
|
) { |
26
|
|
|
parent::__construct($credentials, $httpClient, $storage, $signature, $baseApiUri); |
27
|
|
|
|
28
|
|
|
if (null === $baseApiUri) { |
29
|
|
|
$this->baseApiUri = new Uri('https://openapi.etsy.com/v2/'); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function getRequestTokenEndpoint() |
37
|
|
|
{ |
38
|
|
|
$uri = new Uri($this->baseApiUri . 'oauth/request_token'); |
39
|
|
|
$scopes = $this->getScopes(); |
40
|
|
|
|
41
|
|
|
if (count($scopes)) { |
42
|
|
|
$uri->setQuery('scope=' . implode('%20', $scopes)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $uri; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function getAuthorizationEndpoint() |
52
|
|
|
{ |
53
|
|
|
return new Uri($this->baseApiUri); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function getAccessTokenEndpoint() |
60
|
|
|
{ |
61
|
|
|
return new Uri($this->baseApiUri . 'oauth/access_token'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
protected function parseRequestTokenResponse($responseBody) |
68
|
|
|
{ |
69
|
|
|
parse_str($responseBody, $data); |
70
|
|
|
|
71
|
|
|
if (null === $data || !is_array($data)) { |
72
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
73
|
|
|
} elseif (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true') { |
74
|
|
|
throw new TokenResponseException('Error in retrieving token.'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->parseAccessTokenResponse($responseBody); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
protected function parseAccessTokenResponse($responseBody) |
84
|
|
|
{ |
85
|
|
|
parse_str($responseBody, $data); |
86
|
|
|
|
87
|
|
|
if (null === $data || !is_array($data)) { |
88
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
89
|
|
|
} elseif (isset($data['error'])) { |
90
|
|
|
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$token = new StdOAuth1Token(); |
94
|
|
|
|
95
|
|
|
$token->setRequestToken($data['oauth_token']); |
96
|
|
|
$token->setRequestTokenSecret($data['oauth_token_secret']); |
97
|
|
|
$token->setAccessToken($data['oauth_token']); |
98
|
|
|
$token->setAccessTokenSecret($data['oauth_token_secret']); |
99
|
|
|
|
100
|
|
|
$token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES); |
101
|
|
|
unset($data['oauth_token'], $data['oauth_token_secret']); |
102
|
|
|
$token->setExtraParams($data); |
103
|
|
|
|
104
|
|
|
return $token; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set the scopes for permissions |
109
|
|
|
* @see https://www.etsy.com/developers/documentation/getting_started/oauth#section_permission_scopes |
110
|
|
|
* @param array $scopes |
111
|
|
|
* |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
|
|
public function setScopes(array $scopes) |
115
|
|
|
{ |
116
|
|
|
if (!is_array($scopes)) { |
117
|
|
|
$scopes = array(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$this->scopes = $scopes; |
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Return the defined scopes |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
public function getScopes() |
129
|
|
|
{ |
130
|
|
|
return $this->scopes; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.