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

Server   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 267
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 20
lcom 2
cbo 4
dl 0
loc 267
rs 10
c 3
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A byUuid() 0 18 3
A guzzleHeaders() 0 7 1
A guzzleClient() 0 4 1
B js() 0 26 3
A user() 0 4 1
A subusers() 0 4 1
A allocation() 0 4 1
A allocations() 0 4 1
A pack() 0 4 1
A service() 0 4 1
A option() 0 4 1
A variables() 0 4 1
A node() 0 4 1
A tasks() 0 4 1
A databases() 0 4 1
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\Models;
26
27
use Auth;
28
use Javascript;
29
use Illuminate\Database\Eloquent\Model;
30
use Illuminate\Notifications\Notifiable;
31
use Illuminate\Database\Eloquent\SoftDeletes;
32
33
class Server extends Model
34
{
35
    use Notifiable, SoftDeletes;
36
37
    /**
38
     * The table associated with the model.
39
     *
40
     * @var string
41
     */
42
    protected $table = 'servers';
43
44
    /**
45
     * The attributes excluded from the model's JSON form.
46
     *
47
     * @var array
48
     */
49
    protected $hidden = ['daemonSecret', 'sftp_password'];
50
51
    /**
52
     * The attributes that should be mutated to dates.
53
     *
54
     * @var array
55
     */
56
    protected $dates = ['deleted_at'];
57
58
    /**
59
     * Fields that are not mass assignable.
60
     *
61
     * @var array
62
     */
63
    protected $guarded = ['id', 'installed', 'created_at', 'updated_at', 'deleted_at'];
64
65
     /**
66
      * Cast values to correct type.
67
      *
68
      * @var array
69
      */
70
     protected $casts = [
71
         'node_id' => 'integer',
72
         'suspended' => 'integer',
73
         'owner_id' => 'integer',
74
         'memory' => 'integer',
75
         'swap' => 'integer',
76
         'disk' => 'integer',
77
         'io' => 'integer',
78
         'cpu' => 'integer',
79
         'oom_disabled' => 'integer',
80
         'allocation_id' => 'integer',
81
         'service_id' => 'integer',
82
         'option_id' => 'integer',
83
         'pack_id' => 'integer',
84
         'installed' => 'integer',
85
     ];
86
87
    /**
88
     * @var array
89
     */
90
    protected static $serverUUIDInstance = [];
91
92
    /**
93
     * @var mixed
94
     */
95
    protected static $user;
96
97
    /**
98
     * Constructor.
99
     */
100
    public function __construct()
101
    {
102
        parent::__construct();
103
        self::$user = Auth::user();
104
    }
105
106
    /**
107
     * Returns a single server specified by UUID.
108
     * DO NOT USE THIS TO MODIFY SERVER DETAILS OR SAVE THOSE DETAILS.
109
     * YOU WILL OVERWRITE THE SECRET KEY AND BREAK THINGS.
110
     *
111
     * @param  string $uuid The Short-UUID of the server to return an object about.
112
     * @return \Illuminate\Database\Eloquent\Collection
113
     */
114
    public static function byUuid($uuid)
115
    {
116
        $query = self::with('service', 'node')->where(function ($q) use ($uuid) {
0 ignored issues
show
Bug introduced by
The method where 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...
117
            $q->where('uuidShort', $uuid)->orWhere('uuid', $uuid);
118
        });
119
120
        if (! Auth::user()->isRootAdmin()) {
121
            $query->whereIn('id', Auth::user()->serverAccessArray());
122
        }
123
124
        $result = $query->first();
125
126
        if (! is_null($result)) {
127
            $result->daemonSecret = Auth::user()->daemonToken($result);
128
        }
129
130
        return $result;
131
    }
132
133
    /**
134
     * Returns non-administrative headers for accessing a server on the daemon.
135
     *
136
     * @param  string $uuid
0 ignored issues
show
Bug introduced by
There is no parameter named $uuid. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
137
     * @return array
138
     */
139
    public function guzzleHeaders()
140
    {
141
        return [
142
            'X-Access-Server' => $this->uuid,
0 ignored issues
show
Documentation introduced by
The property uuid 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...
143
            'X-Access-Token' => Auth::user()->daemonToken($this),
144
        ];
145
    }
146
147
    /**
148
     * Return an instance of the Guzzle client for this specific server using defined access token.
149
     *
150
     * @return \GuzzleHttp\Client
151
     */
152
    public function guzzleClient()
153
    {
154
        return $this->node->guzzleClient($this->guzzleHeaders());
0 ignored issues
show
Documentation introduced by
The property node 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...
155
    }
156
157
    /**
158
     * Returns javascript object to be embedded on server view pages with relevant information.
159
     *
160
     * @return \Laracasts\Utilities\JavaScript\JavaScriptFacade
161
     */
162
    public function js($additional = null, $overwrite = null)
163
    {
164
        $response = [
165
            'server' => collect($this->makeVisible('daemonSecret'))->only([
166
                'uuid',
167
                'uuidShort',
168
                'daemonSecret',
169
                'username',
170
            ]),
171
            'node' => collect($this->node)->only([
0 ignored issues
show
Documentation introduced by
The property node 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...
172
                'fqdn',
173
                'scheme',
174
                'daemonListen',
175
            ]),
176
        ];
177
178
        if (is_array($additional)) {
179
            $response = array_merge($response, $additional);
180
        }
181
182
        if (is_array($overwrite)) {
183
            $response = $overwrite;
184
        }
185
186
        return Javascript::put($response);
187
    }
188
189
    /**
190
     * Gets the user who owns the server.
191
     *
192
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
193
     */
194
    public function user()
195
    {
196
        return $this->belongsTo(User::class, 'owner_id');
197
    }
198
199
    /**
200
     * Gets the subusers associated with a server.
201
     *
202
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
203
     */
204
    public function subusers()
205
    {
206
        return $this->hasMany(Subuser::class);
207
    }
208
209
    /**
210
     * Gets the default allocation for a server.
211
     *
212
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
213
     */
214
    public function allocation()
215
    {
216
        return $this->hasOne(Allocation::class, 'id', 'allocation_id');
217
    }
218
219
    /**
220
     * Gets all allocations associated with this server.
221
     *
222
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
223
     */
224
    public function allocations()
225
    {
226
        return $this->hasMany(Allocation::class, 'server_id');
227
    }
228
229
    /**
230
     * Gets information for the pack associated with this server.
231
     *
232
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
233
     */
234
    public function pack()
235
    {
236
        return $this->hasOne(ServicePack::class, 'id', 'pack_id');
237
    }
238
239
    /**
240
     * Gets information for the service associated with this server.
241
     *
242
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
243
     */
244
    public function service()
245
    {
246
        return $this->belongsTo(Service::class);
247
    }
248
249
    /**
250
     * Gets information for the service option associated with this server.
251
     *
252
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
253
     */
254
    public function option()
255
    {
256
        return $this->belongsTo(ServiceOption::class);
257
    }
258
259
    /**
260
     * Gets information for the service variables associated with this server.
261
     *
262
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
263
     */
264
    public function variables()
265
    {
266
        return $this->hasMany(ServerVariable::class);
267
    }
268
269
    /**
270
     * Gets information for the node associated with this server.
271
     *
272
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
273
     */
274
    public function node()
275
    {
276
        return $this->belongsTo(Node::class);
277
    }
278
279
    /**
280
     * Gets information for the tasks associated with this server.
281
     *
282
     * @TODO adjust server column in tasks to be server_id
283
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
284
     */
285
    public function tasks()
286
    {
287
        return $this->hasMany(Task::class, 'server', 'id');
288
    }
289
290
    /**
291
     * Gets all databases associated with a server.
292
     *
293
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
294
     */
295
    public function databases()
296
    {
297
        return $this->hasMany(Database::class);
298
    }
299
}
300