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
Pull Request — develop (#248)
by Dane
03:14
created

ServersController::postNewServerOptionDetails()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 9.2
cc 2
eloc 16
nc 2
nop 1
1
<?php
2
/**
3
 * Pterodactyl - Panel
4
 * Copyright (c) 2015 - 2016 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\Http\Controllers\Admin;
26
27
use DB;
28
use Log;
29
use Alert;
30
use Pterodactyl\Models;
31
use Illuminate\Http\Request;
32
use Pterodactyl\Exceptions\DisplayException;
33
use Pterodactyl\Http\Controllers\Controller;
34
use Pterodactyl\Repositories\ServerRepository;
35
use Pterodactyl\Repositories\DatabaseRepository;
36
use Pterodactyl\Exceptions\DisplayValidationException;
37
38
class ServersController extends Controller
39
{
40
    /**
41
     * Controller Constructor.
42
     */
43
    public function __construct()
44
    {
45
        //
46
    }
47
48
    public function getIndex(Request $request)
49
    {
50
        $query = Models\Server::withTrashed()->select(
0 ignored issues
show
Bug introduced by
The method withTrashed() does not exist on Pterodactyl\Models\Server. Did you maybe mean trashed()?

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...
51
            'servers.*',
52
            'nodes.name as a_nodeName',
53
            'users.email as a_ownerEmail',
54
            'allocations.ip',
55
            'allocations.port',
56
            'allocations.ip_alias'
57
        )->join('nodes', 'servers.node', '=', 'nodes.id')
58
        ->join('users', 'servers.owner', '=', 'users.id')
59
        ->join('allocations', 'servers.allocation', '=', 'allocations.id');
60
61
        if ($request->input('filter') && ! is_null($request->input('filter'))) {
62
            preg_match_all('/[^\s"\']+|"([^"]*)"|\'([^\']*)\'/', urldecode($request->input('filter')), $matches);
63
            foreach ($matches[0] as $match) {
64
                $match = str_replace('"', '', $match);
65
                if (strpos($match, ':')) {
66
                    list($field, $term) = explode(':', $match);
67
                    if ($field === 'node') {
68
                        $field = 'nodes.name';
69
                    } elseif ($field === 'owner') {
70
                        $field = 'users.email';
71
                    } elseif (! strpos($field, '.')) {
72
                        $field = 'servers.' . $field;
73
                    }
74
75
                    $query->orWhere($field, 'LIKE', '%' . $term . '%');
76
                } else {
77
                    $query->where('servers.name', 'LIKE', '%' . $match . '%');
78
                    $query->orWhere([
79
                        ['servers.username', 'LIKE', '%' . $match . '%'],
80
                        ['users.email', 'LIKE', '%' . $match . '%'],
81
                        ['allocations.port', 'LIKE', '%' . $match . '%'],
82
                        ['allocations.ip', 'LIKE', '%' . $match . '%'],
83
                    ]);
84
                }
85
            }
86
        }
87
88
        try {
89
            $servers = $query->paginate(20);
90
        } catch (\Exception $ex) {
91
            Alert::warning('There was an error with the search parameters provided.');
92
            $servers = Models\Server::withTrashed()->select(
0 ignored issues
show
Bug introduced by
The method withTrashed() does not exist on Pterodactyl\Models\Server. Did you maybe mean trashed()?

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...
93
                'servers.*',
94
                'nodes.name as a_nodeName',
95
                'users.email as a_ownerEmail',
96
                'allocations.ip',
97
                'allocations.port',
98
                'allocations.ip_alias'
99
            )->join('nodes', 'servers.node', '=', 'nodes.id')
100
            ->join('users', 'servers.owner', '=', 'users.id')
101
            ->join('allocations', 'servers.allocation', '=', 'allocations.id')
102
            ->paginate(20);
103
        }
104
105
        return view('admin.servers.index', [
106
            'servers' => $servers,
107
        ]);
108
    }
109
110
    public function getNew(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111
    {
112
        return view('admin.servers.new', [
113
            'locations' => Models\Location::all(),
114
            'services' => Models\Service::all(),
115
        ]);
116
    }
117
118
    public function getView(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
119
    {
120
        $server = Models\Server::withTrashed()->select(
0 ignored issues
show
Bug introduced by
The method withTrashed() does not exist on Pterodactyl\Models\Server. Did you maybe mean trashed()?

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...
121
            'servers.*',
122
            'users.email as a_ownerEmail',
123
            'services.name as a_serviceName',
124
            DB::raw('IFNULL(service_options.executable, services.executable) as a_serviceExecutable'),
125
            'service_options.docker_image',
126
            'service_options.name as a_servceOptionName',
127
            'allocations.ip',
128
            'allocations.port',
129
            'allocations.ip_alias'
130
        )->join('nodes', 'servers.node', '=', 'nodes.id')
131
        ->join('users', 'servers.owner', '=', 'users.id')
132
        ->join('services', 'servers.service', '=', 'services.id')
133
        ->join('service_options', 'servers.option', '=', 'service_options.id')
134
        ->join('allocations', 'servers.allocation', '=', 'allocations.id')
135
        ->where('servers.id', $id)
136
        ->first();
137
138
        if (! $server) {
139
            return abort(404);
140
        }
141
142
        return view('admin.servers.view', [
143
            'server' => $server,
144
            'node' => Models\Node::select(
145
                    'nodes.*',
146
                    'locations.long as a_locationName'
147
                )->join('locations', 'nodes.location', '=', 'locations.id')
148
                ->where('nodes.id', $server->node)
149
                ->first(),
150
            'assigned' => Models\Allocation::where('assigned_to', $id)->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(),
151
            'unassigned' => Models\Allocation::where('node', $server->node)->whereNull('assigned_to')->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(),
152
            'startup' => Models\ServiceVariables::select('service_variables.*', 'server_variables.variable_value as a_serverValue')
153
                ->join('server_variables', 'server_variables.variable_id', '=', 'service_variables.id')
154
                ->where('service_variables.option_id', $server->option)
155
                ->where('server_variables.server_id', $server->id)
156
                ->get(),
157
            'databases' => Models\Database::select('databases.*', 'database_servers.host as a_host', 'database_servers.port as a_port')
158
                ->where('server_id', $server->id)
159
                ->join('database_servers', 'database_servers.id', '=', 'databases.db_server')
160
                ->get(),
161
            'db_servers' => Models\DatabaseServer::all(),
162
        ]);
163
    }
164
165 View Code Duplication
    public function postNewServer(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
166
    {
167
        try {
168
            $server = new ServerRepository;
169
            $response = $server->create($request->all());
170
171
            return redirect()->route('admin.servers.view', ['id' => $response]);
172
        } catch (DisplayValidationException $ex) {
173
            return redirect()->route('admin.servers.new')->withErrors(json_decode($ex->getMessage()))->withInput();
174
        } catch (DisplayException $ex) {
175
            Alert::danger($ex->getMessage())->flash();
176
177
            return redirect()->route('admin.servers.new')->withInput();
178
        } catch (\Exception $ex) {
179
            Log::error($ex);
180
            Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
181
182
            return redirect()->route('admin.servers.new')->withInput();
183
        }
184
    }
185
186
    /**
187
     * Returns a JSON tree of all avaliable nodes in a given location.
188
     *
189
     * @param  \Illuminate\Http\Request $request
190
     * @return \Illuminate\Contracts\View\View
191
     */
192
    public function postNewServerGetNodes(Request $request)
193
    {
194
        if (! $request->input('location')) {
195
            return response()->json([
196
                'error' => 'Missing location in request.',
197
            ], 500);
198
        }
199
200
        return response()->json(Models\Node::select('id', 'name', 'public')->where('location', $request->input('location'))->get());
201
    }
202
203
    /**
204
     * Returns a JSON tree of all avaliable IPs and Ports on a given node.
205
     *
206
     * @param  \Illuminate\Http\Request $request
207
     * @return \Illuminate\Contracts\View\View
208
     */
209
    public function postNewServerGetIps(Request $request)
210
    {
211
        if (! $request->input('node')) {
212
            return response()->json([
213
                'error' => 'Missing node in request.',
214
            ], 500);
215
        }
216
217
        $ips = Models\Allocation::where('node', $request->input('node'))->whereNull('assigned_to')->get();
218
        $listing = [];
219
220 View Code Duplication
        foreach ($ips as &$ip) {
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...
221
            if (array_key_exists($ip->ip, $listing)) {
222
                $listing[$ip->ip] = array_merge($listing[$ip->ip], [$ip->port]);
223
            } else {
224
                $listing[$ip->ip] = [$ip->port];
225
            }
226
        }
227
228
        return response()->json($listing);
229
    }
230
231
    /**
232
     * Returns a JSON tree of all avaliable options for a given service.
233
     *
234
     * @param  \Illuminate\Http\Request $request
235
     * @return \Illuminate\Contracts\View\View
236
     */
237
    public function postNewServerServiceOptions(Request $request)
238
    {
239
        if (! $request->input('service')) {
240
            return response()->json([
241
                'error' => 'Missing service in request.',
242
            ], 500);
243
        }
244
245
        $service = Models\Service::select('executable', 'startup')->where('id', $request->input('service'))->first();
0 ignored issues
show
Unused Code introduced by
$service is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
246
247
        return response()->json(Models\ServiceOptions::select('id', 'name', 'docker_image')->where('parent_service', $request->input('service'))->orderBy('name', 'asc')->get());
248
    }
249
250
    /**
251
     * Returns a JSON tree of all avaliable variables for a given service option.
252
     *
253
     * @param  \Illuminate\Http\Request $request
254
     * @return \Illuminate\Contracts\View\View
255
     */
256
    public function postNewServerOptionDetails(Request $request)
257
    {
258
        if (! $request->input('option')) {
259
            return response()->json([
260
                'error' => 'Missing option in request.',
261
            ], 500);
262
        }
263
264
        $option = Models\ServiceOptions::select(
265
                DB::raw('COALESCE(service_options.executable, services.executable) as executable'),
266
                DB::raw('COALESCE(service_options.startup, services.startup) as startup')
267
            )->leftJoin('services', 'services.id', '=', 'service_options.parent_service')
268
            ->where('service_options.id', $request->input('option'))
269
            ->first();
270
271
        return response()->json([
272
            'packs' => Models\ServicePack::select('id', 'name', 'version')->where('option', $request->input('option'))->where('selectable', true)->get(),
273
            'variables' => Models\ServiceVariables::where('option_id', $request->input('option'))->get(),
274
            'exec' => $option->executable,
275
            'startup' => $option->startup,
276
        ]);
277
    }
278
279
    public function postUpdateServerDetails(Request $request, $id)
280
    {
281
        try {
282
            $server = new ServerRepository;
283
            $server->updateDetails($id, [
284
                'owner' => $request->input('owner'),
285
                'name' => $request->input('name'),
286
                'reset_token' => ($request->input('reset_token', false) === 'on') ? true : false,
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string|array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
287
            ]);
288
289
            Alert::success('Server details were successfully updated.')->flash();
290
        } catch (DisplayValidationException $ex) {
291
            return redirect()->route('admin.servers.view', [
292
                'id' => $id,
293
                'tab' => 'tab_details',
294
            ])->withErrors(json_decode($ex->getMessage()))->withInput();
295
        } catch (DisplayException $ex) {
296
            Alert::danger($ex->getMessage())->flash();
297
        } catch (\Exception $ex) {
298
            Log::error($ex);
299
            Alert::danger('An unhandled exception occured while attemping to update this server. Please try again.')->flash();
300
        }
301
302
        return redirect()->route('admin.servers.view', [
303
            'id' => $id,
304
            'tab' => 'tab_details',
305
        ])->withInput();
306
    }
307
308 View Code Duplication
    public function postUpdateContainerDetails(Request $request, $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
309
    {
310
        try {
311
            $server = new ServerRepository;
312
            $server->updateContainer($id, [
313
                'image' => $request->input('docker_image'),
314
            ]);
315
            Alert::success('Successfully updated this server\'s docker image.')->flash();
316
        } catch (DisplayValidationException $ex) {
317
            return redirect()->route('admin.servers.view', [
318
                'id' => $id,
319
                'tab' => 'tab_details',
320
            ])->withErrors(json_decode($ex->getMessage()))->withInput();
321
        } catch (DisplayException $ex) {
322
            Alert::danger($ex->getMessage())->flash();
323
        } catch (\Exception $ex) {
324
            Log::error($ex);
325
            Alert::danger('An unhandled exception occured while attemping to update this server\'s docker image. Please try again.')->flash();
326
        }
327
328
        return redirect()->route('admin.servers.view', [
329
            'id' => $id,
330
            'tab' => 'tab_details',
331
        ]);
332
    }
333
334
    public function postUpdateServerToggleBuild(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
335
    {
336
        $server = Models\Server::findOrFail($id);
337
        $node = Models\Node::findOrFail($server->node);
338
        $client = Models\Node::guzzleRequest($server->node);
339
340
        try {
341
            $res = $client->request('POST', '/server/rebuild', [
0 ignored issues
show
Unused Code introduced by
$res is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
342
                'headers' => [
343
                    'X-Access-Server' => $server->uuid,
344
                    'X-Access-Token' => $node->daemonSecret,
345
                ],
346
            ]);
347
            Alert::success('A rebuild has been queued successfully. It will run the next time this server is booted.')->flash();
348
        } catch (\GuzzleHttp\Exception\TransferException $ex) {
349
            Log::warning($ex);
350
            Alert::danger('An error occured while attempting to toggle a rebuild.')->flash();
351
        }
352
353
        return redirect()->route('admin.servers.view', [
354
            'id' => $id,
355
            'tab' => 'tab_manage',
356
        ]);
357
    }
358
359
    public function postUpdateServerUpdateBuild(Request $request, $id)
360
    {
361
        try {
362
            $server = new ServerRepository;
363
            $server->changeBuild($id, [
364
                'default' => $request->input('default'),
365
                'add_additional' => $request->input('add_additional'),
366
                'remove_additional' => $request->input('remove_additional'),
367
                'memory' => $request->input('memory'),
368
                'swap' => $request->input('swap'),
369
                'io' => $request->input('io'),
370
                'cpu' => $request->input('cpu'),
371
            ]);
372
            Alert::success('Server details were successfully updated.')->flash();
373
        } catch (DisplayValidationException $ex) {
374
            return redirect()->route('admin.servers.view', [
375
                'id' => $id,
376
                'tab' => 'tab_build',
377
            ])->withErrors(json_decode($ex->getMessage()))->withInput();
378
        } catch (DisplayException $ex) {
379
            Alert::danger($ex->getMessage())->flash();
380
381
            return redirect()->route('admin.servers.view', [
382
                'id' => $id,
383
                'tab' => 'tab_build',
384
            ]);
385
        } catch (\Exception $ex) {
386
            Log::error($ex);
387
            Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
388
        }
389
390
        return redirect()->route('admin.servers.view', [
391
            'id' => $id,
392
            'tab' => 'tab_build',
393
        ]);
394
    }
395
396 View Code Duplication
    public function deleteServer(Request $request, $id, $force = null)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
397
    {
398
        try {
399
            $server = new ServerRepository;
400
            $server->deleteServer($id, $force);
401
            Alert::success('Server has been marked for deletion on the system.')->flash();
402
403
            return redirect()->route('admin.servers');
404
        } catch (DisplayException $ex) {
405
            Alert::danger($ex->getMessage())->flash();
406
        } catch (\Exception $ex) {
407
            Log::error($ex);
408
            Alert::danger('An unhandled exception occured while attemping to delete this server. Please try again.')->flash();
409
        }
410
411
        return redirect()->route('admin.servers.view', [
412
            'id' => $id,
413
            'tab' => 'tab_delete',
414
        ]);
415
    }
416
417 View Code Duplication
    public function postToggleInstall(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
418
    {
419
        try {
420
            $server = new ServerRepository;
421
            $server->toggleInstall($id);
422
            Alert::success('Server status was successfully toggled.')->flash();
423
        } catch (DisplayException $ex) {
424
            Alert::danger($ex->getMessage())->flash();
425
        } catch (\Exception $ex) {
426
            Log::error($ex);
427
            Alert::danger('An unhandled exception occured while attemping to toggle this servers status.')->flash();
428
        } finally {
429
            return redirect()->route('admin.servers.view', [
430
                'id' => $id,
431
                'tab' => 'tab_manage',
432
            ]);
433
        }
434
    }
435
436 View Code Duplication
    public function postUpdateServerStartup(Request $request, $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
437
    {
438
        try {
439
            $server = new ServerRepository;
440
            $server->updateStartup($id, $request->except([
441
                '_token',
442
            ]), true);
443
            Alert::success('Server startup variables were successfully updated.')->flash();
444
        } catch (\Pterodactyl\Exceptions\DisplayException $e) {
445
            Alert::danger($e->getMessage())->flash();
446
        } catch (\Exception $e) {
447
            Log::error($e);
448
            Alert::danger('An unhandled exception occured while attemping to update startup variables for this server. Please try again.')->flash();
449
        } finally {
450
            return redirect()->route('admin.servers.view', [
451
                'id' => $id,
452
                'tab' => 'tab_startup',
453
            ])->withInput();
454
        }
455
    }
456
457 View Code Duplication
    public function postDatabase(Request $request, $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
458
    {
459
        try {
460
            $repo = new DatabaseRepository;
461
            $repo->create($id, $request->except([
462
                '_token',
463
            ]));
464
            Alert::success('Added new database to this server.')->flash();
465
        } catch (DisplayValidationException $ex) {
466
            return redirect()->route('admin.servers.view', [
467
                'id' => $id,
468
                'tab' => 'tab_database',
469
            ])->withInput()->withErrors(json_decode($ex->getMessage()))->withInput();
470
        } catch (\Exception $ex) {
471
            Log::error($ex);
472
            Alert::danger('An exception occured while attempting to add a new database for this server.')->flash();
473
        }
474
475
        return redirect()->route('admin.servers.view', [
476
            'id' => $id,
477
            'tab' => 'tab_database',
478
        ])->withInput();
479
    }
480
481 View Code Duplication
    public function postSuspendServer(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
482
    {
483
        try {
484
            $repo = new ServerRepository;
485
            $repo->suspend($id);
486
            Alert::success('Server has been suspended on the system. All running processes have been stopped and will not be startable until it is un-suspended.');
487
        } catch (DisplayException $e) {
488
            Alert::danger($e->getMessage())->flash();
489
        } catch (\Exception $e) {
490
            Log::error($e);
491
            Alert::danger('An unhandled exception occured while attemping to suspend this server. Please try again.')->flash();
492
        } finally {
493
            return redirect()->route('admin.servers.view', [
494
                'id' => $id,
495
                'tab' => 'tab_manage',
496
            ]);
497
        }
498
    }
499
500 View Code Duplication
    public function postUnsuspendServer(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
501
    {
502
        try {
503
            $repo = new ServerRepository;
504
            $repo->unsuspend($id);
505
            Alert::success('Server has been unsuspended on the system. Access has been re-enabled.');
506
        } catch (DisplayException $e) {
507
            Alert::danger($e->getMessage())->flash();
508
        } catch (\Exception $e) {
509
            Log::error($e);
510
            Alert::danger('An unhandled exception occured while attemping to unsuspend this server. Please try again.')->flash();
511
        } finally {
512
            return redirect()->route('admin.servers.view', [
513
                'id' => $id,
514
                'tab' => 'tab_manage',
515
            ]);
516
        }
517
    }
518
519
    public function postQueuedDeletionHandler(Request $request, $id)
520
    {
521
        try {
522
            $repo = new ServerRepository;
523
            if (! is_null($request->input('cancel'))) {
524
                $repo->cancelDeletion($id);
525
                Alert::success('Server deletion has been cancelled. This server will remain suspended until you unsuspend it.')->flash();
526
527
                return redirect()->route('admin.servers.view', $id);
528
            } elseif (! is_null($request->input('delete'))) {
529
                $repo->deleteNow($id);
530
                Alert::success('Server was successfully deleted from the system.')->flash();
531
532
                return redirect()->route('admin.servers');
533
            } elseif (! is_null($request->input('force_delete'))) {
534
                $repo->deleteNow($id, true);
535
                Alert::success('Server was successfully force deleted from the system.')->flash();
536
537
                return redirect()->route('admin.servers');
538
            }
539
        } catch (DisplayException $ex) {
540
            Alert::danger($ex->getMessage())->flash();
541
542
            return redirect()->route('admin.servers.view', $id);
543
        } catch (\Exception $ex) {
544
            Log::error($ex);
545
            Alert::danger('An unhandled error occured while attempting to perform this action.')->flash();
546
547
            return redirect()->route('admin.servers.view', $id);
548
        }
549
    }
550
}
551