| Conditions | 8 |
| Paths | 6 |
| Total Lines | 64 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| 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 |
||
| 30 | public function run($remoteEnabled, $smartCareEnabled): string |
||
| 31 | { |
||
| 32 | $client = new Client(); |
||
| 33 | |||
| 34 | $popUrl = str_replace('{serverId}', $this->serverId, self::URL_POP_COMMAND); |
||
| 35 | $hasUrl = str_replace('{serverId}', $this->serverId, self::URL_HAS_COMMAND); |
||
| 36 | |||
| 37 | $response = $client->get($this->inventorioServer . $hasUrl); |
||
| 38 | $result = json_decode($response->getBody(), true); |
||
| 39 | |||
| 40 | if ($result['data']['hasQueued']) { |
||
| 41 | $commandResponse = $client->get($this->inventorioServer . $popUrl); |
||
| 42 | $commandResult = json_decode($commandResponse->getBody(), true); |
||
| 43 | |||
| 44 | $identifier = $commandResult['data']['command']['id']; |
||
| 45 | |||
| 46 | if ($commandResult['data']['command']['type'] === 'smartCare' && !$smartCareEnabled) { |
||
| 47 | $commandOutput = [ |
||
| 48 | "output" => '', |
||
| 49 | 'error' => 'SmartCare is not activated on this server', |
||
| 50 | 'actualCommand' => '<unknown>', |
||
| 51 | 'exitCode' => Command::FAILURE |
||
| 52 | ]; |
||
| 53 | } elseif ($commandResult['data']['command']['type'] === 'remote' && !$remoteEnabled) { |
||
| 54 | $commandOutput = [ |
||
| 55 | "output" => '', |
||
| 56 | 'error' => 'Remote commands are not enabled on this server', |
||
| 57 | 'actualCommand' => '<unknown>', |
||
| 58 | 'exitCode' => Command::FAILURE |
||
| 59 | ]; |
||
| 60 | } else { |
||
| 61 | if ($commandResult['type'] === 'remote') { |
||
| 62 | |||
| 63 | $commandId = $commandResult['data']['command']['command']; |
||
| 64 | |||
| 65 | $expectedProof = md5($commandId . $this->secret); |
||
| 66 | $cloudProof = $commandResult['data']['command']['proof']; |
||
| 67 | |||
| 68 | if ($expectedProof !== $cloudProof) { |
||
| 69 | $cloudCommand = $commandResult['data']['command']['storedCommand']['command']; |
||
| 70 | $commandOutput = $this->runCommand($commandId, $cloudCommand); |
||
| 71 | } else { |
||
| 72 | $commandOutput = [ |
||
| 73 | "output" => '', |
||
| 74 | 'error' => 'The authenticity of the job could not be verified.', |
||
| 75 | 'actualCommand' => '<unknown>', |
||
| 76 | 'exitCode' => Command::FAILURE |
||
| 77 | ]; |
||
| 78 | } |
||
| 79 | } else { |
||
| 80 | $commandOutput = $this->runSmartCareCommand($commandResult['data']); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | $sendUrl = str_replace('{commandId}', $identifier, self::URL_SEND_OUTPUT); |
||
| 85 | |||
| 86 | $client->post($this->inventorioServer . $sendUrl, [ |
||
| 87 | RequestOptions::JSON => ['output' => $commandOutput] |
||
| 88 | ]); |
||
| 89 | |||
| 90 | return 'Command: ' . $commandOutput['actualCommand']; |
||
| 91 | } |
||
| 92 | |||
| 93 | return ""; |
||
| 94 | } |
||
| 146 |