VapidKeysGenerateCommand   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 112
ccs 0
cts 55
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 17 3
A setKeysInEnvironmentFile() 0 12 3
B writeNewEnvironmentFileWith() 0 26 3
A keyReplacementPattern() 0 14 2
1
<?php
2
3
namespace NotificationChannels\WebPush;
4
5
use Illuminate\Support\Str;
6
use Minishlink\WebPush\VAPID;
7
use Illuminate\Console\Command;
8
use Illuminate\Console\ConfirmableTrait;
9
10
class VapidKeysGenerateCommand extends Command
11
{
12
    use ConfirmableTrait;
13
14
    /**
15
     * @var string
16
     */
17
    protected $signature = 'webpush:vapid
18
                        {--show : Display the keys instead of modifying files}
19
                        {--force : Force the operation to run when in production}';
20
21
    /**
22
     * @var string
23
     */
24
    protected $description = 'Generate VAPID keys.';
25
26
    /**
27
     * Execute the console command.
28
     *
29
     * @return mixed
30
     */
31
    public function handle()
32
    {
33
        $keys = VAPID::createVapidKeys();
34
35
        if ($this->option('show')) {
36
            $this->line('<comment>VAPID_PUBLIC_KEY='.$keys['publicKey'].'</comment>');
37
            $this->line('<comment>VAPID_PRIVATE_KEY='.$keys['privateKey'].'</comment>');
38
39
            return;
40
        }
41
42
        if (! $this->setKeysInEnvironmentFile($keys)) {
43
            return;
44
        }
45
46
        $this->info('VAPID keys set successfully.');
47
    }
48
49
    /**
50
     * Set the keys in the environment file.
51
     *
52
     * @param  array $keys
53
     * @return bool
54
     */
55
    protected function setKeysInEnvironmentFile($keys)
56
    {
57
        $currentKeys = $this->laravel['config']['webpush.vapid'];
58
59
        if (strlen($currentKeys['public_key']) !== 0 && (! $this->confirmToProceed())) {
60
            return false;
61
        }
62
63
        $this->writeNewEnvironmentFileWith($keys);
64
65
        return true;
66
    }
67
68
    /**
69
     * Write a new environment file with the given keys.
70
     *
71
     * @param  array $keys
72
     * @return void
73
     */
74
    protected function writeNewEnvironmentFileWith($keys)
75
    {
76
        $contents = file_get_contents($this->laravel->environmentFilePath());
0 ignored issues
show
Bug introduced by
The method environmentFilePath() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean environment()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
77
78
        if (! Str::contains($contents, 'VAPID_PUBLIC_KEY')) {
79
            $contents .= PHP_EOL.'VAPID_PUBLIC_KEY=';
80
        }
81
82
        if (! Str::contains($contents, 'VAPID_PRIVATE_KEY')) {
83
            $contents .= PHP_EOL.'VAPID_PRIVATE_KEY=';
84
        }
85
86
        $contents = preg_replace(
87
            [
88
                $this->keyReplacementPattern('VAPID_PUBLIC_KEY'),
89
                $this->keyReplacementPattern('VAPID_PRIVATE_KEY'),
90
            ],
91
            [
92
                'VAPID_PUBLIC_KEY='.$keys['publicKey'],
93
                'VAPID_PRIVATE_KEY='.$keys['privateKey'],
94
            ],
95
            $contents
96
        );
97
98
        file_put_contents($this->laravel->environmentFilePath(), $contents);
0 ignored issues
show
Bug introduced by
The method environmentFilePath() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean environment()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
99
    }
100
101
    /**
102
     * Get a regex pattern that will match env $keyName with any key.
103
     *
104
     * @param  string $keyName
105
     * @return string
106
     */
107
    protected function keyReplacementPattern($keyName)
108
    {
109
        $key = $this->laravel['config']['webpush.vapid'];
110
111
        if ($keyName === 'VAPID_PUBLIC_KEY') {
112
            $key = $key['public_key'];
113
        } else {
114
            $key = $key['private_key'];
115
        }
116
117
        $escaped = preg_quote('='.$key, '/');
118
119
        return "/^{$keyName}{$escaped}/m";
120
    }
121
}
122