1 | <?php |
||
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() |
||
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | public function getAccessTokenEndpoint() |
||
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() |
||
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.