Passed
Push — master ( a34831...34d521 )
by Miroslaw
04:02
created

EnvGetCommand   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 30
c 4
b 0
f 0
dl 0
loc 107
rs 10
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 12 2
A _printOutput() 0 12 4
A _printKeyValue() 0 10 5
A _printAllEnvValues() 0 4 2
1
<?php
2
3
namespace msztorc\LaravelEnv\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Str;
7
use msztorc\LaravelEnv\Commands\Traits\CommandValidator;
8
use msztorc\LaravelEnv\Env;
9
10
class EnvGetCommand extends Command
11
{
12
13
    use CommandValidator;
14
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'env:get {key?} {--key-value} {--json}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Get variable value from an environment file';
28
29
    /**
30
     * Environment variable key.
31
     *
32
     * @var string
33
     */
34
    protected $key;
35
36
    /**
37
     * key-value format arg
38
     *
39
     * @var bool
40
     */
41
    protected $keyValFormat;
42
43
    /**
44
     * json format argument
45
     *
46
     * @var bool
47
     */
48
    protected $json;
49
50
    /**
51
     * Env object
52
     *
53
     * @var object
54
     */
55
    protected $env;
56
57
    /**
58
     * Create a new command instance.
59
     *
60
     * @return void
61
     */
62
    public function __construct()
63
    {
64
        parent::__construct();
65
    }
66
67
    /**
68
     * Execute the console command.
69
     *
70
     * @return mixed
71
     */
72
    public function handle()
73
    {
74
        $this->key = (string)$this->argument('key');
75
76
        if (strlen($this->key))
77
            $this->isValidKey($this->key);
78
79
        $this->json = (bool)$this->option('json');
80
        $this->keyValFormat = (bool)$this->option('key-value');
81
        $this->env = new Env();
82
83
        return $this->_printOutput();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->_printOutput() targeting msztorc\LaravelEnv\Comma...Command::_printOutput() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
84
85
    }
86
87
    private function _printAllEnvValues()
88
    {
89
        $this->line(($this->json) ? json_encode($this->env->getVariables()) : $this->env->getEnvContent());
90
        return;
91
    }
92
93
    private function _printKeyValue()
94
    {
95
        $value = ($this->json) ? json_encode($this->env->getKeyValue($this->key)) : ($this->keyValFormat ? $this->env->getKeyValue($this->key) : $this->env->getValue($this->key));
96
97
        $this->line(($this->json)
98
            ? (string)$value
99
            : ($this->keyValFormat ? "{$this->key}={$value[$this->key]}" : (string)$value)
100
        );
101
102
        return;
103
    }
104
105
    private function _printOutput()
106
    {
107
        if (!strlen($this->key)) {
108
            $this->_printAllEnvValues();
109
        }
110
111
        if (strlen($this->key) && $this->env->exists($this->key)) {
112
            $this->_printKeyValue();
113
        } else {
114
            $this->line("There is no variable '{$this->key}'");
115
        }
116
        return;
117
    }
118
}
119