ListCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 31
dl 0
loc 87
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDomains() 0 22 4
A getOptions() 0 5 1
A getRows() 0 15 3
A handle() 0 9 1
1
<?php
2
3
namespace Salah3id\Domains\Commands;
4
5
use Illuminate\Console\Command;
6
use Salah3id\Domains\Domain;
7
use Symfony\Component\Console\Input\InputOption;
8
9
class ListCommand extends Command
10
{
11
    /**
12
     * The console command name.
13
     *
14
     * @var string
15
     */
16
    protected $name = 'domain:list';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Show list of all domains.';
24
25
    /**
26
     * Execute the console command.
27
     */
28
    public function handle(): int
29
    {
30
        $this->components->twoColumnDetail('<fg=gray>Status / Name</>', '<fg=gray>Path / priority</>');
31
        collect($this->getRows())->each(function ($row) {
0 ignored issues
show
Bug introduced by
$this->getRows() of type array|array<mixed,array<integer,mixed|string>> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
        collect(/** @scrutinizer ignore-type */ $this->getRows())->each(function ($row) {
Loading history...
32
33
            $this->components->twoColumnDetail("[{$row[1]}] {$row[0]}", "{$row[3]} [{$row[2]}]");
34
        });
35
36
        return 0;
37
    }
38
39
    /**
40
     * Get table rows.
41
     *
42
     * @return array
43
     */
44
    public function getRows()
45
    {
46
        $rows = [];
47
48
        /** @var Domain $domain */
49
        foreach ($this->getDomains() as $domain) {
50
            $rows[] = [
51
                $domain->getName(),
52
                $domain->isEnabled() ? '<fg=green>Enabled</>' : '<fg=red>Disabled</>',
53
                $domain->get('priority'),
54
                $domain->getPath(),
55
            ];
56
        }
57
58
        return $rows;
59
    }
60
61
    public function getDomains()
62
    {
63
        switch ($this->option('only')) {
64
            case 'enabled':
65
                return $this->laravel['domains']->getByStatus(1);
66
67
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
68
69
            case 'disabled':
70
                return $this->laravel['domains']->getByStatus(0);
71
72
                break;
73
74
            case 'priority':
75
                return $this->laravel['domains']->getPriority($this->option('direction'));
76
77
                break;
78
79
            default:
80
                return $this->laravel['domains']->all();
81
82
                break;
83
        }
84
    }
85
86
    /**
87
     * Get the console command options.
88
     *
89
     * @return array
90
     */
91
    protected function getOptions()
92
    {
93
        return [
94
            ['only', 'o', InputOption::VALUE_OPTIONAL, 'Types of domains will be displayed.', null],
95
            ['direction', 'd', InputOption::VALUE_OPTIONAL, 'The direction of ordering.', 'asc'],
96
        ];
97
    }
98
}
99