| Conditions | 13 |
| Paths | 218 |
| Total Lines | 80 |
| Code Lines | 56 |
| 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 |
||
| 140 | protected function run($argument) { |
||
| 141 | $target = $argument['url']; |
||
| 142 | $created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime(); |
||
| 143 | $currentTime = $this->time->getTime(); |
||
| 144 | $source = $this->urlGenerator->getAbsoluteURL('/'); |
||
| 145 | $source = rtrim($source, '/'); |
||
| 146 | $token = $argument['token']; |
||
| 147 | |||
| 148 | // kill job after 30 days of trying |
||
| 149 | $deadline = $currentTime - $this->maxLifespan; |
||
| 150 | if ($created < $deadline) { |
||
| 151 | $this->retainJob = false; |
||
| 152 | $this->trustedServers->setServerStatus($target,TrustedServers::STATUS_FAILURE); |
||
| 153 | return; |
||
| 154 | } |
||
| 155 | |||
| 156 | $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); |
||
| 157 | $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
||
| 158 | |||
| 159 | // make sure that we have a well formatted url |
||
| 160 | $url = rtrim($target, '/') . '/' . trim($endPoint, '/'); |
||
| 161 | |||
| 162 | $result = null; |
||
|
|
|||
| 163 | try { |
||
| 164 | $result = $this->httpClient->get( |
||
| 165 | $url, |
||
| 166 | [ |
||
| 167 | 'query' => |
||
| 168 | [ |
||
| 169 | 'url' => $source, |
||
| 170 | 'token' => $token, |
||
| 171 | 'format' => 'json', |
||
| 172 | ], |
||
| 173 | 'timeout' => 3, |
||
| 174 | 'connect_timeout' => 3, |
||
| 175 | ] |
||
| 176 | ); |
||
| 177 | |||
| 178 | $status = $result->getStatusCode(); |
||
| 179 | } catch (ClientException $e) { |
||
| 180 | $status = $e->getCode(); |
||
| 181 | if ($status === Http::STATUS_FORBIDDEN) { |
||
| 182 | $this->logger->info($target . ' refused to exchange a shared secret with you.', ['app' => 'federation']); |
||
| 183 | } else { |
||
| 184 | $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
||
| 185 | } |
||
| 186 | } catch (RequestException $e) { |
||
| 187 | $status = -1; // There is no status code if we could not connect |
||
| 188 | $this->logger->logException($e, [ |
||
| 189 | 'message' => 'Could not connect to ' . $target, |
||
| 190 | 'level' => ILogger::INFO, |
||
| 191 | 'app' => 'federation', |
||
| 192 | ]); |
||
| 193 | } catch (\Throwable $e) { |
||
| 194 | $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
||
| 195 | $this->logger->logException($e, ['app' => 'federation']); |
||
| 196 | } |
||
| 197 | |||
| 198 | // if we received a unexpected response we try again later |
||
| 199 | if ( |
||
| 200 | $status !== Http::STATUS_OK |
||
| 201 | && $status !== Http::STATUS_FORBIDDEN |
||
| 202 | ) { |
||
| 203 | $this->retainJob = true; |
||
| 204 | } |
||
| 205 | |||
| 206 | if ($status === Http::STATUS_OK && $result instanceof IResponse) { |
||
| 207 | $body = $result->getBody(); |
||
| 208 | $result = json_decode($body, true); |
||
| 209 | if (isset($result['ocs']['data']['sharedSecret'])) { |
||
| 210 | $this->trustedServers->addSharedSecret( |
||
| 211 | $target, |
||
| 212 | $result['ocs']['data']['sharedSecret'] |
||
| 213 | ); |
||
| 214 | } else { |
||
| 215 | $this->logger->error( |
||
| 216 | 'remote server "' . $target . '"" does not return a valid shared secret. Received data: ' . $body, |
||
| 217 | ['app' => 'federation'] |
||
| 218 | ); |
||
| 219 | $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
||
| 220 | } |
||
| 243 |