GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

setKeysInEnvironmentFile()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
rs 9.8666
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3.0416
1
<?php
2
3
namespace NotificationChannels\WebPush;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Console\ConfirmableTrait;
7
use Illuminate\Support\Str;
8
use Minishlink\WebPush\VAPID;
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 3
    public function handle()
32
    {
33 3
        $keys = VAPID::createVapidKeys();
34
35 3
        if ($this->option('show')) {
36 2
            $this->line('<comment>VAPID_PUBLIC_KEY='.$keys['publicKey'].'</comment>');
37 2
            $this->line('<comment>VAPID_PRIVATE_KEY='.$keys['privateKey'].'</comment>');
38
39 2
            return;
40
        }
41
42 1
        if (! $this->setKeysInEnvironmentFile($keys)) {
43
            return;
44
        }
45
46 1
        $this->info('VAPID keys set successfully.');
47 1
    }
48
49
    /**
50
     * Set the keys in the environment file.
51
     *
52
     * @param  array $keys
53
     * @return bool
54
     */
55 1
    protected function setKeysInEnvironmentFile($keys)
56
    {
57 1
        $currentKeys = $this->laravel['config']['webpush.vapid'];
58
59 1
        if (strlen($currentKeys['public_key']) !== 0 && (! $this->confirmToProceed())) {
60
            return false;
61
        }
62
63 1
        $this->writeNewEnvironmentFileWith($keys);
64
65 1
        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 1
    protected function writeNewEnvironmentFileWith($keys)
75
    {
76 1
        $contents = file_get_contents($this->laravel->environmentFilePath());
77
78 1
        if (! Str::contains($contents, 'VAPID_PUBLIC_KEY')) {
79 1
            $contents .= PHP_EOL.'VAPID_PUBLIC_KEY=';
80
        }
81
82 1
        if (! Str::contains($contents, 'VAPID_PRIVATE_KEY')) {
83 1
            $contents .= PHP_EOL.'VAPID_PRIVATE_KEY=';
84
        }
85
86 1
        $contents = preg_replace(
87
            [
88 1
                $this->keyReplacementPattern('VAPID_PUBLIC_KEY'),
89 1
                $this->keyReplacementPattern('VAPID_PRIVATE_KEY'),
90
            ],
91
            [
92 1
                'VAPID_PUBLIC_KEY='.$keys['publicKey'],
93 1
                'VAPID_PRIVATE_KEY='.$keys['privateKey'],
94
            ],
95 1
            $contents
96
        );
97
98 1
        file_put_contents($this->laravel->environmentFilePath(), $contents);
99 1
    }
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 1
    protected function keyReplacementPattern($keyName)
108
    {
109 1
        $key = $this->laravel['config']['webpush.vapid'];
110
111 1
        if ($keyName === 'VAPID_PUBLIC_KEY') {
112 1
            $key = $key['public_key'];
113
        } else {
114 1
            $key = $key['private_key'];
115
        }
116
117 1
        $escaped = preg_quote('='.$key, '/');
118
119 1
        return "/^{$keyName}{$escaped}/m";
120
    }
121
}
122