Completed
Push — master ( c4fab6...faae25 )
by Alireza
03:17
created

ServerSSHCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 70
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
B execute() 0 34 6
A getHomeDir() 0 8 2
1
<?php
2
3
/**
4
 * Ssh to server
5
 *
6
 * @author Alireza Josheghani <[email protected]>
7
 * @since  29 Sep 2018
8
 */
9
10
namespace Josh\Console\Commands;
11
12
use Josh\Console\ConsoleStyle as Style;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class ServerSSHCommand extends Command
19
{
20
21
    /**
22
     * configure command
23
     *
24
     * @return void
25
     */
26
    public function configure()
27
    {
28
        $this->setName('server:ssh')
29
            ->setDescription('ssh to the server');
30
31
        $this->addArgument('server', InputArgument::REQUIRED, "Server id or name");
32
    }
33
34
    /**
35
     * execute command
36
     *
37
     * @param InputInterface $input
38
     * @param OutputInterface $output
39
     */
40
    protected function execute(InputInterface $input , OutputInterface $output)
41
    {
42
        $command = new Style($input, $output);
43
44
        if(file_exists($file = $this->getHomeDir() . "/.Josh/servers.json")){
45
46
            $servers = json_decode(file_get_contents($file), true);
47
            $serverIdOrName = $input->getArgument('server');
48
49
            if (count($servers) == 0) {
50
51
                $command->line("No server added to the list. use [ server:add ] to add one.");
52
            }
53
54
            $currentServer = [];
55
56
            foreach ($servers as $server){
57
58
                if ($server[0] == $serverIdOrName){
59
                    $currentServer = $server;
60
                } else if ($server[1] == $serverIdOrName){
61
                    $currentServer = $server;
62
                }
63
            }
64
65
            $command->info("Connecting to [$currentServer[1]]...");
66
67
            system("ssh root@$currentServer[2]");
68
69
        } else {
70
71
            $command->line("No server added to the list. use [ server:add ] to add one.");
72
        }
73
    }
74
75
    /** Get home directory
76
     *
77
     * @return array
78
     */
79
    public function getHomeDir()
80
    {
81
        if(empty($_SERVER['HOME'])){
82
            return posix_getpwuid(posix_getuid());
83
        }
84
85
        return $_SERVER['HOME'];
86
    }
87
}
88