| Conditions | 8 |
| Paths | 34 |
| Total Lines | 76 |
| Code Lines | 46 |
| 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 |
||
| 47 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 48 | { |
||
| 49 | $this->initConfiguration($input->getOption('configFile')); |
||
| 50 | |||
| 51 | if ($this->isInitialized()) { |
||
| 52 | $output->writeln('<info>System is already initialized.</info>'); |
||
| 53 | return Command::SUCCESS; |
||
| 54 | } |
||
| 55 | |||
| 56 | $serverName = $this->getServerName($input, $output); |
||
| 57 | |||
| 58 | $configFile = $this->getConfigFile(); |
||
| 59 | $serverId = $this->createServerId(); |
||
| 60 | |||
| 61 | $userId = $input->getArgument('userId'); |
||
| 62 | |||
| 63 | $client = new Client(); |
||
| 64 | |||
| 65 | $payload = [ |
||
| 66 | 'userId' => $userId, |
||
| 67 | 'serverId' => $serverId, |
||
| 68 | 'serverName' => $serverName |
||
| 69 | ]; |
||
| 70 | |||
| 71 | try { |
||
| 72 | $response = $client->post($this->getPreparedEndpoint($serverId, $input->getOption('serverApi')), [ |
||
| 73 | RequestOptions::JSON => $payload |
||
| 74 | ]); |
||
| 75 | } catch (ClientException $exception) { |
||
| 76 | $result = json_decode((string)$exception->getResponse()->getBody(), true); |
||
| 77 | $output->writeln('<error>Unable to initialize: ' . $result['message'] . '</error>'); |
||
| 78 | return Command::FAILURE; |
||
| 79 | } |
||
| 80 | |||
| 81 | $result = json_decode((string)$response->getBody(), true); |
||
| 82 | |||
| 83 | $config = [ |
||
| 84 | 'serverId' => $serverId, |
||
| 85 | 'userId' => $userId, |
||
| 86 | 'remote' => false, |
||
| 87 | 'smartCare' => false, |
||
| 88 | 'metrics' => false, |
||
| 89 | 'commands' => $this->config->getCommands(false), |
||
| 90 | 'secret' => $result['data']['secret'] |
||
| 91 | ]; |
||
| 92 | |||
| 93 | if (!file_exists(dirname($configFile))) { |
||
| 94 | mkdir(dirname($configFile), 0777, true); |
||
| 95 | } |
||
| 96 | |||
| 97 | file_put_contents($configFile, json_encode($config), JSON_PRETTY_PRINT); |
||
| 98 | |||
| 99 | $output->writeln('<info>Server registered.</info>'); |
||
| 100 | |||
| 101 | $array = [ |
||
| 102 | 'command' => 'config' |
||
| 103 | ]; |
||
| 104 | |||
| 105 | if ($input->getOption('remote')) { |
||
| 106 | $array['--remote'] = $input->getOption('remote'); |
||
| 107 | } |
||
| 108 | if ($input->getOption('metrics')) { |
||
| 109 | $array['--metrics'] = $input->getOption('metrics'); |
||
| 110 | } |
||
| 111 | if ($input->getOption('smartCare')) { |
||
| 112 | $array['--smartCare'] = $input->getOption('smartCare'); |
||
| 113 | } |
||
| 114 | if ($input->getOption('serverApi')) { |
||
| 115 | $array['--serverApi'] = $input->getOption('serverApi'); |
||
| 116 | } |
||
| 117 | |||
| 118 | $newInput = new ArrayInput($array); |
||
| 119 | |||
| 120 | $this->getApplication()->find('config')->run($newInput, $output); |
||
| 121 | |||
| 122 | return Command::SUCCESS; |
||
| 123 | } |
||
| 177 |