JWTGenerateCommand::fire()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 0
cts 13
cp 0
rs 9.4286
cc 3
eloc 10
nc 3
nop 0
crap 12
1
<?php
2
3
namespace Tymon\JWTAuth\Commands;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Console\Command;
7
use Symfony\Component\Console\Input\InputOption;
8
9
class JWTGenerateCommand extends Command
10
{
11
    /**
12
     * The console command name.
13
     *
14
     * @var string
15
     */
16
    protected $name = 'jwt:generate';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Set the JWTAuth secret key used to sign the tokens';
24
25
    /**
26
     * Execute the console command.
27
     *
28
     * @return void
29
     */
30
    public function fire()
31
    {
32
        $key = $this->getRandomKey();
33
34
        if ($this->option('show')) {
35
            return $this->line('<comment>'.$key.'</comment>');
36
        }
37
38
        $path = config_path('jwt.php');
39
40
        if (file_exists($path)) {
41
            file_put_contents($path, str_replace(
42
                $this->laravel['config']['jwt.secret'], $key, file_get_contents($path)
43
            ));
44
        }
45
46
        $this->laravel['config']['jwt.secret'] = $key;
47
48
        $this->info("jwt-auth secret [$key] set successfully.");
49
    }
50
51
    /**
52
     * Generate a random key for the JWT Auth secret.
53
     *
54
     * @return string
55
     */
56
    protected function getRandomKey()
57
    {
58
        return Str::random(32);
59
    }
60
61
    /**
62
     * Get the console command options.
63
     *
64
     * @return array
65
     */
66 3
    protected function getOptions()
67
    {
68
        return [
69 3
            ['show', null, InputOption::VALUE_NONE, 'Simply display the key instead of modifying files.'],
70 2
        ];
71
    }
72
}
73