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

ServerListCommand::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
 * Server list
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\Helper\Table;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class ServerListCommand extends Command
19
{
20
21
    /**
22
     * configure command
23
     *
24
     * @return void
25
     */
26
    public function configure()
27
    {
28
        $this->setName('server:list')
29
            ->setDescription('servers list');
30
    }
31
32
    /**
33
     * execute command
34
     *
35
     * @param InputInterface $input
36
     * @param OutputInterface $output
37
     */
38
    protected function execute(InputInterface $input , OutputInterface $output)
39
    {
40
        $command = new Style($input, $output);
41
42 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...
43
44
            if (! is_dir($dir = package_path())) {
45
                mkdir($dir,0777);
46
            }
47
48
            touch($file);
49
            chmod($file,0777);
50
            file_put_contents($file,json_encode([]));
51
        }
52
53
        $servers = json_decode(file_get_contents($file), true);
54
55
        if (count($servers) == 0) {
56
57
            $command->line("No server added to the list. use [ server:add ] to add one.");
58
        } else {
59
60
            $table = new Table($output);
61
62
            $table->setHeaders(['Id', 'Name', 'Ip address', 'Last connect'])
63
                ->setRows($servers)
64
                ->render();
65
        }
66
    }
67
}
68