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