Conditions | 27 |
Paths | 9793 |
Total Lines | 114 |
Code Lines | 59 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
134 | public function generateToken() |
||
135 | { |
||
136 | if ($this->token && array_key_exists('expires_in', $this->token) && array_key_exists('created', $this->token) |
||
137 | && $this->token['created'] + $this->token['expires_in'] < time() + 20) |
||
138 | { |
||
139 | if ($this->getOption('authorization.userefresh', true)) |
||
140 | { |
||
141 | $token = $this->refreshToken($this->token['refresh_token']); |
||
142 | |||
143 | if ($token) |
||
144 | { |
||
145 | $this->token = $token; |
||
146 | |||
147 | return $this->token; |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | |||
152 | // It can be client, password, refresh_token, ... |
||
153 | $data = array(); |
||
154 | $data['grant_type'] = $this->getOption('authorization.granttype', 'client_credentials'); |
||
155 | $data['client_id'] = $this->getOption('authorization.clientid'); |
||
156 | $data['client_secret'] = $this->getOption('authorization.clientsecret'); |
||
157 | $url = $this->getOption('authorization.tokenurl', $this->getOption('authorization.authurl')); |
||
158 | $method = $this->getOption('authorization.authmethod', 'post'); |
||
159 | |||
160 | if ($this->getOption('authorization.redirecturi')) |
||
161 | { |
||
162 | $data['redirect_uri'] = $this->getOption('authorization.redirecturi'); |
||
163 | } |
||
164 | |||
165 | if ($this->getOption('authorization.scope')) |
||
166 | { |
||
167 | $scope = is_array($this->getOption('authorization.scope')) ? |
||
168 | implode(' ', $this->getOption('authorization.scope')) : $this->getOption('authorization.scope'); |
||
169 | $data['scope'] = $scope; |
||
170 | } |
||
171 | |||
172 | if ($this->getOption('authorization.state')) |
||
173 | { |
||
174 | $data['state'] = $this->getOption('authorization.state'); |
||
175 | } |
||
176 | |||
177 | if ($this->getOption('authorization.username')) |
||
178 | { |
||
179 | $data['username'] = $this->getOption('authorization.username'); |
||
180 | } |
||
181 | |||
182 | if ($this->getOption('authorization.password')) |
||
183 | { |
||
184 | $data['password'] = $this->getOption('authorization.password'); |
||
185 | } |
||
186 | |||
187 | if ($this->getOption('authorization.requestparams')) |
||
188 | { |
||
189 | $requestParams = (array) $this->getOption('authorization.requestparams'); |
||
190 | |||
191 | foreach ($requestParams as $key => $value) |
||
192 | { |
||
193 | $data[$key] = $value; |
||
194 | } |
||
195 | } |
||
196 | |||
197 | switch ($method) |
||
198 | { |
||
199 | case 'head': |
||
200 | case 'get': |
||
201 | case 'delete': |
||
202 | case 'trace': |
||
203 | |||
204 | if (strpos($url, '?')) |
||
205 | { |
||
206 | $url .= '&'; |
||
207 | } |
||
208 | else |
||
209 | { |
||
210 | $url .= '?1=1'; |
||
211 | } |
||
212 | |||
213 | foreach ($data as $key => $value) |
||
214 | { |
||
215 | $url .= '&' . $key . '=' . urlencode($value); |
||
216 | } |
||
217 | |||
218 | $response = $this->http->{$method}($url); |
||
219 | break; |
||
220 | case 'post': |
||
221 | case 'put': |
||
222 | case 'patch': |
||
223 | $response = $this->http->{$method}($url, $data); |
||
224 | break; |
||
225 | default: |
||
226 | throw new InvalidArgumentException('Unknown HTTP request method: ' . $method . '.'); |
||
227 | } |
||
228 | |||
229 | if ($response->code >= 200 || $response->code < 400) |
||
230 | { |
||
231 | if ($response->headers['Content-Type'] == 'application/json') |
||
232 | { |
||
233 | $token = array_merge(json_decode($response->body, true), array('created' => time())); |
||
234 | } |
||
235 | else |
||
236 | { |
||
237 | parse_str($response->body, $token); |
||
238 | $token = array_merge($token, array('created' => time())); |
||
239 | } |
||
240 | |||
241 | $this->setToken($token); |
||
242 | |||
243 | return $this->token; |
||
244 | } |
||
245 | else |
||
246 | { |
||
247 | throw new Exception('Error code ' . $response->code . ' received refreshing token: ' . $response->body . '.'); |
||
248 | } |
||
366 |