ServerAddCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 8
lcom 2
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 9 1
B execute() 0 35 7
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 Josh\Console\Models\Server;
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 ServerCommand
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
        $servers = $this->model->all();
46
47
        $ip = $input->getOption('ip');
48
        $name = $input->getOption('name');
49
50
        if (empty($name) || empty($ip)){
51
52
            $command->error("Server name and ip address is required. use [server:add --ip=***.***.*** --name=example]");
53
        } else {
54
55
            /** @var Server $server */
56
            foreach ($servers as $server){
57
                if ( $server->getAttribute("name") == $name ) {
58
                    $command->error("Server {$name} already exists.");
59
                    exit;
60
                }
61
62
                if ( $server->getAttribute("ip") == $ip ) {
63
                    $command->error("Server IP {$ip} already exists.");
64
                    exit;
65
                }
66
            }
67
68
            $id = ( $servers->count() == 0 ? 1 : array_reverse($servers->toArray())[0]->id + 1 );
69
70
            $this->model->create(compact("id", "ip", "name"));
71
72
            $command->info("Server {$name} added successfully.");
73
        }
74
        exit;
75
    }
76
}
77