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
08:45 queued 05:44
created

ServersController   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 419
Duplicated Lines 26.73 %

Coupling/Cohesion

Components 0
Dependencies 14

Importance

Changes 0
Metric Value
wmc 57
lcom 0
cbo 14
dl 112
loc 419
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 20 1
A postNewServer() 20 20 4
A postNewServerGetNodes() 0 10 2
A postNewServerGetIps() 0 21 4
A postNewServerOptionDetails() 0 19 2
B postUpdateServerDetails() 0 28 5
B postUpdateContainerDetails() 0 23 4
A postUpdateServerToggleBuild() 0 20 2
B postUpdateServerUpdateBuild() 0 32 4
A deleteServer() 18 20 3
A postToggleInstall() 18 18 3
A postUpdateServerStartup() 20 20 3
A postDatabase() 0 23 3
A postSuspendServer() 18 18 3
A postUnsuspendServer() 18 18 3
B postQueuedDeletionHandler() 0 31 6
A postNewServerServiceOption() 0 12 2

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 Log;
28
use Alert;
29
use Pterodactyl\Models;
30
use Illuminate\Http\Request;
31
use Pterodactyl\Exceptions\DisplayException;
32
use Pterodactyl\Http\Controllers\Controller;
33
use Pterodactyl\Repositories\ServerRepository;
34
use Pterodactyl\Repositories\DatabaseRepository;
35
use Pterodactyl\Exceptions\DisplayValidationException;
36
37
class ServersController extends Controller
38
{
39
    /**
40
     * Controller Constructor.
41
     */
42
    public function __construct()
43
    {
44
        //
45
    }
46
47
    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...
48
    {
49
        return view('admin.servers.index', [
50
            '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...
51
        ]);
52
    }
53
54
    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...
55
    {
56
        return view('admin.servers.new', [
57
            'locations' => Models\Location::all(),
58
            'services' => Models\Service::all(),
59
        ]);
60
    }
61
62
    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...
63
    {
64
        $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...
65
            'user', 'option.variables', 'variables',
66
            'node.allocations', 'databases.host'
67
        )->findOrFail($id);
68
69
        $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...
70
            $item->server_value = $server->variables->where('variable_id', $item->id)->pluck('variable_value')->first();
71
72
            return $item;
73
        });
74
75
        return view('admin.servers.view', [
76
            'server' => $server,
77
            'assigned' => $server->node->allocations->where('server_id', $server->id)->sortBy('port')->sortBy('ip'),
78
            'unassigned' => $server->node->allocations->where('server_id', null)->sortBy('port')->sortBy('ip'),
79
            'db_servers' => Models\DatabaseServer::all(),
80
        ]);
81
    }
82
83 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...
84
    {
85
        try {
86
            $server = new ServerRepository;
87
            $response = $server->create($request->except('_token'));
88
89
            return redirect()->route('admin.servers.view', ['id' => $response->id]);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Pterodactyl\Models\Server>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
90
        } catch (DisplayValidationException $ex) {
91
            return redirect()->route('admin.servers.new')->withErrors(json_decode($ex->getMessage()))->withInput();
92
        } catch (DisplayException $ex) {
93
            Alert::danger($ex->getMessage())->flash();
94
95
            return redirect()->route('admin.servers.new')->withInput();
96
        } catch (\Exception $ex) {
97
            Log::error($ex);
98
            Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
99
100
            return redirect()->route('admin.servers.new')->withInput();
101
        }
102
    }
103
104
    /**
105
     * Returns a JSON tree of all avaliable nodes in a given location.
106
     *
107
     * @param  \Illuminate\Http\Request $request
108
     * @return \Illuminate\Contracts\View\View
109
     */
110
    public function postNewServerGetNodes(Request $request)
111
    {
112
        if (! $request->input('location')) {
113
            return response()->json([
114
                'error' => 'Missing location in request.',
115
            ], 500);
116
        }
117
118
        return response()->json(Models\Node::select('id', 'name', 'public')->where('location', $request->input('location'))->get());
119
    }
120
121
    /**
122
     * Returns a JSON tree of all avaliable IPs and Ports on a given node.
123
     *
124
     * @param  \Illuminate\Http\Request $request
125
     * @return \Illuminate\Contracts\View\View
126
     */
127
    public function postNewServerGetIps(Request $request)
128
    {
129
        if (! $request->input('node')) {
130
            return response()->json([
131
                'error' => 'Missing node in request.',
132
            ], 500);
133
        }
134
135
        $ips = Models\Allocation::where('node', $request->input('node'))->whereNull('assigned_to')->get();
136
        $listing = [];
137
138
        foreach ($ips as &$ip) {
139
            if (array_key_exists($ip->ip, $listing)) {
140
                $listing[$ip->ip] = array_merge($listing[$ip->ip], [$ip->port]);
141
            } else {
142
                $listing[$ip->ip] = [$ip->port];
143
            }
144
        }
145
146
        return response()->json($listing);
147
    }
148
149
    /**
150
     * Returns a JSON tree of all avaliable options for a given service.
151
     *
152
     * @param  \Illuminate\Http\Request $request
153
     * @return \Illuminate\Contracts\View\View
154
     */
155
    public function postNewServerServiceOption(Request $request)
156
    {
157
        if (! $request->input('service')) {
158
            return response()->json([
159
                'error' => 'Missing service in request.',
160
            ], 500);
161
        }
162
163
        $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...
164
165
        return response()->json(Models\ServiceOption::select('id', 'name', 'docker_image')->where('service_id', $request->input('service'))->orderBy('name', 'asc')->get());
166
    }
167
168
    /**
169
     * Returns a JSON tree of all avaliable variables for a given service option.
170
     *
171
     * @param  \Illuminate\Http\Request $request
172
     * @return \Illuminate\Contracts\View\View
173
     */
174
    public function postNewServerOptionDetails(Request $request)
175
    {
176
        if (! $request->input('option')) {
177
            return response()->json([
178
                'error' => 'Missing option in request.',
179
            ], 500);
180
        }
181
182
        $option = Models\ServiceOption::with('variables')->with(['packs' => function ($query) {
183
            $query->where('selectable', true);
184
        }])->findOrFail($request->input('option'));
185
186
        return response()->json([
187
            'packs' => $option->packs,
188
            'variables' => $option->variables,
189
            'exec' => $option->display_executable,
190
            'startup' => $option->display_startup,
191
        ]);
192
    }
193
194
    public function postUpdateServerDetails(Request $request, $id)
195
    {
196
        try {
197
            $server = new ServerRepository;
198
            $server->updateDetails($id, [
199
                'owner' => $request->input('owner'),
200
                'name' => $request->input('name'),
201
                '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...
202
            ]);
203
204
            Alert::success('Server details were successfully updated.')->flash();
205
        } catch (DisplayValidationException $ex) {
206
            return redirect()->route('admin.servers.view', [
207
                'id' => $id,
208
                'tab' => 'tab_details',
209
            ])->withErrors(json_decode($ex->getMessage()))->withInput();
210
        } catch (DisplayException $ex) {
211
            Alert::danger($ex->getMessage())->flash();
212
        } catch (\Exception $ex) {
213
            Log::error($ex);
214
            Alert::danger('An unhandled exception occured while attemping to update this server. Please try again.')->flash();
215
        }
216
217
        return redirect()->route('admin.servers.view', [
218
            'id' => $id,
219
            'tab' => 'tab_details',
220
        ])->withInput();
221
    }
222
223
    public function postUpdateContainerDetails(Request $request, $id)
224
    {
225
        try {
226
            $server = new ServerRepository;
227
            $server->updateContainer($id, ['image' => $request->input('docker_image')]);
228
            Alert::success('Successfully updated this server\'s docker image.')->flash();
229
        } catch (DisplayValidationException $ex) {
230
            return redirect()->route('admin.servers.view', [
231
                'id' => $id,
232
                'tab' => 'tab_details',
233
            ])->withErrors(json_decode($ex->getMessage()))->withInput();
234
        } catch (DisplayException $ex) {
235
            Alert::danger($ex->getMessage())->flash();
236
        } catch (\Exception $ex) {
237
            Log::error($ex);
238
            Alert::danger('An unhandled exception occured while attemping to update this server\'s docker image. Please try again.')->flash();
239
        }
240
241
        return redirect()->route('admin.servers.view', [
242
            'id' => $id,
243
            'tab' => 'tab_details',
244
        ]);
245
    }
246
247
    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...
248
    {
249
        $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...
250
251
        try {
252
            $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...
253
                'X-Access-Server' => $server->uuid,
254
                '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...
255
            ])->request('POST', '/server/rebuild');
256
            Alert::success('A rebuild has been queued successfully. It will run the next time this server is booted.')->flash();
257
        } catch (\GuzzleHttp\Exception\TransferException $ex) {
258
            Log::warning($ex);
259
            Alert::danger('An error occured while attempting to toggle a rebuild.')->flash();
260
        }
261
262
        return redirect()->route('admin.servers.view', [
263
            'id' => $id,
264
            'tab' => 'tab_manage',
265
        ]);
266
    }
267
268
    public function postUpdateServerUpdateBuild(Request $request, $id)
269
    {
270
        try {
271
            $server = new ServerRepository;
272
            $server->changeBuild($id, $request->only([
273
                'default', 'add_additional',
274
                'remove_additional', 'memory',
275
                'swap', 'io', 'cpu',
276
            ]));
277
            Alert::success('Server details were successfully updated.')->flash();
278
        } catch (DisplayValidationException $ex) {
279
            return redirect()->route('admin.servers.view', [
280
                'id' => $id,
281
                'tab' => 'tab_build',
282
            ])->withErrors(json_decode($ex->getMessage()))->withInput();
283
        } catch (DisplayException $ex) {
284
            Alert::danger($ex->getMessage())->flash();
285
286
            return redirect()->route('admin.servers.view', [
287
                'id' => $id,
288
                'tab' => 'tab_build',
289
            ]);
290
        } catch (\Exception $ex) {
291
            Log::error($ex);
292
            Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
293
        }
294
295
        return redirect()->route('admin.servers.view', [
296
            'id' => $id,
297
            'tab' => 'tab_build',
298
        ]);
299
    }
300
301 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...
302
    {
303
        try {
304
            $server = new ServerRepository;
305
            $server->deleteServer($id, $force);
306
            Alert::success('Server has been marked for deletion on the system.')->flash();
307
308
            return redirect()->route('admin.servers');
309
        } catch (DisplayException $ex) {
310
            Alert::danger($ex->getMessage())->flash();
311
        } catch (\Exception $ex) {
312
            Log::error($ex);
313
            Alert::danger('An unhandled exception occured while attemping to delete this server. Please try again.')->flash();
314
        }
315
316
        return redirect()->route('admin.servers.view', [
317
            'id' => $id,
318
            'tab' => 'tab_delete',
319
        ]);
320
    }
321
322 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...
323
    {
324
        try {
325
            $server = new ServerRepository;
326
            $server->toggleInstall($id);
327
            Alert::success('Server status was successfully toggled.')->flash();
328
        } catch (DisplayException $ex) {
329
            Alert::danger($ex->getMessage())->flash();
330
        } catch (\Exception $ex) {
331
            Log::error($ex);
332
            Alert::danger('An unhandled exception occured while attemping to toggle this servers status.')->flash();
333
        } finally {
334
            return redirect()->route('admin.servers.view', [
335
                'id' => $id,
336
                'tab' => 'tab_manage',
337
            ]);
338
        }
339
    }
340
341 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...
342
    {
343
        try {
344
            $server = new ServerRepository;
345
            $server->updateStartup($id, $request->except([
346
                '_token',
347
            ]), true);
348
            Alert::success('Server startup variables were successfully updated.')->flash();
349
        } catch (\Pterodactyl\Exceptions\DisplayException $e) {
350
            Alert::danger($e->getMessage())->flash();
351
        } catch (\Exception $e) {
352
            Log::error($e);
353
            Alert::danger('An unhandled exception occured while attemping to update startup variables for this server. Please try again.')->flash();
354
        } finally {
355
            return redirect()->route('admin.servers.view', [
356
                'id' => $id,
357
                'tab' => 'tab_startup',
358
            ])->withInput();
359
        }
360
    }
361
362
    public function postDatabase(Request $request, $id)
363
    {
364
        try {
365
            $repo = new DatabaseRepository;
366
            $repo->create($id, $request->only([
367
                'db_server', 'database', 'remote',
368
            ]));
369
            Alert::success('Added new database to this server.')->flash();
370
        } catch (DisplayValidationException $ex) {
371
            return redirect()->route('admin.servers.view', [
372
                'id' => $id,
373
                'tab' => 'tab_database',
374
            ])->withInput()->withErrors(json_decode($ex->getMessage()))->withInput();
375
        } catch (\Exception $ex) {
376
            Log::error($ex);
377
            Alert::danger('An exception occured while attempting to add a new database for this server.')->flash();
378
        }
379
380
        return redirect()->route('admin.servers.view', [
381
            'id' => $id,
382
            'tab' => 'tab_database',
383
        ])->withInput();
384
    }
385
386 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...
387
    {
388
        try {
389
            $repo = new ServerRepository;
390
            $repo->suspend($id);
391
            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.');
392
        } catch (DisplayException $e) {
393
            Alert::danger($e->getMessage())->flash();
394
        } catch (\Exception $e) {
395
            Log::error($e);
396
            Alert::danger('An unhandled exception occured while attemping to suspend this server. Please try again.')->flash();
397
        } finally {
398
            return redirect()->route('admin.servers.view', [
399
                'id' => $id,
400
                'tab' => 'tab_manage',
401
            ]);
402
        }
403
    }
404
405 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...
406
    {
407
        try {
408
            $repo = new ServerRepository;
409
            $repo->unsuspend($id);
410
            Alert::success('Server has been unsuspended on the system. Access has been re-enabled.');
411
        } catch (DisplayException $e) {
412
            Alert::danger($e->getMessage())->flash();
413
        } catch (\Exception $e) {
414
            Log::error($e);
415
            Alert::danger('An unhandled exception occured while attemping to unsuspend this server. Please try again.')->flash();
416
        } finally {
417
            return redirect()->route('admin.servers.view', [
418
                'id' => $id,
419
                'tab' => 'tab_manage',
420
            ]);
421
        }
422
    }
423
424
    public function postQueuedDeletionHandler(Request $request, $id)
425
    {
426
        try {
427
            $repo = new ServerRepository;
428
            if (! is_null($request->input('cancel'))) {
429
                $repo->cancelDeletion($id);
430
                Alert::success('Server deletion has been cancelled. This server will remain suspended until you unsuspend it.')->flash();
431
432
                return redirect()->route('admin.servers.view', $id);
433
            } elseif (! is_null($request->input('delete'))) {
434
                $repo->deleteNow($id);
435
                Alert::success('Server was successfully deleted from the system.')->flash();
436
437
                return redirect()->route('admin.servers');
438
            } elseif (! is_null($request->input('force_delete'))) {
439
                $repo->deleteNow($id, true);
440
                Alert::success('Server was successfully force deleted from the system.')->flash();
441
442
                return redirect()->route('admin.servers');
443
            }
444
        } catch (DisplayException $ex) {
445
            Alert::danger($ex->getMessage())->flash();
446
447
            return redirect()->route('admin.servers.view', $id);
448
        } catch (\Exception $ex) {
449
            Log::error($ex);
450
            Alert::danger('An unhandled error occured while attempting to perform this action.')->flash();
451
452
            return redirect()->route('admin.servers.view', $id);
453
        }
454
    }
455
}
456