| Conditions | 8 |
| Paths | 8 |
| Total Lines | 54 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 57 | public function authenticate() |
||
| 58 | { |
||
| 59 | $client = $this->getClient(); |
||
| 60 | $request = Request::get(); |
||
| 61 | $session = new Session; |
||
| 62 | |||
| 63 | if ($code = $request->query->get('code')) { |
||
| 64 | if (!$requestState = $request->query->get('state')) { |
||
| 65 | throw new \Exception('Missing state from Vimeo request'); |
||
| 66 | } |
||
| 67 | |||
| 68 | if (!$sessionState = $session->get('vimeo_state')) { |
||
| 69 | throw new \Exception('Missing state from Vimeo session'); |
||
| 70 | } |
||
| 71 | |||
| 72 | if (strval($requestState) !== strval($sessionState)) { |
||
| 73 | throw new \Exception('Vimeo session state and request state don\'t match'); |
||
| 74 | } |
||
| 75 | |||
| 76 | $this->debug('Found Vimeo oAuth code', ['code' => $code]); |
||
| 77 | $response = $client->accessToken($code, $this->getConfiguration('redirectUri')); |
||
| 78 | |||
| 79 | if (200 === $response['status'] and $token = $response['body']['access_token']) { |
||
| 80 | $this->debug('Found Vimeo oAuth token', ['token' => $token]); |
||
| 81 | $client->setToken($token); |
||
| 82 | $this->setCredentials($token); |
||
| 83 | } else { |
||
| 84 | $this->error('Vimeo authentication failed', $response); |
||
| 85 | throw new \Exception('Vimeo authentication failed'); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | if (!$client->getToken()) { |
||
| 90 | $this->debug('Missing Vimeo token, try to authenticate...'); |
||
| 91 | |||
| 92 | $state = mt_rand(); |
||
| 93 | $session->set('vimeo_state', $state); |
||
| 94 | |||
| 95 | $this->redirect($client->buildAuthorizationEndpoint( |
||
| 96 | $this->getConfiguration('redirectUri'), |
||
| 97 | $this->getConfiguration('scopes'), |
||
| 98 | $state |
||
| 99 | )); |
||
| 100 | } |
||
| 101 | |||
| 102 | // Clean query parameters as they may cause error |
||
| 103 | // on other Adapter's authentication process |
||
| 104 | $request->query->set('code', null); |
||
| 105 | $request->query->set('state', null); |
||
| 106 | |||
| 107 | $this->debug('Vimeo account is authenticated'); |
||
| 108 | |||
| 109 | $this->isAuthenticated = true; |
||
| 110 | } |
||
| 111 | |||
| 159 |