Completed
Push — master ( 8cd4e2...916334 )
by Maarten
01:34 queued 18s
created

EnvScan   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 19 2
A showOutput() 0 19 3
1
<?php
2
3
namespace Mtolhuys\LaravelEnvScanner\Commands;
4
5
use Illuminate\Console\Command;
6
use Mtolhuys\LaravelEnvScanner\LaravelEnvScanner;
7
8
class EnvScan extends Command
9
{
10
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = '
17
        env:scan 
18
            { --d|dir= : Specify directory to scan (defaults to your config folder) }
19
            { --u|undefined-only : Only show undefined variables as output }
20
    ';
21
22
    private $scanner;
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Check environmental variables used in your app';
30
31
    /**
32
     * Execute the console command.
33
     *
34
     * @return mixed
35
     * @throws \Exception
36
     */
37
    public function handle()
38
    {
39
        $this->scanner = new LaravelEnvScanner(
40
            $this->option('dir')
41
        );
42
43
        if (! file_exists($this->scanner->dir)) {
44
            $this->error("{$this->scanner->dir} does not exist");
45
            exit();
46
        }
47
48
        $this->output->write(
49
            "<fg=green>Scanning:</fg=green> <fg=white>{$this->scanner->dir}...</fg=white>\n"
50
        );
51
52
        $this->scanner->scan();
53
54
        $this->showOutput();
55
    }
56
57
    private function showOutput() {
58
        if ($this->option('undefined-only')) {
59
            if ($this->scanner->results['undefined'] === 0) {
60
                $this->info("Looking good!");
61
            } else {
62
                $this->output->write(
63
                    "<fg=red>{$this->scanner->results['undefined']} used environmental variables are undefined:</fg=red>\n"
64
                );
65
                $this->output->write('<fg=red>'.implode(PHP_EOL, $this->scanner->undefined)."</fg=red>\n");
66
            }
67
        } else {
68
            $this->table([
69
                "Files ({$this->scanner->results['files']})",
70
                "Defined ({$this->scanner->results['defined']})",
71
                "Depending on default ({$this->scanner->results['depending_on_default']})",
72
                "Undefined ({$this->scanner->results['undefined']})",
73
            ], $this->scanner->results['data']);
74
        }
75
    }
76
}
77