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 (#334)
by Dane
05:31 queued 02:47
created

Server   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 271
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

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

15 Methods

Rating   Name   Duplication   Size   Complexity  
B byUuid() 0 25 4
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 Cache;
29
use Carbon;
30
use Javascript;
31
use Illuminate\Database\Eloquent\Model;
32
use Illuminate\Notifications\Notifiable;
33
use Illuminate\Database\Eloquent\SoftDeletes;
34
use Nicolaslopezj\Searchable\SearchableTrait;
35
36
class Server extends Model
37
{
38
    use Notifiable, SearchableTrait, SoftDeletes;
39
40
    /**
41
     * The table associated with the model.
42
     *
43
     * @var string
44
     */
45
    protected $table = 'servers';
46
47
    /**
48
     * The attributes excluded from the model's JSON form.
49
     *
50
     * @var array
51
     */
52
    protected $hidden = ['daemonSecret', 'sftp_password'];
53
54
    /**
55
     * The attributes that should be mutated to dates.
56
     *
57
     * @var array
58
     */
59
    protected $dates = ['deleted_at'];
60
61
    /**
62
     * Fields that are not mass assignable.
63
     *
64
     * @var array
65
     */
66
    protected $guarded = ['id', 'installed', 'created_at', 'updated_at', 'deleted_at'];
67
68
     /**
69
      * Cast values to correct type.
70
      *
71
      * @var array
72
      */
73
     protected $casts = [
74
         'node_id' => 'integer',
75
         'suspended' => 'integer',
76
         'owner_id' => 'integer',
77
         'memory' => 'integer',
78
         'swap' => 'integer',
79
         'disk' => 'integer',
80
         'io' => 'integer',
81
         'cpu' => 'integer',
82
         'oom_disabled' => 'integer',
83
         'allocation_id' => 'integer',
84
         'service_id' => 'integer',
85
         'option_id' => 'integer',
86
         'pack_id' => 'integer',
87
         'installed' => 'integer',
88
     ];
89
90
    protected $searchable = [
91
         'columns' => [
92
             'servers.name' => 10,
93
             'servers.username' => 10,
94
             'servers.uuidShort' => 9,
95
             'servers.uuid' => 8,
96
             'users.email' => 6,
97
             'users.username' => 6,
98
             'nodes.name' => 2,
99
         ],
100
         'joins' => [
101
            'users' => ['users.id', 'servers.owner_id'],
102
            'nodes' => ['nodes.id', 'servers.node_id'],
103
         ],
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
        if (! Auth::check()) {
117
            throw new \Exception('You must call Server:byUuid as an authenticated user.');
118
        }
119
120
        // Results are cached because we call this functions a few times on page load.
121
        $result = Cache::tags(['Model:Server', 'Model:Server:byUuid:' . $uuid])->remember('Model:Server:byUuid:' . $uuid . Auth::user()->uuid, Carbon::now()->addMinutes(15), function () use ($uuid) {
122
            $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...
123
                $q->where('uuidShort', $uuid)->orWhere('uuid', $uuid);
124
            });
125
126
            if (! Auth::user()->isRootAdmin()) {
127
                $query->whereIn('id', Auth::user()->serverAccessArray());
128
            }
129
130
            return $query->first();
131
        });
132
133
        if (! is_null($result)) {
134
            $result->daemonSecret = Auth::user()->daemonToken($result);
135
        }
136
137
        return $result;
138
    }
139
140
    /**
141
     * Returns non-administrative headers for accessing a server on the daemon.
142
     *
143
     * @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...
144
     * @return array
145
     */
146
    public function guzzleHeaders()
147
    {
148
        return [
149
            '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...
150
            'X-Access-Token' => Auth::user()->daemonToken($this),
151
        ];
152
    }
153
154
    /**
155
     * Return an instance of the Guzzle client for this specific server using defined access token.
156
     *
157
     * @return \GuzzleHttp\Client
158
     */
159
    public function guzzleClient()
160
    {
161
        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...
162
    }
163
164
    /**
165
     * Returns javascript object to be embedded on server view pages with relevant information.
166
     *
167
     * @return \Laracasts\Utilities\JavaScript\JavaScriptFacade
168
     */
169
    public function js($additional = null, $overwrite = null)
170
    {
171
        $response = [
172
            'server' => collect($this->makeVisible('daemonSecret'))->only([
173
                'uuid',
174
                'uuidShort',
175
                'daemonSecret',
176
                'username',
177
            ]),
178
            '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...
179
                'fqdn',
180
                'scheme',
181
                'daemonListen',
182
            ]),
183
        ];
184
185
        if (is_array($additional)) {
186
            $response = array_merge($response, $additional);
187
        }
188
189
        if (is_array($overwrite)) {
190
            $response = $overwrite;
191
        }
192
193
        return Javascript::put($response);
194
    }
195
196
    /**
197
     * Gets the user who owns the server.
198
     *
199
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
200
     */
201
    public function user()
202
    {
203
        return $this->belongsTo(User::class, 'owner_id');
204
    }
205
206
    /**
207
     * Gets the subusers associated with a server.
208
     *
209
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
210
     */
211
    public function subusers()
212
    {
213
        return $this->hasMany(Subuser::class);
214
    }
215
216
    /**
217
     * Gets the default allocation for a server.
218
     *
219
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
220
     */
221
    public function allocation()
222
    {
223
        return $this->hasOne(Allocation::class, 'id', 'allocation_id');
224
    }
225
226
    /**
227
     * Gets all allocations associated with this server.
228
     *
229
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
230
     */
231
    public function allocations()
232
    {
233
        return $this->hasMany(Allocation::class, 'server_id');
234
    }
235
236
    /**
237
     * Gets information for the pack associated with this server.
238
     *
239
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
240
     */
241
    public function pack()
242
    {
243
        return $this->hasOne(ServicePack::class, 'id', 'pack_id');
244
    }
245
246
    /**
247
     * Gets information for the service associated with this server.
248
     *
249
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
250
     */
251
    public function service()
252
    {
253
        return $this->belongsTo(Service::class);
254
    }
255
256
    /**
257
     * Gets information for the service option associated with this server.
258
     *
259
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
260
     */
261
    public function option()
262
    {
263
        return $this->belongsTo(ServiceOption::class);
264
    }
265
266
    /**
267
     * Gets information for the service variables associated with this server.
268
     *
269
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
270
     */
271
    public function variables()
272
    {
273
        return $this->hasMany(ServerVariable::class);
274
    }
275
276
    /**
277
     * Gets information for the node associated with this server.
278
     *
279
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
280
     */
281
    public function node()
282
    {
283
        return $this->belongsTo(Node::class);
284
    }
285
286
    /**
287
     * Gets information for the tasks associated with this server.
288
     *
289
     * @TODO adjust server column in tasks to be server_id
290
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
291
     */
292
    public function tasks()
293
    {
294
        return $this->hasMany(Task::class, 'server', 'id');
295
    }
296
297
    /**
298
     * Gets all databases associated with a server.
299
     *
300
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
301
     */
302
    public function databases()
303
    {
304
        return $this->hasMany(Database::class);
305
    }
306
}
307