EditCredentialsCommand   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 28 2
1
<?php
2
3
namespace BeyondCode\Credentials;
4
5
use Illuminate\Console\Command;
6
use Symfony\Component\Process\Process;
7
use BeyondCode\Credentials\Exceptions\InvalidJSON;
8
9
class EditCredentialsCommand extends Command
10
{
11
    /**
12
     * The console command name.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'credentials:edit';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Encrypt and edit existing credentials. They will be decrypted after saving.';
24
25
    /**
26
     * The command handler.
27
     *
28
     * @param \BeyondCode\Credentials\Credentials $credentials
29
     * @return void
30
     */
31
    public function handle(Credentials $credentials)
32
    {
33
        $filename = config('credentials.file');
34
35
        $decrypted = $credentials->load($filename);
36
37
        $handle = tmpfile();
38
        $meta = stream_get_meta_data($handle);
39
40
        fwrite($handle, json_encode($decrypted, JSON_PRETTY_PRINT | JSON_FORCE_OBJECT));
41
42
        $editor = config('credential.editor', 'vi');
43
44
        $process = new Process([$editor, $meta['uri']]);
45
46
        $process->setTty(true);
47
        $process->mustRun();
48
49
        $data = json_decode(file_get_contents($meta['uri']), JSON_OBJECT_AS_ARRAY);
50
51
        if (JSON_ERROR_NONE !== json_last_error()) {
52
            throw InvalidJSON::create(json_last_error());
53
        }
54
55
        $credentials->store($data, $filename);
56
57
        $this->info('Successfully updated credentials.');
58
    }
59
}
60