Complex classes like PublishSSH often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PublishSSH, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 18 | class PublishSSH extends Command  | 
            ||
| 19 | { | 
            ||
| 20 | use PossibleEmails, ChecksSSHConnection, ChecksEnv, DiesIfEnvVariableIsnotInstalled;  | 
            ||
| 21 | |||
| 22 | /**  | 
            ||
| 23 | * SSH_ID_RSA_PRIV  | 
            ||
| 24 | */  | 
            ||
| 25 | const SSH_ID_RSA_PRIV = '/.ssh/id_rsa';  | 
            ||
| 26 | |||
| 27 | /**  | 
            ||
| 28 | * SSH_ID_RSA_PUB  | 
            ||
| 29 | */  | 
            ||
| 30 | const SSH_ID_RSA_PUB = '/.ssh/id_rsa.pub';  | 
            ||
| 31 | |||
| 32 | /**  | 
            ||
| 33 | * USR_BIN_SSH  | 
            ||
| 34 | */  | 
            ||
| 35 | const USR_BIN_SSH = '/usr/bin/ssh';  | 
            ||
| 36 | |||
| 37 | /**  | 
            ||
| 38 | * Servers.  | 
            ||
| 39 | *  | 
            ||
| 40 | * @var array  | 
            ||
| 41 | */  | 
            ||
| 42 | protected $servers;  | 
            ||
| 43 | |||
| 44 | /**  | 
            ||
| 45 | * Server.  | 
            ||
| 46 | *  | 
            ||
| 47 | * @var array  | 
            ||
| 48 | */  | 
            ||
| 49 | protected $server;  | 
            ||
| 50 | |||
| 51 | /**  | 
            ||
| 52 | * Server name.  | 
            ||
| 53 | *  | 
            ||
| 54 | * @var array  | 
            ||
| 55 | */  | 
            ||
| 56 | protected $server_name;  | 
            ||
| 57 | |||
| 58 | /**  | 
            ||
| 59 | * Server names.  | 
            ||
| 60 | *  | 
            ||
| 61 | * @var array  | 
            ||
| 62 | */  | 
            ||
| 63 | protected $server_names;  | 
            ||
| 64 | |||
| 65 | /**  | 
            ||
| 66 | *API endpint URL  | 
            ||
| 67 | *  | 
            ||
| 68 | * @var String  | 
            ||
| 69 | */  | 
            ||
| 70 | protected $url;  | 
            ||
| 71 | |||
| 72 | /**  | 
            ||
| 73 | * The name and signature of the console command.  | 
            ||
| 74 | *  | 
            ||
| 75 | * @var string  | 
            ||
| 76 | */  | 
            ||
| 77 |     protected $signature = 'publish:ssh {email?} {server_name?} {ip?}'; | 
            ||
| 78 | |||
| 79 | /**  | 
            ||
| 80 | * The console command description.  | 
            ||
| 81 | *  | 
            ||
| 82 | * @var string  | 
            ||
| 83 | */  | 
            ||
| 84 | protected $description = 'Add ssh configuration and publish SSH keys to Laravel Forge server';  | 
            ||
| 85 | |||
| 86 | /**  | 
            ||
| 87 | * Guzzle http client.  | 
            ||
| 88 | *  | 
            ||
| 89 | * @var Client  | 
            ||
| 90 | */  | 
            ||
| 91 | protected $http;  | 
            ||
| 92 | |||
| 93 | /**  | 
            ||
| 94 | * Create a new command instance.  | 
            ||
| 95 | *  | 
            ||
| 96 | */  | 
            ||
| 97 | public function __construct(Client $http)  | 
            ||
| 102 | |||
| 103 | /**  | 
            ||
| 104 | * Execute the console command.  | 
            ||
| 105 | *  | 
            ||
| 106 | */  | 
            ||
| 107 | public function handle()  | 
            ||
| 124 | |||
| 125 | /**  | 
            ||
| 126 | * Check for SSH client or install it if missing.  | 
            ||
| 127 | */  | 
            ||
| 128 | protected function checkForSSHClientOrInstall()  | 
            ||
| 137 | |||
| 138 | /**  | 
            ||
| 139 | * Check for SSH client or install it if missing.  | 
            ||
| 140 | */  | 
            ||
| 141 | protected function checkForSSHKeysOrCreate()  | 
            ||
| 150 | |||
| 151 | protected function obtainServersInfo()  | 
            ||
| 152 |     { | 
            ||
| 153 | $this->obtainServers();  | 
            ||
| 154 | $this->obtainServer();  | 
            ||
| 155 | }  | 
            ||
| 156 | |||
| 157 | /**  | 
            ||
| 158 | * Obtain servers.  | 
            ||
| 159 | */  | 
            ||
| 160 | protected function obtainServers()  | 
            ||
| 161 |     { | 
            ||
| 162 | $this->servers = $this->fetchServers();  | 
            ||
| 163 |         $this->server_names = collect($this->servers)->pluck('name')->toArray(); | 
            ||
| 164 |         if (empty($this->server_names)) { | 
            ||
| 165 |             $this->error('No valid servers assigned to user. Skipping SSH key installation on server...'); | 
            ||
| 166 | die();  | 
            ||
| 167 | }  | 
            ||
| 168 | }  | 
            ||
| 169 | |||
| 170 | /**  | 
            ||
| 171 | * Obtain server.  | 
            ||
| 172 | */  | 
            ||
| 173 | protected function obtainServer()  | 
            ||
| 174 |     { | 
            ||
| 175 |         if (! $this->server) { | 
            ||
| 176 |             if ($this->argument('server_name')) { | 
            ||
| 177 |                 $this->server = $this->getForgeIdServer($this->servers, $this->argument('server_name')); | 
            ||
| 178 |                 if (!$this->server) { | 
            ||
| 179 |                     $this->error('No server name found on servers assigned to user!'); | 
            ||
| 180 | die();  | 
            ||
| 181 | }  | 
            ||
| 182 |                 $this->server_name = $this->argument('server_name'); | 
            ||
| 183 |             } else { | 
            ||
| 184 |                 if (fp_env('ACACHA_FORGE_SERVER')) { | 
            ||
| 185 |                     $this->server = fp_env('ACACHA_FORGE_SERVER'); | 
            ||
| 186 |                     if (fp_env('ACACHA_FORGE_SERVER_NAME')) { | 
            ||
| 187 |                         $this->server_name = fp_env('ACACHA_FORGE_SERVER_NAME'); | 
            ||
| 188 |                     } else { | 
            ||
| 189 | $this->server_name = $this->getForgeName($this->servers, $this->server);  | 
            ||
| 190 | }  | 
            ||
| 191 |                 } else { | 
            ||
| 192 |                     $this->server_name = $this->choice('Forge server?', $this->server_names, 0); | 
            ||
| 193 | $this->server = $this->getForgeIdServer($this->servers, $this->server_name);  | 
            ||
| 194 | }  | 
            ||
| 195 | }  | 
            ||
| 196 | }  | 
            ||
| 197 | |||
| 198 | $this->checkServer($this->server);  | 
            ||
| 199 | $this->checkServerName($this->server_name);  | 
            ||
| 200 | }  | 
            ||
| 201 | |||
| 202 | /**  | 
            ||
| 203 | * Get ENDPOINT API URL.  | 
            ||
| 204 | *  | 
            ||
| 205 | * @return string  | 
            ||
| 206 | */  | 
            ||
| 207 | protected function endPointAPIURL()  | 
            ||
| 208 |     { | 
            ||
| 209 |         $uri = str_replace('{forgeserver}', $this->server, config('forge-publish.post_ssh_keys_uri')); | 
            ||
| 210 |         return config('forge-publish.url') . $uri; | 
            ||
| 211 | }  | 
            ||
| 212 | |||
| 213 | /**  | 
            ||
| 214 | * Install ssh key on server.  | 
            ||
| 215 | */  | 
            ||
| 216 | protected function installSSHKeyOnServer()  | 
            ||
| 217 |     { | 
            ||
| 218 |         $this->info('Adding SSH key to Laravel Forge server'); | 
            ||
| 219 | // We cannot use ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] because SSH acces via user/password is not enabled on Laravel Forge  | 
            ||
| 220 | // We need to use the Laravel Forge API to add a key  | 
            ||
| 221 | |||
| 222 | $this->url = $this->endPointAPIURL();  | 
            ||
| 223 | |||
| 224 | $response = $this->postKeyToLaravelForgeServer($keyName = $this->getUniqueKeyNameFromEmail());  | 
            ||
| 225 | |||
| 226 | $result = json_decode($contents = $response->getBody()->getContents());  | 
            ||
| 227 | |||
| 228 |         if (! isset($result->status)) { | 
            ||
| 229 |             $this->error("An error has been succeded: $contents"); | 
            ||
| 230 | die();  | 
            ||
| 231 | }  | 
            ||
| 232 |         if ($result->status == 'installed') { | 
            ||
| 233 |             $this->info("The SSH Key ($keyName) has been correctly installed in Laravel Forge Server " . $this->server_name . ' (' . $this->server . ')'); | 
            ||
| 234 | }  | 
            ||
| 235 | }  | 
            ||
| 236 | |||
| 237 | /**  | 
            ||
| 238 | * Post key to Laravel Forge Server.  | 
            ||
| 239 | */  | 
            ||
| 240 | protected function postKeyToLaravelForgeServer($keyName=null)  | 
            ||
| 256 | |||
| 257 | /**  | 
            ||
| 258 | * Abort command execution?  | 
            ||
| 259 | */  | 
            ||
| 260 | protected function abortCommandExecution()  | 
            ||
| 264 | |||
| 265 | /**  | 
            ||
| 266 | * Get unique key name from email.  | 
            ||
| 267 | */  | 
            ||
| 268 | protected function getUniqueKeyNameFromEmail()  | 
            ||
| 272 | |||
| 273 | /**  | 
            ||
| 274 | * Append ssh config.  | 
            ||
| 275 | */  | 
            ||
| 276 | protected function appendSSHConfig()  | 
            ||
| 277 |     { | 
            ||
| 299 | |||
| 300 | /**  | 
            ||
| 301 | * IP address.  | 
            ||
| 302 | *  | 
            ||
| 303 | * @return array|string  | 
            ||
| 304 | */  | 
            ||
| 305 | protected function ip_address()  | 
            ||
| 316 | |||
| 317 | /**  | 
            ||
| 318 | * Validate ip address.  | 
            ||
| 319 | *  | 
            ||
| 320 | * @param $ip_address  | 
            ||
| 321 | */  | 
            ||
| 322 | protected function validateIpAddress($ip_address)  | 
            ||
| 333 | |||
| 334 | /**  | 
            ||
| 335 | * Test SSH connection.  | 
            ||
| 336 | */  | 
            ||
| 337 | protected function testSSHConnection()  | 
            ||
| 346 | |||
| 347 | /**  | 
            ||
| 348 | * Create SSH Keys.  | 
            ||
| 349 | */  | 
            ||
| 350 | protected function createSSHKeys()  | 
            ||
| 356 | |||
| 357 | /**  | 
            ||
| 358 | * Get email.  | 
            ||
| 359 | *  | 
            ||
| 360 | * @return string  | 
            ||
| 361 | */  | 
            ||
| 362 | protected function getEmail()  | 
            ||
| 366 | |||
| 367 | /**  | 
            ||
| 368 | * Install ssh client.  | 
            ||
| 369 | */  | 
            ||
| 370 | protected function installSshClient()  | 
            ||
| 375 | }  | 
            ||
| 376 | 
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: