GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( 4e916c...5e2777 )
by Dane
02:47
created

ServerController::power()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 14

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
dl 24
loc 24
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 2
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\API\User;
26
27
use Log;
28
use Illuminate\Http\Request;
29
use Pterodactyl\Models\Server;
30
use Pterodactyl\Http\Controllers\API\BaseController;
31
32
class ServerController extends BaseController
33
{
34
    public function info(Request $request, $uuid)
35
    {
36
        $server = Server::byUuid($uuid)->load('allocations');
37
38
        try {
39
            $response = $server->guzzleClient()->request('GET', '/server');
40
41
            $json = json_decode($response->getBody());
42
            $daemon = [
43
                'status' => $json->status,
44
                'stats' => $json->proc,
45
            ];
46
        } catch (\Exception $ex) {
47
            $daemon = [
48
                'error' => 'An error was encountered while trying to connect to the daemon to collect information. It might be offline.',
49
            ];
50
            Log::error($ex);
51
        }
52
53
        return [
54
            'uuidShort' => $server->uuidShort,
0 ignored issues
show
Bug introduced by
The property uuidShort does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
55
            'uuid' => $server->uuid,
0 ignored issues
show
Bug introduced by
The property uuid does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
56
            'name' => $server->name,
0 ignored issues
show
Bug introduced by
The property name does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
57
            'node' => $server->node->name,
0 ignored issues
show
Bug introduced by
The property node does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
58
            'limits' => [
59
                'memory' => $server->memory,
0 ignored issues
show
Bug introduced by
The property memory does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
60
                'swap' => $server->swap,
0 ignored issues
show
Bug introduced by
The property swap does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
61
                'disk' => $server->disk,
0 ignored issues
show
Bug introduced by
The property disk does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
62
                'io' => $server->io,
0 ignored issues
show
Bug introduced by
The property io does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
63
                'cpu' => $server->cpu,
0 ignored issues
show
Bug introduced by
The property cpu does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
64
                'oom_disabled' => (bool) $server->oom_disabled,
0 ignored issues
show
Bug introduced by
The property oom_disabled does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
65
            ],
66
            'allocations' => $server->allocations->map(function ($item) use ($server) {
0 ignored issues
show
Bug introduced by
The property allocations does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
67
                return [
68
                    'ip' => $item->alias,
69
                    'port' => $item->port,
70
                    'default' => ($item->id === $server->allocation_id),
71
                ];
72
            }),
73
            'sftp' => [
74
                'username' => ($request->user()->can('view-sftp', $server)) ? $server->username : null,
0 ignored issues
show
Bug introduced by
The property username does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
75
            ],
76
            'daemon' => [
77
                'token' => $server->daemonSecret,
0 ignored issues
show
Bug introduced by
The property daemonSecret does not seem to exist in Illuminate\Database\Eloquent\Collection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
78
                'response' => $daemon,
79
            ],
80
        ];
81
    }
82
83 View Code Duplication
    public function power(Request $request, $uuid)
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
        $server = Server::byUuid($uuid);
86
        $request->user()->can('power-' . $request->input('action'), $server);
87
88
        if (empty($request->input('action'))) {
89
            return $this->response()->error([
0 ignored issues
show
Documentation introduced by
array('error' => 'An act...ssed to this request.') is of type array<string,string,{"error":"string"}>, but the function expects a string.

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...
90
                'error' => 'An action must be passed to this request.',
91
            ], 422);
92
        }
93
94
        $res = $server->guzzleClient()->request('PUT', '/server/power', [
95
            'exceptions' => false,
96
            'json' => [
97
                'action' => $request->input('action'),
98
            ],
99
        ]);
100
101
        if ($res->getStatusCode() !== 204) {
102
            return $this->response->error(json_decode($res->getBody())->error, $res->getStatusCode());
103
        }
104
105
        return $this->response->noContent();
106
    }
107
108 View Code Duplication
    public function command(Request $request, $uuid)
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...
109
    {
110
        $server = Server::byUuid($uuid);
111
        $request->user()->can('send-command', $server);
112
113
        if (empty($request->input('command'))) {
114
            return $this->response()->error([
0 ignored issues
show
Documentation introduced by
array('error' => 'A comm...ssed to this request.') is of type array<string,string,{"error":"string"}>, but the function expects a string.

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...
115
                'error' => 'A command must be passed to this request.',
116
            ], 422);
117
        }
118
119
        $res = $server->guzzleClient()->request('POST', '/server/command', [
120
            'exceptions' => false,
121
            'json' => [
122
                'command' => $request->input('command'),
123
            ],
124
        ]);
125
126
        if ($res->getStatusCode() !== 204) {
127
            return $this->response->error(json_decode($res->getBody())->error, $res->getStatusCode());
128
        }
129
130
        return $this->response->noContent();
131
    }
132
}
133