Test Setup Failed
Branch develop (afdb67)
by Joseph
05:47 queued 02:50
created

Edit::loadEnvContents()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
/*
3
 * This file is part of laravel-env-security.
4
 *
5
 *  (c) Signature Tech Studio, Inc <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE.md
8
 * file that was distributed with this source code.
9
 */
10
11
namespace STS\EnvSecurity\Console;
12
13
use Illuminate\Console\Command;
14
use STS\EnvSecurity\Console\Concerns\HandlesEnvFiles;
15
use STS\EnvSecurity\EnvSecurityManager;
16
use Symfony\Component\Process\Process;
17
18
/**
19
 * Class Edit
20
 * @package STS\EnvSecurity\Console
21
 */
22
class Edit extends Command
23
{
24
    use HandlesEnvFiles;
25
26
    /**
27
     * The name and signature of the console command.
28
     *
29
     * @var string
30
     */
31
    protected $signature = 'env:edit {environment : Which environment file you wish to decrypt}';
32
33
    /**
34
     * The console command description.
35
     *
36
     * @var string
37
     */
38
    protected $description = 'Edit an encrypted env file';
39
40
    /**
41
     * @var EnvSecurityManager
42
     */
43
    protected $envSecurity;
44
45
    public function __construct(EnvSecurityManager $envSecurity)
46
    {
47
        $this->envSecurity = $envSecurity;
48
49
        parent::__construct();
50
    }
51
52
    /**
53
     * Execute the console command.
54
     *
55
     * @return mixed
56
     */
57
    public function handle()
58
    {
59
        $this->saveEnvContents(
60
            $this->edit(
61
                $this->loadEnvContents()
62
            )
63
        );
64
65
        $this->info("Successfully updated .env for environment [{$this->environment()}]");
66
    }
67
68
    /**
69
     * @param $contents
70
     * @codeCoverageIgnore
71
     *
72
     * @return mixed
73
     */
74
    protected function edit($contents)
75
    {
76
        $tmpFile = tmpfile();
77
        fwrite($tmpFile, $contents);
0 ignored issues
show
Bug introduced by
It seems like $tmpFile can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
        fwrite(/** @scrutinizer ignore-type */ $tmpFile, $contents);
Loading history...
78
        $meta = stream_get_meta_data($tmpFile);
0 ignored issues
show
Bug introduced by
It seems like $tmpFile can also be of type false; however, parameter $stream of stream_get_meta_data() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
        $meta = stream_get_meta_data(/** @scrutinizer ignore-type */ $tmpFile);
Loading history...
79
80
        $process = new Process(config('env-security.editor') . ' ' . $meta['uri']);
0 ignored issues
show
Bug introduced by
config('env-security.edi...') . ' ' . $meta['uri'] of type string is incompatible with the type array expected by parameter $command of Symfony\Component\Process\Process::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
        $process = new Process(/** @scrutinizer ignore-type */ config('env-security.editor') . ' ' . $meta['uri']);
Loading history...
81
        $process->setTty(true);
82
        $process->mustRun();
83
84
        return file_get_contents($meta['uri']);
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    protected function environment()
91
    {
92
        return $this->argument('environment');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->argument('environment') also could return the type string[] which is incompatible with the documented return type string.
Loading history...
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    protected function loadEnvContents()
99
    {
100
        if($ciphertext = $this->loadEncrypted($this->environment())) {
101
            return $this->envSecurity->decrypt($ciphertext);
0 ignored issues
show
Bug introduced by
The method decrypt() does not exist on STS\EnvSecurity\EnvSecurityManager. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
            return $this->envSecurity->/** @scrutinizer ignore-call */ decrypt($ciphertext);
Loading history...
102
        }
103
104
        return '';
105
    }
106
107
    /**
108
     * @param $plaintext
109
     */
110
    protected function saveEnvContents($plaintext)
111
    {
112
        $ciphertext = !empty($plaintext)
113
            ? $this->envSecurity->encrypt($plaintext)
0 ignored issues
show
Bug introduced by
The method encrypt() does not exist on STS\EnvSecurity\EnvSecurityManager. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
            ? $this->envSecurity->/** @scrutinizer ignore-call */ encrypt($plaintext)
Loading history...
114
            : '';
115
116
        $this->saveEncrypted($ciphertext, $this->environment());
117
    }
118
}
119