Completed
Pull Request — master (#4)
by
unknown
02:16
created

SystemCheckCommand::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Arrilot\SystemCheck\Console;
4
5
use Arrilot\SystemCheck\ChecksCollection;
6
use Illuminate\Console\Command;
7
use InvalidArgumentException;
8
9
class SystemCheckCommand extends Command
10
{
11
    /**
12
     * The console command name.
13
     *
14
     * @var string
15
     */
16
    protected $name = 'system:check';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Check server and laravel configuration';
24
25
    /**
26
     * The collection of all available checks.
27
     *
28
     * @var ChecksCollection
29
     */
30
    protected $checks;
31
32
    /**
33
     * The table headers for the command.
34
     *
35
     * @var array
36
     */
37
    protected $headers = ['Check', 'Result', 'Comment'];
38
39
    /**
40
     * Constructor.
41
     *
42
     * @param ChecksCollection $checks
43
     */
44
    public function __construct(ChecksCollection $checks)
45
    {
46
        parent::__construct();
47
48
        $this->checks = $checks;
49
    }
50
51
    /**
52
     * Execute the console command.
53
     *
54
     * @return void
55
     */
56
    public function fire()
57
    {
58
        $env = is_null($this->option('env'))
59
            ? $this->laravel->environment()
60
            : $this->option('env');
61
62
        $this->output->writeln("<info>Performing checks for environment:</info> <comment>{$env}</comment>");
63
        $this->output->newLine();
64
65
        $this->performServerChecks($env);
66
        $this->performLaravelChecks($env);
67
    }
68
69
    /**
70
     * laravel 5.* support
71
     */
72
    public function handle() {
73
        $this->fire();
74
    }
75
76
    /**
77
     * Perform server configuration checks.
78
     *
79
     * @param string $env
80
     *
81
     * @return void
82
     */
83
    protected function performServerChecks($env)
84
    {
85
        $this->info('Server configuration checks:');
86
87
        $this->performChecks($this->checks->getServerChecks($env));
88
    }
89
90
    /**
91
     * Perform laravel configuration checks.
92
     *
93
     * @param string $env
94
     *
95
     * @return void
96
     */
97
    protected function performLaravelChecks($env)
98
    {
99
        $this->info('Laravel configuration checks:');
100
101
        $this->performChecks($this->checks->getLaravelChecks($env));
102
    }
103
104
    /**
105
     * Perform configuration checks.
106
     *
107
     * @param array $checks
108
     *
109
     * @return void
110
     */
111
    protected function performChecks($checks)
112
    {
113
        $rows = [];
114
115
        foreach ($checks as $check) {
116
            if ($row = $this->performCheck($check)) {
117
                $rows[] = $row;
118
            }
119
        }
120
121
        $this->displayRows($rows);
122
    }
123
124
    /**
125
     * Build a check object and perform a check.
126
     *
127
     * @param string $class
128
     *
129
     * @return array
130
     */
131
    protected function performCheck($class)
132
    {
133
        $check = new $class($this->laravel);
134
        $result = $check->perform();
135
136
        if ($result->status == 'Skip') {
137
            return [];
138
        }
139
140
        return [
141
            'Check'   => $check->getDescription(),
142
            'Result'  => $this->styleStatus($result->status),
143
            'Comment' => $result->comment,
144
        ];
145
    }
146
147
    /**
148
     * Display all given rows as a table.
149
     *
150
     * @param array $rows
151
     */
152
    protected function displayRows(array $rows)
153
    {
154
        $this->table($this->headers, $rows);
155
156
        $this->output->newLine();
157
    }
158
159
    /**
160
     * Add some styling to status.
161
     *
162
     * @param string $status
163
     *
164
     * @return string
165
     */
166
    protected function styleStatus($status)
167
    {
168
        if ($status === 'Ok') {
169
            return '<info>Ok</info>';
170
        }
171
172
        if ($status === 'Note') {
173
            return '<comment>Note</comment>';
174
        }
175
176
        if ($status === 'Fail') {
177
            return '<error>Fail</error>';
178
        }
179
180
        throw new InvalidArgumentException("Check can not return status '{$status}'");
181
    }
182
}
183