Completed
Push — master ( 51c0ef...914e8e )
by Marcel
10:30 queued 11s
created

SelfDiagnosisCommand::handle()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 21
cp 0
rs 8.8817
c 0
b 0
f 0
cc 6
nc 12
nop 0
crap 42
1
<?php
2
3
namespace BeyondCode\SelfDiagnosis;
4
5
use Illuminate\Console\Command;
6
use BeyondCode\SelfDiagnosis\Checks\Check;
7
8
class SelfDiagnosisCommand extends Command
9
{
10
    /**
11
     * The console command name.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'self-diagnosis {environment?}';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Perform application self diagnosis.';
23
24
    private $messages = [];
25
26
    public function handle()
27
    {
28
        $this->runChecks(config('self-diagnosis.checks', []), trans('self-diagnosis::commands.self_diagnosis.common_checks'));
29
30
        $environment = $this->argument('environment') ?: app()->environment();
31
        $environmentChecks = config('self-diagnosis.environment_checks.' . $environment, []);
32
33
        if (empty($environmentChecks) && array_key_exists($environment, config('self-diagnosis.environment_aliases'))) {
34
            $environment = config('self-diagnosis.environment_aliases.' . $environment);
35
            $environmentChecks = config('self-diagnosis.environment_checks.' . $environment, []);
36
        }
37
38
        $this->runChecks($environmentChecks, trans('self-diagnosis::commands.self_diagnosis.environment_specific_checks', ['environment' => $environment]));
39
40
        if (count($this->messages)) {
41
            $this->error(trans('self-diagnosis::commands.self_diagnosis.failed_checks'));
42
43
            foreach ($this->messages as $message) {
44
                $this->output->writeln('<fg=red>'.$message.'</fg=red>');
45
                $this->output->writeln('');
46
            }
47
            return 1; // Any other return code then 0 means exit with error
48
        }
49
        $this->info(trans('self-diagnosis::commands.self_diagnosis.success'));
50
        return 0;
51
    }
52
53
    protected function runChecks(array $checks, string $title)
54
    {
55
        $max = count($checks);
56
        $current = 1;
57
58
        $this->output->writeln('|-------------------------------------');
59
        $this->output->writeln('| '.$title);
60
        $this->output->writeln('|-------------------------------------');
61
62
        foreach ($checks as $check => $config) {
63
            if (is_numeric($check)) {
64
                $check = $config;
65
                $config = [];
66
            }
67
68
            $checkClass = app($check);
69
70
            $this->output->write(trans('self-diagnosis::commands.self_diagnosis.running_check', [
71
                'current' => $current,
72
                'max' => $max,
73
                'name' => $checkClass->name($config),
74
            ]));
75
76
            $this->runCheck($checkClass, $config);
77
78
            $current++;
79
        }
80
81
        $this->output->writeln('');
82
    }
83
84
    protected function runCheck(Check $check, array $config)
85
    {
86
        if ($check->check($config)) {
87
            $this->output->write('<fg=green>✔</fg=green>');
88
        } else {
89
            $this->output->write('<fg=red>✘</fg=red>');
90
91
            $this->messages[] = $check->message($config);
92
        }
93
94
        $this->output->write(PHP_EOL);
95
    }
96
}
97