| Conditions | 10 |
| Paths | 84 |
| Total Lines | 70 |
| Code Lines | 45 |
| Lines | 5 |
| Ratio | 7.14 % |
| 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 |
||
| 148 | protected function run($argument) { |
||
| 149 | $target = $argument['url']; |
||
| 150 | $source = $this->urlGenerator->getAbsoluteURL('/'); |
||
| 151 | $source = rtrim($source, '/'); |
||
| 152 | $token = $argument['token']; |
||
| 153 | |||
| 154 | $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); |
||
| 155 | $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
||
| 156 | |||
| 157 | // make sure that we have a well formated url |
||
| 158 | $url = rtrim($target, '/') . '/' . trim($endPoint, '/') . $this->format; |
||
| 159 | |||
| 160 | $result = null; |
||
| 161 | try { |
||
| 162 | $result = $this->httpClient->get( |
||
| 163 | $url, |
||
| 164 | [ |
||
| 165 | 'query' => |
||
| 166 | [ |
||
| 167 | 'url' => $source, |
||
| 168 | 'token' => $token |
||
| 169 | ], |
||
| 170 | 'timeout' => 3, |
||
| 171 | 'connect_timeout' => 3, |
||
| 172 | ] |
||
| 173 | ); |
||
| 174 | |||
| 175 | $status = $result->getStatusCode(); |
||
| 176 | |||
| 177 | } catch (ClientException $e) { |
||
| 178 | $status = $e->getCode(); |
||
| 179 | View Code Duplication | if ($status === Http::STATUS_FORBIDDEN) { |
|
| 180 | $this->logger->info($target . ' refused to exchange a shared secret with you.', ['app' => 'federation']); |
||
| 181 | } else { |
||
| 182 | $this->logger->logException($e, ['app' => 'federation']); |
||
| 183 | } |
||
| 184 | } catch (\Exception $e) { |
||
| 185 | $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
||
| 186 | $this->logger->logException($e, ['app' => 'federation']); |
||
| 187 | } |
||
| 188 | |||
| 189 | // if we received a unexpected response we try again later |
||
| 190 | if ( |
||
| 191 | $status !== Http::STATUS_OK |
||
| 192 | && $status !== Http::STATUS_FORBIDDEN |
||
| 193 | ) { |
||
| 194 | $this->retainJob = true; |
||
| 195 | } else { |
||
| 196 | // reset token if we received a valid response |
||
| 197 | $this->dbHandler->addToken($target, ''); |
||
| 198 | } |
||
| 199 | |||
| 200 | if ($status === Http::STATUS_OK && $result instanceof IResponse) { |
||
| 201 | $body = $result->getBody(); |
||
| 202 | $result = json_decode($body, true); |
||
| 203 | if (isset($result['ocs']['data']['sharedSecret'])) { |
||
| 204 | $this->trustedServers->addSharedSecret( |
||
| 205 | $target, |
||
| 206 | $result['ocs']['data']['sharedSecret'] |
||
| 207 | ); |
||
| 208 | } else { |
||
| 209 | $this->logger->error( |
||
| 210 | 'remote server "' . $target . '"" does not return a valid shared secret', |
||
| 211 | ['app' => 'federation'] |
||
| 212 | ); |
||
| 213 | $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | } |
||
| 218 | } |
||
| 219 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.