| Conditions | 10 |
| Paths | 34 |
| Total Lines | 54 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 44 | public function makeApiCall(RequestInterface $request) |
||
| 45 | { |
||
| 46 | $response = null; |
||
| 47 | |||
| 48 | // TODO: Add oauth decorator if access token isset |
||
| 49 | if ($this->authenticator->getAccessToken() !== null) { |
||
| 50 | $authenticatedRequest = new OAuthDecorator($request); |
||
| 51 | $authenticatedRequest->setAccessToken($this->authenticator->getAccessToken()); |
||
| 52 | $authenticatedRequest->addAuthentication(); |
||
| 53 | |||
| 54 | $response = $this->client->makeCall( |
||
| 55 | $authenticatedRequest->getFullUrl(), |
||
| 56 | $this->add_default_headers($authenticatedRequest->getHeaders()), |
||
| 57 | $authenticatedRequest->getOptions() |
||
| 58 | ); |
||
| 59 | |||
| 60 | if ($response['status'] == 200) { |
||
| 61 | $this->authenticationRetryLimit = 0; |
||
| 62 | |||
| 63 | try { |
||
| 64 | return new Response($response['body'], $response['headers']); |
||
| 65 | } catch (ResponseException $e) { |
||
| 66 | throw new ClientException($e->getMessage(), $e->getCode(), $e); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | if ($response === null || $response['status'] == 401) { |
||
| 72 | |||
| 73 | $this->authenticationRetryLimit++; |
||
| 74 | |||
| 75 | if ($this->authenticationRetryLimit > self::MAX_RETRY_LIMIT) { |
||
| 76 | throw new AccessDeniedException('Authentication retry limit reached.'); |
||
| 77 | } |
||
| 78 | |||
| 79 | try { |
||
| 80 | $this->authenticator->setBaseUrl($request->getBaseUrl()); |
||
| 81 | if ($this->authenticator->getAccessToken() !== null) { |
||
| 82 | $this->authenticator->refreshAccessToken(); |
||
| 83 | } else { |
||
| 84 | $this->authenticator->getAuthenticationTokens(); |
||
| 85 | } |
||
| 86 | |||
| 87 | // Reexecute event |
||
| 88 | return $this->makeApiCall($request); |
||
| 89 | } catch (AccessDeniedException $e) { |
||
| 90 | throw new AccessDeniedException($e->getMessage(), $e->getCode(), $e); |
||
| 91 | } catch (AuthenticationException $e) { |
||
| 92 | throw new AccessDeniedException('Could not authenticate against API.', $e->getCode(), $e); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | throw new ClientException(sprintf('The server returned an error with status %s.', $response['status'])); |
||
| 97 | } |
||
| 98 | |||
| 118 |