| Conditions | 8 |
| Paths | 6 |
| Total Lines | 65 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 9 | ||
| 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 | $type = $commandResult['data']['command']['type']; |
||
| 46 | |||
| 47 | if ($type === 'smartCare' && !$smartCareEnabled) { |
||
| 48 | $commandOutput = [ |
||
| 49 | "output" => '', |
||
| 50 | 'error' => 'SmartCare is not activated on this server', |
||
| 51 | 'actualCommand' => '<unknown>', |
||
| 52 | 'exitCode' => Command::FAILURE |
||
| 53 | ]; |
||
| 54 | } elseif ($type === 'remote' && !$remoteEnabled) { |
||
| 55 | $commandOutput = [ |
||
| 56 | "output" => '', |
||
| 57 | 'error' => 'Remote commands are not enabled on this server', |
||
| 58 | 'actualCommand' => '<unknown>', |
||
| 59 | 'exitCode' => Command::FAILURE |
||
| 60 | ]; |
||
| 61 | } else { |
||
| 62 | if ($type === 'remote') { |
||
| 63 | |||
| 64 | $commandId = $commandResult['data']['command']['command']; |
||
| 65 | |||
| 66 | $expectedProof = md5($commandId . $this->secret); |
||
| 67 | $cloudProof = $commandResult['data']['command']['proof']; |
||
| 68 | |||
| 69 | if ($expectedProof !== $cloudProof) { |
||
| 70 | $cloudCommand = $commandResult['data']['command']['storedCommand']['command']; |
||
| 71 | $commandOutput = $this->runCommand($commandId, $cloudCommand); |
||
| 72 | } else { |
||
| 73 | $commandOutput = [ |
||
| 74 | "output" => '', |
||
| 75 | 'error' => 'The authenticity of the job could not be verified.', |
||
| 76 | 'actualCommand' => '<unknown>', |
||
| 77 | 'exitCode' => Command::FAILURE |
||
| 78 | ]; |
||
| 79 | } |
||
| 80 | } else { |
||
| 81 | $commandOutput = $this->runSmartCareCommand($commandResult['data']); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | $sendUrl = str_replace('{commandId}', $identifier, self::URL_SEND_OUTPUT); |
||
| 86 | |||
| 87 | $client->post($this->inventorioServer . $sendUrl, [ |
||
| 88 | RequestOptions::JSON => ['output' => $commandOutput] |
||
| 89 | ]); |
||
| 90 | |||
| 91 | return 'Command: ' . $commandOutput['actualCommand']; |
||
| 92 | } |
||
| 93 | |||
| 94 | return ""; |
||
| 95 | } |
||
| 179 | } |