| Conditions | 9 |
| Paths | 10 |
| Total Lines | 59 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 66 | public function getAccessToken($authorizationRequestUri, $authorizationResponseCode, $authorizationResponseState) |
||
| 67 | { |
||
| 68 | // parse our authorizationRequestUri to extract the state |
||
| 69 | if (false === strpos($authorizationRequestUri, '?')) { |
||
| 70 | throw new OAuthException('invalid authorizationRequestUri'); |
||
| 71 | } |
||
| 72 | |||
| 73 | parse_str(explode('?', $authorizationRequestUri)[1], $queryParams); |
||
| 74 | |||
| 75 | if (!isset($queryParams['state'])) { |
||
| 76 | throw new OAuthException('state missing from authorizationRequestUri'); |
||
| 77 | } |
||
| 78 | |||
| 79 | if (!isset($queryParams['redirect_uri'])) { |
||
| 80 | throw new OAuthException('redirect_uri missing from authorizationRequestUri'); |
||
| 81 | } |
||
| 82 | |||
| 83 | if ($authorizationResponseState !== $queryParams['state']) { |
||
| 84 | throw new OAuthException('state from authorizationRequestUri MUST match authorizationResponseState'); |
||
| 85 | } |
||
| 86 | |||
| 87 | // prepare access_token request |
||
| 88 | $tokenRequestData = [ |
||
| 89 | 'client_id' => $this->provider->getId(), |
||
| 90 | 'grant_type' => 'authorization_code', |
||
| 91 | 'code' => $authorizationResponseCode, |
||
| 92 | 'redirect_uri' => $queryParams['redirect_uri'], |
||
| 93 | ]; |
||
| 94 | |||
| 95 | $responseData = $this->httpClient->post( |
||
| 96 | $this->provider, |
||
| 97 | $tokenRequestData |
||
| 98 | ); |
||
| 99 | |||
| 100 | if (!isset($responseData['access_token'])) { |
||
| 101 | throw new OAuthException('no access_token received from token endpoint'); |
||
| 102 | } |
||
| 103 | |||
| 104 | if (!isset($responseData['token_type'])) { |
||
| 105 | throw new OAuthException('no token_type received from token endpoint'); |
||
| 106 | } |
||
| 107 | |||
| 108 | $scope = null; |
||
| 109 | if (isset($responseData['scope'])) { |
||
| 110 | $scope = $responseData['scope']; |
||
| 111 | } |
||
| 112 | |||
| 113 | $expiresIn = null; |
||
| 114 | if (isset($responseData['expires_in'])) { |
||
| 115 | $expiresIn = $responseData['expires_in']; |
||
| 116 | } |
||
| 117 | |||
| 118 | return new AccessToken( |
||
| 119 | $responseData['access_token'], |
||
| 120 | $responseData['token_type'], |
||
| 121 | $scope, |
||
| 122 | $expiresIn |
||
| 123 | ); |
||
| 124 | } |
||
| 125 | } |
||
| 126 |