Completed
Push — master ( 68e0a8...c4fab6 )
by Alireza
02:23
created

ServerAddCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Add 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\InputOption;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class ServerAddCommand extends Command
19
{
20
21
    /**
22
     * configure command
23
     *
24
     * @return void
25
     */
26
    public function configure()
27
    {
28
        $this->setName('server:add')
29
            ->setDescription('Add server');
30
31
        $this->addOption('name', 'e', InputOption::VALUE_REQUIRED, 'Server name');
32
33
        $this->addOption('ip', 'i',  InputOption::VALUE_REQUIRED, 'Server ip address');
34
    }
35
36
    /**
37
     * execute command
38
     *
39
     * @param InputInterface $input
40
     * @param OutputInterface $output
41
     */
42
    protected function execute(InputInterface $input , OutputInterface $output)
43
    {
44
        $command = new Style($input, $output);
45
46 View Code Duplication
        if (! file_exists($file = $this->getHomeDir() . "/.Josh/servers.json")) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
48
            if (! is_dir($dir = $this->getHomeDir() . "/.Josh")) {
49
                mkdir($dir,0777);
50
            }
51
52
            touch($file);
53
            chmod($file,0777);
54
            file_put_contents($file,json_encode([]));
55
        }
56
57
        $servers = json_decode(file_get_contents($file), true);
58
59
        $ip = $input->getOption('ip');
60
        $name = $input->getOption('name');
61
62
        foreach ($servers as $server){
63
           if ( $server[1] == $name ) {
64
               $command->error("Server {$name} already exists.");
65
               exit;
66
           }
67
68
            if ( $server[2] == $ip ) {
69
                $command->error("Server IP {$ip} already exists.");
70
                exit;
71
            }
72
        }
73
74
        if(count($servers) == 0){
75
            $id = 1;
76
        } else {
77
            $id = array_reverse($servers)[0][0] + 1;
78
        }
79
80
        var_dump($id);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($id); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
81
82
        $servers[] = [ $id, $name, $ip, null ];
83
84
        file_put_contents($file,json_encode($servers));
85
86
        $command->info("Server {$name} added successfully.");
87
        exit;
88
    }
89
90
    /** Get home directory
91
     *
92
     * @return array
93
     */
94
    public function getHomeDir()
95
    {
96
        if(empty($_SERVER['HOME'])){
97
            return posix_getpwuid(posix_getuid());
98
        }
99
100
        return $_SERVER['HOME'];
101
    }
102
}
103