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

NodeController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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\Http\Controllers\API;
26
27
use Log;
28
use Illuminate\Http\Request;
29
use Pterodactyl\Models\Node;
30
use Pterodactyl\Models\Allocation;
31
use Dingo\Api\Exception\ResourceException;
32
use Pterodactyl\Exceptions\DisplayException;
33
use Pterodactyl\Repositories\NodeRepository;
34
use Pterodactyl\Exceptions\DisplayValidationException;
35
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
36
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
37
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
38
39
class NodeController extends BaseController
40
{
41
    /**
42
     * Lists all nodes currently on the system.
43
     *
44
     * @param  Request  $request
45
     * @return array
46
     */
47
    public function index(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 Node::all()->toArray();
50
    }
51
52
    /**
53
     * Create a new node.
54
     *
55
     * @param  Request $request
56
     * @return array
57
     *
58
     * @throws \Pterodactyl\Exceptions\DisplayException
59
     * @throws \Pterodactyl\Exceptions\DisplayValidationException
60
     */
61
    public function create(Request $request)
62
    {
63
        $repo = new NodeRepository;
64
65
        try {
66
            $node = $repo->create(array_merge(
67
                $request->only([
68
                    'public', 'disk_overallocate', 'memory_overallocate',
69
                ]),
70
                $request->intersect([
71
                    'name', 'location_id', 'fqdn',
72
                    'scheme', 'memory', 'disk',
73
                    'daemonBase', 'daemonSFTP', 'daemonListen',
74
                ])
75
            ));
76
77
            return ['id' => $node->id];
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Pterodactyl\Models\Node>. 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...
78
        } catch (DisplayValidationException $ex) {
79
            throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true));
80
        } catch (DisplayException $ex) {
81
            throw new ResourceException($ex->getMessage());
82
        } catch (\Exception $ex) {
83
            Log::error($ex);
84
            throw new BadRequestHttpException('There was an error while attempting to add this node to the system. This error has been logged.');
85
        }
86
    }
87
88
    /**
89
     * Lists specific fields about a server or all fields pertaining to that node.
90
     *
91
     * @param  Request  $request
92
     * @param  int      $id
93
     * @param  string   $fields
94
     * @return array
95
     */
96
    public function view(Request $request, $id, $fields = null)
0 ignored issues
show
Unused Code introduced by
The parameter $fields 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...
97
    {
98
        $node = Node::with('allocations')->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...
99
100
        $node->allocations->transform(function ($item) {
101
            return collect($item)->only([
102
                'id', 'ip', 'ip_alias', 'port', 'server_id',
103
            ]);
104
        });
105
106 View Code Duplication
        if (! empty($request->input('fields'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
107
            $fields = explode(',', $request->input('fields'));
108
            if (! empty($fields) && is_array($fields)) {
109
                return collect($node)->only($fields);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return collect($node)->only($fields); (Illuminate\Support\Collection) is incompatible with the return type documented by Pterodactyl\Http\Control...PI\NodeController::view of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
110
            }
111
        }
112
113
        return $node->toArray();
114
    }
115
116
     /**
117
      * Returns a configuration file for a given node.
118
      *
119
      * @param  Request $request
120
      * @param  int     $id
121
      * @return array
122
      */
123
    public function config(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...
124
    {
125
        $node = Node::findOrFail($id);
126
127
        return $node->getConfigurationAsJson();
128
    }
129
130
     /**
131
      * Returns a listing of all allocations for every node.
132
      *
133
      * @param  Request $request
134
      * @return array
135
      */
136
     public function allocations(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...
137
     {
138
         return Allocation::all()->toArray();
139
     }
140
141
     /**
142
      * Returns a listing of the allocation for the specified server id.
143
      *
144
      * @param  Request $request
145
      * @return array
146
      */
147
     public function allocationsView(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...
148
     {
149
         return Allocation::where('server_id', $id)->get()->toArray();
150
     }
151
152
    /**
153
     * Delete a node.
154
     *
155
     * @param  Request $request
156
     * @param  int     $id
157
     * @return void
158
     */
159 View Code Duplication
    public function delete(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...
160
    {
161
        $repo = new NodeRepository;
162
        try {
163
            $repo->delete($id);
164
165
            return $this->response->noContent();
166
        } catch (DisplayException $ex) {
167
            throw new ResourceException($ex->getMessage());
168
        } catch (\Exception $e) {
169
            throw new ServiceUnavailableHttpException('An error occured while attempting to delete this node.');
170
        }
171
    }
172
}
173