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 ( 95171a...008ccc )
by Dane
02:59
created

Server::js()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 16
nc 4
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\Models;
26
27
use Auth;
28
use Cache;
29
use Javascript;
30
use Illuminate\Database\Eloquent\Model;
31
use Illuminate\Notifications\Notifiable;
32
use Illuminate\Database\Eloquent\SoftDeletes;
33
34
class Server extends Model
35
{
36
    use Notifiable, SoftDeletes;
37
38
    /**
39
     * The table associated with the model.
40
     *
41
     * @var string
42
     */
43
    protected $table = 'servers';
44
45
    /**
46
     * The attributes excluded from the model's JSON form.
47
     *
48
     * @var array
49
     */
50
    protected $hidden = ['daemonSecret', 'sftp_password'];
51
52
    /**
53
     * The attributes that should be mutated to dates.
54
     *
55
     * @var array
56
     */
57
    protected $dates = ['deleted_at'];
58
59
    /**
60
     * Fields that are not mass assignable.
61
     *
62
     * @var array
63
     */
64
    protected $guarded = ['id', 'installed', 'created_at', 'updated_at', 'deleted_at'];
65
66
     /**
67
      * Cast values to correct type.
68
      *
69
      * @var array
70
      */
71
     protected $casts = [
72
         'node_id' => 'integer',
73
         'suspended' => 'integer',
74
         'owner_id' => 'integer',
75
         'memory' => 'integer',
76
         'swap' => 'integer',
77
         'disk' => 'integer',
78
         'io' => 'integer',
79
         'cpu' => 'integer',
80
         'oom_disabled' => 'integer',
81
         'allocation_id' => 'integer',
82
         'service_id' => 'integer',
83
         'option_id' => 'integer',
84
         'pack_id' => 'integer',
85
         'installed' => 'integer',
86
     ];
87
88
    /**
89
     * Returns a single server specified by UUID.
90
     * DO NOT USE THIS TO MODIFY SERVER DETAILS OR SAVE THOSE DETAILS.
91
     * YOU WILL OVERWRITE THE SECRET KEY AND BREAK THINGS.
92
     *
93
     * @param  string $uuid The Short-UUID of the server to return an object about.
94
     * @return \Illuminate\Database\Eloquent\Collection
95
     */
96
    public static function byUuid($uuid)
97
    {
98
        // Results are cached because we call this functions a few times on page load.
99
        $result = Cache::remember('Server.byUuid.' . $uuid, 60, function () use ($uuid) {
100
            $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...
101
                $q->where('uuidShort', $uuid)->orWhere('uuid', $uuid);
102
            });
103
104
            if (! Auth::user()->isRootAdmin()) {
105
                $query->whereIn('id', Auth::user()->serverAccessArray());
106
            }
107
108
            return $query->first();
109
        });
110
111
        if (! is_null($result)) {
112
            $result->daemonSecret = Auth::user()->daemonToken($result);
113
        }
114
115
        return $result;
116
    }
117
118
    /**
119
     * Returns non-administrative headers for accessing a server on the daemon.
120
     *
121
     * @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...
122
     * @return array
123
     */
124
    public function guzzleHeaders()
125
    {
126
        return [
127
            '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...
128
            'X-Access-Token' => Auth::user()->daemonToken($this),
129
        ];
130
    }
131
132
    /**
133
     * Return an instance of the Guzzle client for this specific server using defined access token.
134
     *
135
     * @return \GuzzleHttp\Client
136
     */
137
    public function guzzleClient()
138
    {
139
        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...
140
    }
141
142
    /**
143
     * Returns javascript object to be embedded on server view pages with relevant information.
144
     *
145
     * @return \Laracasts\Utilities\JavaScript\JavaScriptFacade
146
     */
147
    public function js($additional = null, $overwrite = null)
148
    {
149
        $response = [
150
            'server' => collect($this->makeVisible('daemonSecret'))->only([
151
                'uuid',
152
                'uuidShort',
153
                'daemonSecret',
154
                'username',
155
            ]),
156
            '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...
157
                'fqdn',
158
                'scheme',
159
                'daemonListen',
160
            ]),
161
        ];
162
163
        if (is_array($additional)) {
164
            $response = array_merge($response, $additional);
165
        }
166
167
        if (is_array($overwrite)) {
168
            $response = $overwrite;
169
        }
170
171
        return Javascript::put($response);
172
    }
173
174
    /**
175
     * Gets the user who owns the server.
176
     *
177
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
178
     */
179
    public function user()
180
    {
181
        return $this->belongsTo(User::class, 'owner_id');
182
    }
183
184
    /**
185
     * Gets the subusers associated with a server.
186
     *
187
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
188
     */
189
    public function subusers()
190
    {
191
        return $this->hasMany(Subuser::class);
192
    }
193
194
    /**
195
     * Gets the default allocation for a server.
196
     *
197
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
198
     */
199
    public function allocation()
200
    {
201
        return $this->hasOne(Allocation::class, 'id', 'allocation_id');
202
    }
203
204
    /**
205
     * Gets all allocations associated with this server.
206
     *
207
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
208
     */
209
    public function allocations()
210
    {
211
        return $this->hasMany(Allocation::class, 'server_id');
212
    }
213
214
    /**
215
     * Gets information for the pack associated with this server.
216
     *
217
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
218
     */
219
    public function pack()
220
    {
221
        return $this->hasOne(ServicePack::class, 'id', 'pack_id');
222
    }
223
224
    /**
225
     * Gets information for the service associated with this server.
226
     *
227
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
228
     */
229
    public function service()
230
    {
231
        return $this->belongsTo(Service::class);
232
    }
233
234
    /**
235
     * Gets information for the service option associated with this server.
236
     *
237
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
238
     */
239
    public function option()
240
    {
241
        return $this->belongsTo(ServiceOption::class);
242
    }
243
244
    /**
245
     * Gets information for the service variables associated with this server.
246
     *
247
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
248
     */
249
    public function variables()
250
    {
251
        return $this->hasMany(ServerVariable::class);
252
    }
253
254
    /**
255
     * Gets information for the node associated with this server.
256
     *
257
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
258
     */
259
    public function node()
260
    {
261
        return $this->belongsTo(Node::class);
262
    }
263
264
    /**
265
     * Gets information for the tasks associated with this server.
266
     *
267
     * @TODO adjust server column in tasks to be server_id
268
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
269
     */
270
    public function tasks()
271
    {
272
        return $this->hasMany(Task::class, 'server', 'id');
273
    }
274
275
    /**
276
     * Gets all databases associated with a server.
277
     *
278
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
279
     */
280
    public function databases()
281
    {
282
        return $this->hasMany(Database::class);
283
    }
284
}
285