Passed
Push — master ( 540a00...a34831 )
by Miroslaw
04:51
created

EnvGetCommand::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 3
b 0
f 0
nc 2
nop 0
dl 0
loc 12
rs 10
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 _printOutput()
88
    {
89
        if (!strlen($this->key)) {
90
            $this->line(($this->json) ? json_encode($this->env->getVariables()) : $this->env->getEnvContent());
91
            return;
92
        }
93
94
        if (strlen($this->key) && $this->env->exists($this->key)) {
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
        } else {
102
            $this->line("There is no variable '{$this->key}'");
103
        }
104
    }
105
}
106