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.
Completed
Push — master ( 37c3b2...7c969b )
by Cretu
08:36
created

writeNewEnvironmentFileWith()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 13
nc 4
nop 1
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
            return;
39
        }
40
41
        if (! $this->setKeysInEnvironmentFile($keys)) {
42
            return;
43
        }
44
45
        $this->info('VAPID keys set successfully.');
46
    }
47
48
    /**
49
     * Set the keys in the environment file.
50
     *
51
     * @param  array $keys
52
     * @return bool
53
     */
54
    protected function setKeysInEnvironmentFile($keys)
55
    {
56
        $currentKeys = $this->laravel['config']['webpush.vapid'];
57
58
        if (strlen($currentKeys['public_key']) !== 0 && (! $this->confirmToProceed())) {
59
            return false;
60
        }
61
62
        $this->writeNewEnvironmentFileWith($keys);
63
64
        return true;
65
    }
66
67
    /**
68
     * Write a new environment file with the given keys.
69
     *
70
     * @param  array $keys
71
     * @return void
72
     */
73
    protected function writeNewEnvironmentFileWith($keys)
74
    {
75
        $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...
76
77
        if (! Str::contains($contents, 'VAPID_PUBLIC_KEY')) {
78
            $contents .= PHP_EOL.'VAPID_PUBLIC_KEY=';
79
        }
80
81
        if (! Str::contains($contents, 'VAPID_PRIVATE_KEY')) {
82
            $contents .= PHP_EOL.'VAPID_PRIVATE_KEY=';
83
        }
84
85
        $contents = preg_replace(
86
            [
87
                $this->keyReplacementPattern('VAPID_PUBLIC_KEY'),
88
                $this->keyReplacementPattern('VAPID_PRIVATE_KEY'),
89
            ],
90
            [
91
                'VAPID_PUBLIC_KEY='.$keys['publicKey'],
92
                'VAPID_PRIVATE_KEY='.$keys['privateKey'],
93
            ],
94
            $contents
95
        );
96
97
        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...
98
    }
99
100
    /**
101
     * Get a regex pattern that will match env $keyName with any key.
102
     *
103
     * @param  string $keyName
104
     * @return string
105
     */
106
    protected function keyReplacementPattern($keyName)
107
    {
108
        $key = $this->laravel['config']['webpush.vapid'];
109
110
        if ($keyName === 'VAPID_PUBLIC_KEY') {
111
            $key = $key['public_key'];
112
        } else {
113
            $key = $key['private_key'];
114
        }
115
116
        $escaped = preg_quote('='.$key, '/');
117
118
        return "/^{$keyName}{$escaped}/m";
119
    }
120
}
121