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.

UpdateEnvironment   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 179
Duplicated Lines 39.11 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 23
lcom 1
cbo 3
dl 70
loc 179
rs 10
c 5
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
F handle() 70 137 22

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Pterodactyl - Panel
4
 * Copyright (c) 2015 - 2017 Dane Everitt <[email protected]>.
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in all
14
 * copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 */
24
25
namespace Pterodactyl\Console\Commands;
26
27
use Uuid;
28
use Illuminate\Console\Command;
29
30
class UpdateEnvironment extends Command
31
{
32
    /**
33
     * The name and signature of the console command.
34
     *
35
     * @var string
36
     */
37
    protected $signature = 'pterodactyl:env
38
                            {--dbhost=}
39
                            {--dbport=}
40
                            {--dbname=}
41
                            {--dbuser=}
42
                            {--dbpass=}
43
                            {--url=}
44
                            {--driver=}
45
                            {--session-driver=}
46
                            {--queue-driver=}
47
                            {--timezone=}';
48
49
    /**
50
     * The console command description.
51
     *
52
     * @var string
53
     */
54
    protected $description = 'Update environment settings automatically.';
55
56
    /**
57
     * Create a new command instance.
58
     *
59
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
60
     */
61
    public function __construct()
62
    {
63
        parent::__construct();
64
    }
65
66
    /**
67
     * Execute the console command.
68
     *
69
     * @return mixed
70
     */
71
    public function handle()
72
    {
73
        $variables = [];
74
        $file = base_path() . '/.env';
75
        if (! file_exists($file)) {
76
            $this->error('Missing environment file! It appears that you have not installed this panel correctly.');
77
            exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method handle() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
78
        }
79
80
        $envContents = file_get_contents($file);
81
82
        $this->info('Simply leave blank and press enter to fields that you do not wish to update.');
83
        if (is_null(config('pterodactyl.service.author', null))) {
84
            $this->info('No service author set, setting one now.');
85
            $variables['SERVICE_AUTHOR'] = (string) Uuid::generate(4);
86
        }
87
88
        if (isset($variables['APP_THEME'])) {
89
            if ($variables['APP_THEME'] === 'default') {
90
                $variables['APP_THEME'] = 'pterodactyl';
91
            }
92
        }
93
94 View Code Duplication
        if (is_null($this->option('dbhost'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
            $variables['DB_HOST'] = $this->anticipate('Database Host', ['localhost', '127.0.0.1', config('database.connections.mysql.host')], config('database.connections.mysql.host'));
96
        } else {
97
            $variables['DB_HOST'] = $this->option('dbhost');
98
        }
99
100 View Code Duplication
        if (is_null($this->option('dbport'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
            $variables['DB_PORT'] = $this->anticipate('Database Port', [3306, config('database.connections.mysql.port')], config('database.connections.mysql.port'));
102
        } else {
103
            $variables['DB_PORT'] = $this->option('dbport');
104
        }
105
106 View Code Duplication
        if (is_null($this->option('dbname'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
            $variables['DB_DATABASE'] = $this->anticipate('Database Name', ['pterodactyl', 'homestead', config('database.connections.mysql.database')], config('database.connections.mysql.database'));
108
        } else {
109
            $variables['DB_DATABASE'] = $this->option('dbname');
110
        }
111
112 View Code Duplication
        if (is_null($this->option('dbuser'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
            $variables['DB_USERNAME'] = $this->anticipate('Database Username', [config('database.connections.mysql.username')], config('database.connections.mysql.username'));
114
        } else {
115
            $variables['DB_USERNAME'] = $this->option('dbuser');
116
        }
117
118
        if (is_null($this->option('dbpass'))) {
119
            $this->line('The Database Password field is required; you cannot hit enter and use a default value.');
120
            $variables['DB_PASSWORD'] = $this->secret('Database User Password');
121
        } else {
122
            $variables['DB_PASSWORD'] = $this->option('dbpass');
123
        }
124
125
        if (is_null($this->option('url'))) {
126
            $variables['APP_URL'] = $this->ask('Panel URL (include http(s)://)', config('app.url'));
127
        } else {
128
            $variables['APP_URL'] = $this->option('url');
129
        }
130
131 View Code Duplication
        if (is_null($this->option('timezone'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
            $this->line('The timezone should match one of the supported timezones according to http://php.net/manual/en/timezones.php');
133
            $variables['APP_TIMEZONE'] = $this->anticipate('Panel Timezone', \DateTimeZone::listIdentifiers(\DateTimeZone::ALL), config('app.timezone'));
134
        } else {
135
            $variables['APP_TIMEZONE'] = $this->option('timezone');
136
        }
137
138
        if (is_null($this->option('driver'))) {
139
            $options = [
140
                'memcached' => 'Memcache',
141
                'redis' => 'Redis (recommended)',
142
                'apc' => 'APC',
143
                'array' => 'PHP Array',
144
            ];
145
            $default = (in_array(config('cache.default', 'memcached'), $options)) ? config('cache.default', 'memcached') : 'memcached';
146
147
            $this->line('If you chose redis as your cache driver backend, you *must* have a redis server configured already.');
148
            $variables['CACHE_DRIVER'] = $this->choice('Which cache driver backend would you like to use?', $options, $default);
149
        } else {
150
            $variables['CACHE_DRIVER'] = $this->option('driver');
151
        }
152
153 View Code Duplication
        if (is_null($this->option('session-driver'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
            $options = [
155
                'database' => 'MySQL (recommended)',
156
                'redis' => 'Redis',
157
                'file' => 'File',
158
                'cookie' => 'Cookie',
159
                'apc' => 'APC',
160
                'array' => 'PHP Array',
161
            ];
162
            $default = (in_array(config('session.driver', 'database'), $options)) ? config('cache.default', 'database') : 'database';
163
164
            $this->line('If you chose redis as your cache driver backend, you *must* have a redis server configured already.');
165
            $variables['SESSION_DRIVER'] = $this->choice('Which session driver backend would you like to use?', $options, $default);
166
        } else {
167
            $variables['SESSION_DRIVER'] = $this->option('session-driver');
168
        }
169
170 View Code Duplication
        if (is_null($this->option('queue-driver'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
            $options = [
172
                'database' => 'Database (recommended)',
173
                'redis' => 'Redis',
174
                'sqs' => 'Amazon SQS',
175
                'sync' => 'Sync',
176
                'null' => 'None',
177
            ];
178
            $default = (in_array(config('queue.driver', 'database'), $options)) ? config('queue.driver', 'database') : 'database';
179
180
            $this->line('If you chose redis as your queue driver backend, you *must* have a redis server configured already.');
181
            $variables['QUEUE_DRIVER'] = $this->choice('Which queue driver backend would you like to use?', $options, $default);
182
        } else {
183
            $variables['QUEUE_DRIVER'] = $this->option('queue-driver');
184
        }
185
186
        $bar = $this->output->createProgressBar(count($variables));
187
188 View Code Duplication
        foreach ($variables as $key => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
189
            if (str_contains($value, ' ') && ! str_contains($value, '"')) {
190
                $value = '"' . $value . '"';
191
            }
192
            $newValue = $key . '=' . $value . ' # DO NOT EDIT! set using pterodactyl:env';
193
194
            if (preg_match_all('/^' . $key . '=(.*)$/m', $envContents) < 1) {
195
                $envContents = $envContents . "\n" . $newValue;
196
            } else {
197
                $envContents = preg_replace('/^' . $key . '=(.*)$/m', $newValue, $envContents);
198
            }
199
            $bar->advance();
200
        }
201
202
        file_put_contents($file, $envContents);
203
        $bar->finish();
204
205
        $this->call('config:cache');
206
        $this->line("\n");
207
    }
208
}
209