Completed
Push — develop ( a4838f...d9bf78 )
by Abdelrahman
01:37
created

KeysCommand::handle()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2888
c 0
b 0
f 0
cc 5
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\OAuth\Console\Commands;
6
7
use phpseclib\Crypt\RSA;
8
use Illuminate\Support\Arr;
9
use Illuminate\Console\Command;
10
11
class KeysCommand extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'rinvex:oauth:keys
19
                                      {--force : Overwrite keys they already exist}
20
                                      {--length=4096 : The length of the private key}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Create the encryption keys for API authentication';
28
29
    /**
30
     * Execute the console command.
31
     *
32
     * @param \phpseclib\Crypt\RSA $rsa
33
     *
34
     * @return void
35
     */
36
    public function handle(RSA $rsa)
37
    {
38
        $this->alert($this->description);
39
40
        [$publicKey, $privateKey] = [
0 ignored issues
show
Bug introduced by
The variable $privateKey does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $publicKey does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
41
            self::keyPath('oauth-public.key'),
42
            self::keyPath('oauth-private.key'),
43
        ];
44
45
        if ((file_exists($publicKey) || file_exists($privateKey)) && ! $this->option('force')) {
46
            $this->error('Encryption keys already exist. Use the --force option to overwrite them.');
47
        } else {
48
            $keys = $rsa->createKey($this->input ? (int) $this->option('length') : 4096);
49
50
            file_put_contents($publicKey, Arr::get($keys, 'publickey'));
51
            file_put_contents($privateKey, Arr::get($keys, 'privatekey'));
52
53
            $this->info('Encryption keys generated successfully.');
54
        }
55
    }
56
57
    /**
58
     * The location of the encryption keys.
59
     *
60
     * @param string $file
61
     *
62
     * @return string
63
     */
64
    public static function keyPath($file)
65
    {
66
        $file = ltrim($file, '/\\');
67
68
        return config('rinvex.oauth.key_path')
69
            ? rtrim(config('rinvex.oauth.key_path'), '/\\').DIRECTORY_SEPARATOR.$file
70
            : storage_path($file);
71
    }
72
}
73