| Conditions | 7 |
| Paths | 9 |
| Total Lines | 64 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 55 | public function get($requestUri, array $requestHeaders = []) |
||
| 56 | { |
||
| 57 | // make sure we have an access token |
||
| 58 | $accessToken = $this->tokenStorage->getAccessToken($this->userId); |
||
| 59 | if (is_null($accessToken)) { |
||
| 60 | error_log('no access_token available'); |
||
| 61 | |||
| 62 | return false; |
||
| 63 | } |
||
| 64 | |||
| 65 | $refreshedToken = false; |
||
| 66 | if ($accessToken->isExpired()) { |
||
| 67 | error_log('access_token expired'); |
||
| 68 | // access_token is expired, try to refresh it |
||
| 69 | if (is_null($accessToken->getRefreshToken())) { |
||
| 70 | error_log('no refresh_token available, delete access_token'); |
||
| 71 | // we do not have a refresh_token, delete this access token, it |
||
| 72 | // is useless now... |
||
| 73 | $this->tokenStorage->deleteAccessToken($this->userId, $accessToken); |
||
| 74 | |||
| 75 | return false; |
||
| 76 | } |
||
| 77 | |||
| 78 | error_log('attempting to refresh access_token'); |
||
| 79 | // deal with possibly revoked authorization! XXX |
||
| 80 | try { |
||
| 81 | $accessToken = $this->oauthClient->refreshAccessToken($accessToken); |
||
| 82 | } catch (OAuthServerException $e) { |
||
| 83 | error_log(sprintf('unable to use refresh_token %s', $e->getMessage())); |
||
| 84 | |||
| 85 | // delete the access_token, the refresh_token could not be used |
||
| 86 | $this->tokenStorage->deleteAccessToken($this->userId, $accessToken); |
||
| 87 | |||
| 88 | return false; |
||
| 89 | } |
||
| 90 | |||
| 91 | // maybe delete old accesstoken here? XXX |
||
| 92 | error_log('access_token refreshed'); |
||
| 93 | $refreshedToken = true; |
||
| 94 | } |
||
| 95 | |||
| 96 | // add Authorization header to the request headers |
||
| 97 | $requestHeaders['Authorization'] = sprintf('Bearer %s', $accessToken->getToken()); |
||
| 98 | |||
| 99 | $response = $this->oauthClient->getHttpClient()->get($requestUri, $requestHeaders); |
||
| 100 | if (401 === $response->getStatusCode()) { |
||
| 101 | error_log('access_token appears to be invalid, delete access_token'); |
||
| 102 | // this indicates an invalid access_token |
||
| 103 | $this->tokenStorage->deleteAccessToken($this->userId, $accessToken); |
||
| 104 | |||
| 105 | return false; |
||
| 106 | } |
||
| 107 | |||
| 108 | error_log('access_token was valid, call succeeded'); |
||
| 109 | |||
| 110 | if ($refreshedToken) { |
||
| 111 | error_log('access_token was refreshed, so store it now for future use'); |
||
| 112 | // if we refreshed the token, and it was successful, i.e. not a 401, |
||
| 113 | // update the stored AccessToken |
||
| 114 | $this->tokenStorage->setAccessToken($this->userId, $accessToken); |
||
| 115 | } |
||
| 116 | |||
| 117 | return $response; |
||
| 118 | } |
||
| 119 | |||
| 124 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.