Completed
Push — master ( d51385...c569d6 )
by Marcel
01:54
created

SelfDiagnosisCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 76
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 22 4
A runChecks() 0 21 2
A runCheck() 0 12 2
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';
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', []), 'Running Common Checks');
29
30
        $environmentChecks = config('self-diagnosis.development', []);
31
        if (in_array(app()->environment(), config('self-diagnosis.productionEnvironments'))) {
32
            $environmentChecks = config('self-diagnosis.production', []);
33
        }
34
35
        $this->runChecks($environmentChecks, 'Environment Specific Checks ('.app()->environment().')');
36
37
        if (count($this->messages)) {
38
            $this->output->writeln('The following checks failed:');
39
40
            foreach ($this->messages as $message) {
41
                $this->output->writeln('<fg=red>'.$message.'</fg=red>');
42
                $this->output->writeln('');
43
            }
44
        } else {
45
            $this->info('Good job, looks like you are all set up.');
46
        }
47
    }
48
49
    protected function runChecks(array $checks, string $title)
50
    {
51
        $max = count($checks);
52
        $current = 1;
53
54
        $this->output->writeln('|-------------------------------------');
55
        $this->output->writeln('| '.$title);
56
        $this->output->writeln('|-------------------------------------');
57
58
        foreach ($checks as $check) {
59
            $checkClass = app($check);
60
61
            $this->output->write("<fg=yellow>Running check {$current}/{$max}:</fg=yellow> {$checkClass->name()}...  ");
62
63
            $this->runCheck($checkClass);
64
65
            $current++;
66
        }
67
68
        $this->output->writeln('');
69
    }
70
71
    protected function runCheck(Check $check)
72
    {
73
        if ($check->check()) {
74
            $this->output->write('<fg=green>✔</fg=green>');
75
        } else {
76
            $this->output->write('<fg=red>✘</fg=red>');
77
78
            $this->messages[] = $check->message();
79
        }
80
81
        $this->output->write(PHP_EOL);
82
    }
83
}