EnvGetCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace msztorc\LaravelEnv\Commands;
4
5
use Illuminate\Console\Command;
6
use msztorc\LaravelEnv\Commands\Traits\CommandValidator;
7
use msztorc\LaravelEnv\Env;
8
9
class EnvGetCommand extends Command
10
{
11
    use CommandValidator;
12
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'env:get {key?} {--key-value} {--json}';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Get variable value from an environment file';
26
27
    /**
28
     * Environment variable key.
29
     *
30
     * @var string
31
     */
32
    protected $key;
33
34
    /**
35
     * key-value format arg
36
     *
37
     * @var bool
38
     */
39
    protected $keyValFormat;
40
41
    /**
42
     * json format argument
43
     *
44
     * @var bool
45
     */
46
    protected $json;
47
48
    /**
49
     * Env object
50
     *
51
     * @var object
52
     */
53
    protected $env;
54
55
    /**
56
     * Create a new command instance.
57
     *
58
     * @return void
59
     */
60
    public function __construct()
61
    {
62
        parent::__construct();
63
    }
64
65
    /**
66
     * Execute the console command.
67
     *
68
     * @return mixed
69
     */
70
    public function handle()
71
    {
72
        $this->key = (string)$this->argument('key');
73
74
        if (strlen($this->key)) {
75
            $this->isValidKey($this->key);
76
        }
77
78
        $this->json = (bool)$this->option('json');
79
        $this->keyValFormat = (bool)$this->option('key-value');
80
        $this->env = new Env();
81
82
        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...
83
    }
84
85
    private function _getEntireEnvContent()
86
    {
87
        return ($this->json) ? json_encode($this->env->getVariables()) : $this->env->getEnvContent();
88
    }
89
90
    private function _printKeyValue()
91
    {
92
        $value = ($this->json) ? json_encode($this->env->getKeyValue($this->key)) : ($this->keyValFormat ? $this->env->getKeyValue($this->key) : $this->env->getValue($this->key));
93
94
        return($this->json) ? (string)$value : ($this->keyValFormat ? "{$this->key}={$value[$this->key]}" : (string)$value);
95
    }
96
97
    private function _printOutput(): void
98
    {
99
        if (!strlen($this->key)) {
100
            $this->line($this->_getEntireEnvContent());
101
102
            return;
103
        }
104
105
        if (strlen($this->key) && $this->env->exists($this->key)) {
106
            $this->output->writeln($this->_printKeyValue());
107
        } else {
108
            $this->line("There is no variable '{$this->key}'");
109
        }
110
111
        return;
112
    }
113
}
114