| Conditions | 10 |
| Paths | 114 |
| Total Lines | 64 |
| Code Lines | 39 |
| Lines | 10 |
| Ratio | 15.63 % |
| 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 |
||
| 150 | protected function run($argument) { |
||
| 151 | |||
| 152 | $target = $argument['url']; |
||
| 153 | $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
||
| 154 | $currentTime = $this->timeFactory->getTime(); |
||
| 155 | $source = $this->urlGenerator->getAbsoluteURL('/'); |
||
| 156 | $source = rtrim($source, '/'); |
||
| 157 | $token = $argument['token']; |
||
| 158 | |||
| 159 | // kill job after 30 days of trying |
||
| 160 | $deadline = $currentTime - $this->maxLifespan; |
||
| 161 | View Code Duplication | if ($created < $deadline) { |
|
| 162 | $this->retainJob = false; |
||
| 163 | $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
||
| 164 | return; |
||
| 165 | } |
||
| 166 | |||
| 167 | $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); |
||
| 168 | $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
||
| 169 | |||
| 170 | // make sure that we have a well formated url |
||
| 171 | $url = rtrim($target, '/') . '/' . trim($endPoint, '/') . $this->format; |
||
| 172 | |||
| 173 | try { |
||
| 174 | $result = $this->httpClient->post( |
||
| 175 | $url, |
||
| 176 | [ |
||
| 177 | 'body' => [ |
||
| 178 | 'url' => $source, |
||
| 179 | 'token' => $token, |
||
| 180 | ], |
||
| 181 | 'timeout' => 3, |
||
| 182 | 'connect_timeout' => 3, |
||
| 183 | ] |
||
| 184 | ); |
||
| 185 | |||
| 186 | $status = $result->getStatusCode(); |
||
| 187 | |||
| 188 | } catch (ClientException $e) { |
||
| 189 | $status = $e->getCode(); |
||
| 190 | View Code Duplication | if ($status === Http::STATUS_FORBIDDEN) { |
|
| 191 | $this->logger->info($target . ' refused to ask for a shared secret.', ['app' => 'federation']); |
||
| 192 | } else { |
||
| 193 | $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
||
| 194 | } |
||
| 195 | } catch (\Exception $e) { |
||
| 196 | $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
||
| 197 | $this->logger->logException($e, ['app' => 'federation']); |
||
| 198 | } |
||
| 199 | |||
| 200 | // if we received a unexpected response we try again later |
||
| 201 | if ( |
||
| 202 | $status !== Http::STATUS_OK |
||
| 203 | && $status !== Http::STATUS_FORBIDDEN |
||
| 204 | ) { |
||
| 205 | $this->retainJob = true; |
||
| 206 | } |
||
| 207 | |||
| 208 | if ($status === Http::STATUS_FORBIDDEN) { |
||
| 209 | // clear token if remote server refuses to ask for shared secret |
||
| 210 | $this->dbHandler->addToken($target, ''); |
||
| 211 | } |
||
| 212 | |||
| 213 | } |
||
| 214 | |||
| 235 |
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):