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 (#286)
by Dane
03:00
created

ServersController   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 433
Duplicated Lines 21.25 %

Coupling/Cohesion

Components 0
Dependencies 14

Importance

Changes 0
Metric Value
wmc 57
lcom 0
cbo 14
dl 92
loc 433
rs 5.1724
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getIndex() 0 6 1
A getNew() 0 7 1
A getView() 0 21 1
B postNewServer() 0 27 4
A postNewServerGetNodes() 0 10 2
A postNewServerGetIps() 0 21 4
A postNewServerServiceOptions() 0 12 2
A postNewServerOptionDetails() 0 19 2
B postUpdateServerDetails() 0 28 5
B postUpdateContainerDetails() 0 23 4
A postUpdateServerToggleBuild() 0 20 2
B postUpdateServerUpdateBuild() 0 36 4
A deleteServer() 18 20 3
A postToggleInstall() 18 18 3
A postUpdateServerStartup() 20 20 3
B postDatabase() 0 25 3
A postSuspendServer() 18 18 3
A postUnsuspendServer() 18 18 3
B postQueuedDeletionHandler() 0 31 6

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ServersController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ServersController, and based on these observations, apply Extract Interface, too.

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\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)
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...
49
    {
50
        return view('admin.servers.index', [
51
            'servers' => Models\Server::withTrashed()->with('node', 'user')->paginate(25),
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...
52
        ]);
53
    }
54
55
    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...
56
    {
57
        return view('admin.servers.new', [
58
            'locations' => Models\Location::all(),
59
            'services' => Models\Service::all(),
60
        ]);
61
    }
62
63
    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...
64
    {
65
66
        $server = Models\Server::withTrashed()->with(
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...
67
            'user', 'option.variables', 'variables',
68
            'node.allocations', 'databases.host'
69
        )->findOrFail($id);
70
71
        $server->option->variables->transform(function ($item, $key) use ($server) {
0 ignored issues
show
Unused Code introduced by
The parameter $key 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...
72
            $item->server_value = $server->variables->where('variable_id', $item->id)->pluck('variable_value')->first();
73
74
            return $item;
75
        });
76
77
        return view('admin.servers.view', [
78
            'server' => $server,
79
            'assigned' => $server->node->allocations->where('server_id', $server->id)->sortBy('port')->sortBy('ip'),
80
            'unassigned' => $server->node->allocations->where('server_id', null)->sortBy('port')->sortBy('ip'),
81
            'db_servers' => Models\DatabaseServer::all(),
82
        ]);
83
    }
84
85
    public function postNewServer(Request $request)
86
    {
87
        try {
88
            $server = new ServerRepository;
89
            $response = $server->create($request->only([
90
                'owner', 'name', 'memory', 'swap',
91
                'node', 'ip', 'port', 'allocation',
92
                'cpu', 'disk', 'service',
93
                'option', 'location', 'pack',
94
                'startup', 'custom_image_name',
95
                'auto_deploy', 'custom_id',
96
            ]));
97
98
            return redirect()->route('admin.servers.view', ['id' => $response]);
99
        } catch (DisplayValidationException $ex) {
100
            return redirect()->route('admin.servers.new')->withErrors(json_decode($ex->getMessage()))->withInput();
101
        } catch (DisplayException $ex) {
102
            Alert::danger($ex->getMessage())->flash();
103
104
            return redirect()->route('admin.servers.new')->withInput();
105
        } catch (\Exception $ex) {
106
            Log::error($ex);
107
            Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
108
109
            return redirect()->route('admin.servers.new')->withInput();
110
        }
111
    }
112
113
    /**
114
     * Returns a JSON tree of all avaliable nodes in a given location.
115
     *
116
     * @param  \Illuminate\Http\Request $request
117
     * @return \Illuminate\Contracts\View\View
118
     */
119
    public function postNewServerGetNodes(Request $request)
120
    {
121
        if (! $request->input('location')) {
122
            return response()->json([
123
                'error' => 'Missing location in request.',
124
            ], 500);
125
        }
126
127
        return response()->json(Models\Node::select('id', 'name', 'public')->where('location', $request->input('location'))->get());
128
    }
129
130
    /**
131
     * Returns a JSON tree of all avaliable IPs and Ports on a given node.
132
     *
133
     * @param  \Illuminate\Http\Request $request
134
     * @return \Illuminate\Contracts\View\View
135
     */
136
    public function postNewServerGetIps(Request $request)
137
    {
138
        if (! $request->input('node')) {
139
            return response()->json([
140
                'error' => 'Missing node in request.',
141
            ], 500);
142
        }
143
144
        $ips = Models\Allocation::where('node', $request->input('node'))->whereNull('assigned_to')->get();
145
        $listing = [];
146
147
        foreach ($ips as &$ip) {
148
            if (array_key_exists($ip->ip, $listing)) {
149
                $listing[$ip->ip] = array_merge($listing[$ip->ip], [$ip->port]);
150
            } else {
151
                $listing[$ip->ip] = [$ip->port];
152
            }
153
        }
154
155
        return response()->json($listing);
156
    }
157
158
    /**
159
     * Returns a JSON tree of all avaliable options for a given service.
160
     *
161
     * @param  \Illuminate\Http\Request $request
162
     * @return \Illuminate\Contracts\View\View
163
     */
164
    public function postNewServerServiceOptions(Request $request)
165
    {
166
        if (! $request->input('service')) {
167
            return response()->json([
168
                'error' => 'Missing service in request.',
169
            ], 500);
170
        }
171
172
        $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...
173
174
        return response()->json(Models\ServiceOptions::select('id', 'name', 'docker_image')->where('service_id', $request->input('service'))->orderBy('name', 'asc')->get());
175
    }
176
177
    /**
178
     * Returns a JSON tree of all avaliable variables for a given service option.
179
     *
180
     * @param  \Illuminate\Http\Request $request
181
     * @return \Illuminate\Contracts\View\View
182
     */
183
    public function postNewServerOptionDetails(Request $request)
184
    {
185
        if (! $request->input('option')) {
186
            return response()->json([
187
                'error' => 'Missing option in request.',
188
            ], 500);
189
        }
190
191
        $option = Models\ServiceOptions::with('variables', ['packs' => function ($query) {
0 ignored issues
show
Bug introduced by
The method findOrFail does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
192
            $query->where('selectable', true);
193
        }])->findOrFail($request->input('option'));
194
195
        return response()->json([
196
            'packs' => $option->packs,
197
            'variables' => $option->variables,
198
            'exec' => $option->display_executable,
199
            'startup' => $option->display_startup,
200
        ]);
201
    }
202
203
    public function postUpdateServerDetails(Request $request, $id)
204
    {
205
        try {
206
            $server = new ServerRepository;
207
            $server->updateDetails($id, [
208
                'owner' => $request->input('owner'),
209
                'name' => $request->input('name'),
210
                '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...
211
            ]);
212
213
            Alert::success('Server details were successfully updated.')->flash();
214
        } catch (DisplayValidationException $ex) {
215
            return redirect()->route('admin.servers.view', [
216
                'id' => $id,
217
                'tab' => 'tab_details',
218
            ])->withErrors(json_decode($ex->getMessage()))->withInput();
219
        } catch (DisplayException $ex) {
220
            Alert::danger($ex->getMessage())->flash();
221
        } catch (\Exception $ex) {
222
            Log::error($ex);
223
            Alert::danger('An unhandled exception occured while attemping to update this server. Please try again.')->flash();
224
        }
225
226
        return redirect()->route('admin.servers.view', [
227
            'id' => $id,
228
            'tab' => 'tab_details',
229
        ])->withInput();
230
    }
231
232
    public function postUpdateContainerDetails(Request $request, $id)
233
    {
234
        try {
235
            $server = new ServerRepository;
236
            $server->updateContainer($id, ['image' => $request->input('docker_image')]);
237
            Alert::success('Successfully updated this server\'s docker image.')->flash();
238
        } catch (DisplayValidationException $ex) {
239
            return redirect()->route('admin.servers.view', [
240
                'id' => $id,
241
                'tab' => 'tab_details',
242
            ])->withErrors(json_decode($ex->getMessage()))->withInput();
243
        } catch (DisplayException $ex) {
244
            Alert::danger($ex->getMessage())->flash();
245
        } catch (\Exception $ex) {
246
            Log::error($ex);
247
            Alert::danger('An unhandled exception occured while attemping to update this server\'s docker image. Please try again.')->flash();
248
        }
249
250
        return redirect()->route('admin.servers.view', [
251
            'id' => $id,
252
            'tab' => 'tab_details',
253
        ]);
254
    }
255
256
    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...
257
    {
258
        $server = Models\Server::with('node')->findOrFail($id);
0 ignored issues
show
Bug introduced by
The method findOrFail does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
259
260
        try {
261
            $res = $server->node->guzzleClient([
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...
262
                'X-Access-Server' => $server->uuid,
263
                'X-Access-Token' => $node->daemonSecret,
0 ignored issues
show
Bug introduced by
The variable $node does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
264
            ])->request('POST', '/server/rebuild');
265
            Alert::success('A rebuild has been queued successfully. It will run the next time this server is booted.')->flash();
266
        } catch (\GuzzleHttp\Exception\TransferException $ex) {
267
            Log::warning($ex);
268
            Alert::danger('An error occured while attempting to toggle a rebuild.')->flash();
269
        }
270
271
        return redirect()->route('admin.servers.view', [
272
            'id' => $id,
273
            'tab' => 'tab_manage',
274
        ]);
275
    }
276
277
    public function postUpdateServerUpdateBuild(Request $request, $id)
278
    {
279
        try {
280
            $server = new ServerRepository;
281
            $server->changeBuild($id, $request->only([
282
                'default',
283
                'add_additional',
284
                'remove_additional',
285
                'memory',
286
                'swap',
287
                'io',
288
                'cpu',
289
            ]));
290
            Alert::success('Server details were successfully updated.')->flash();
291
        } catch (DisplayValidationException $ex) {
292
            return redirect()->route('admin.servers.view', [
293
                'id' => $id,
294
                'tab' => 'tab_build',
295
            ])->withErrors(json_decode($ex->getMessage()))->withInput();
296
        } catch (DisplayException $ex) {
297
            Alert::danger($ex->getMessage())->flash();
298
299
            return redirect()->route('admin.servers.view', [
300
                'id' => $id,
301
                'tab' => 'tab_build',
302
            ]);
303
        } catch (\Exception $ex) {
304
            Log::error($ex);
305
            Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
306
        }
307
308
        return redirect()->route('admin.servers.view', [
309
            'id' => $id,
310
            'tab' => 'tab_build',
311
        ]);
312
    }
313
314 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...
315
    {
316
        try {
317
            $server = new ServerRepository;
318
            $server->deleteServer($id, $force);
319
            Alert::success('Server has been marked for deletion on the system.')->flash();
320
321
            return redirect()->route('admin.servers');
322
        } catch (DisplayException $ex) {
323
            Alert::danger($ex->getMessage())->flash();
324
        } catch (\Exception $ex) {
325
            Log::error($ex);
326
            Alert::danger('An unhandled exception occured while attemping to delete this server. Please try again.')->flash();
327
        }
328
329
        return redirect()->route('admin.servers.view', [
330
            'id' => $id,
331
            'tab' => 'tab_delete',
332
        ]);
333
    }
334
335 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...
336
    {
337
        try {
338
            $server = new ServerRepository;
339
            $server->toggleInstall($id);
340
            Alert::success('Server status was successfully toggled.')->flash();
341
        } catch (DisplayException $ex) {
342
            Alert::danger($ex->getMessage())->flash();
343
        } catch (\Exception $ex) {
344
            Log::error($ex);
345
            Alert::danger('An unhandled exception occured while attemping to toggle this servers status.')->flash();
346
        } finally {
347
            return redirect()->route('admin.servers.view', [
348
                'id' => $id,
349
                'tab' => 'tab_manage',
350
            ]);
351
        }
352
    }
353
354 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...
355
    {
356
        try {
357
            $server = new ServerRepository;
358
            $server->updateStartup($id, $request->except([
359
                '_token',
360
            ]), true);
361
            Alert::success('Server startup variables were successfully updated.')->flash();
362
        } catch (\Pterodactyl\Exceptions\DisplayException $e) {
363
            Alert::danger($e->getMessage())->flash();
364
        } catch (\Exception $e) {
365
            Log::error($e);
366
            Alert::danger('An unhandled exception occured while attemping to update startup variables for this server. Please try again.')->flash();
367
        } finally {
368
            return redirect()->route('admin.servers.view', [
369
                'id' => $id,
370
                'tab' => 'tab_startup',
371
            ])->withInput();
372
        }
373
    }
374
375
    public function postDatabase(Request $request, $id)
376
    {
377
        try {
378
            $repo = new DatabaseRepository;
379
            $repo->create($id, $request->only([
380
                'db_server',
381
                'database',
382
                'remote',
383
            ]));
384
            Alert::success('Added new database to this server.')->flash();
385
        } catch (DisplayValidationException $ex) {
386
            return redirect()->route('admin.servers.view', [
387
                'id' => $id,
388
                'tab' => 'tab_database',
389
            ])->withInput()->withErrors(json_decode($ex->getMessage()))->withInput();
390
        } catch (\Exception $ex) {
391
            Log::error($ex);
392
            Alert::danger('An exception occured while attempting to add a new database for this server.')->flash();
393
        }
394
395
        return redirect()->route('admin.servers.view', [
396
            'id' => $id,
397
            'tab' => 'tab_database',
398
        ])->withInput();
399
    }
400
401 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...
402
    {
403
        try {
404
            $repo = new ServerRepository;
405
            $repo->suspend($id);
406
            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.');
407
        } catch (DisplayException $e) {
408
            Alert::danger($e->getMessage())->flash();
409
        } catch (\Exception $e) {
410
            Log::error($e);
411
            Alert::danger('An unhandled exception occured while attemping to suspend this server. Please try again.')->flash();
412
        } finally {
413
            return redirect()->route('admin.servers.view', [
414
                'id' => $id,
415
                'tab' => 'tab_manage',
416
            ]);
417
        }
418
    }
419
420 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...
421
    {
422
        try {
423
            $repo = new ServerRepository;
424
            $repo->unsuspend($id);
425
            Alert::success('Server has been unsuspended on the system. Access has been re-enabled.');
426
        } catch (DisplayException $e) {
427
            Alert::danger($e->getMessage())->flash();
428
        } catch (\Exception $e) {
429
            Log::error($e);
430
            Alert::danger('An unhandled exception occured while attemping to unsuspend this server. Please try again.')->flash();
431
        } finally {
432
            return redirect()->route('admin.servers.view', [
433
                'id' => $id,
434
                'tab' => 'tab_manage',
435
            ]);
436
        }
437
    }
438
439
    public function postQueuedDeletionHandler(Request $request, $id)
440
    {
441
        try {
442
            $repo = new ServerRepository;
443
            if (! is_null($request->input('cancel'))) {
444
                $repo->cancelDeletion($id);
445
                Alert::success('Server deletion has been cancelled. This server will remain suspended until you unsuspend it.')->flash();
446
447
                return redirect()->route('admin.servers.view', $id);
448
            } elseif (! is_null($request->input('delete'))) {
449
                $repo->deleteNow($id);
450
                Alert::success('Server was successfully deleted from the system.')->flash();
451
452
                return redirect()->route('admin.servers');
453
            } elseif (! is_null($request->input('force_delete'))) {
454
                $repo->deleteNow($id, true);
455
                Alert::success('Server was successfully force deleted from the system.')->flash();
456
457
                return redirect()->route('admin.servers');
458
            }
459
        } catch (DisplayException $ex) {
460
            Alert::danger($ex->getMessage())->flash();
461
462
            return redirect()->route('admin.servers.view', $id);
463
        } catch (\Exception $ex) {
464
            Log::error($ex);
465
            Alert::danger('An unhandled error occured while attempting to perform this action.')->flash();
466
467
            return redirect()->route('admin.servers.view', $id);
468
        }
469
    }
470
}
471