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

ServerAddCommand::getHomeDir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
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
     * configure command
22
     *
23
     * @return void
24
     */
25
    public function configure()
26
    {
27
        $this->setName('server:add')
28
            ->setDescription('Add server');
29
30
        $this->addOption('name', 'e', InputOption::VALUE_REQUIRED, 'Server name');
31
32
        $this->addOption('ip', 'i',  InputOption::VALUE_REQUIRED, 'Server ip address');
33
    }
34
35
    /**
36
     * execute command
37
     *
38
     * @param InputInterface $input
39
     * @param OutputInterface $output
40
     */
41
    protected function execute(InputInterface $input , OutputInterface $output)
42
    {
43
        $command = new Style($input, $output);
44
45 View Code Duplication
        if (! file_exists($file = package_path("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...
46
47
            if (! is_dir($dir = package_path())) {
48
                mkdir($dir,0777);
49
            }
50
51
            touch($file);
52
            chmod($file,0777);
53
            file_put_contents($file,json_encode([]));
54
        }
55
56
        $servers = json_decode(file_get_contents($file), true);
57
58
        $ip = $input->getOption('ip');
59
        $name = $input->getOption('name');
60
61
        if (empty($name) || empty($ip)){
62
63
            $command->error("Server name and ip address is required. use [server:add --ip=***.***.*** --name=example]");
64
        } else {
65
66
            foreach ($servers as $server){
67
                if ( $server[1] == $name ) {
68
                    $command->error("Server {$name} already exists.");
69
                    exit;
70
                }
71
72
                if ( $server[2] == $ip ) {
73
                    $command->error("Server IP {$ip} already exists.");
74
                    exit;
75
                }
76
            }
77
78
            $id = ( count($servers) == 0 ? 1 : array_reverse($servers)[0][0] + 1 );
79
80
            $servers[] = [ $id, $name, $ip, null ];
81
82
            file_put_contents($file,json_encode($servers));
83
84
            $command->info("Server {$name} added successfully.");
85
        }
86
        exit;
87
    }
88
}
89