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
04:53 queued 02:03
created

ServiceController::getNew()   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\Admin;
26
27
use Log;
28
use Alert;
29
use Storage;
30
use Pterodactyl\Models;
31
use Illuminate\Http\Request;
32
use Pterodactyl\Exceptions\DisplayException;
33
use Pterodactyl\Http\Controllers\Controller;
34
use Pterodactyl\Repositories\ServiceRepository;
35
use Pterodactyl\Exceptions\DisplayValidationException;
36
37
class ServiceController extends Controller
38
{
39
    public function __construct()
40
    {
41
        //
42
    }
43
44
    public function getIndex(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...
45
    {
46
        return view('admin.services.index', [
47
            'services' => Models\Service::withCount('servers')->get(),
48
        ]);
49
    }
50
51
    public function getNew(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...
52
    {
53
        return view('admin.services.new');
54
    }
55
56 View Code Duplication
    public function postNew(Request $request)
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...
57
    {
58
        try {
59
            $repo = new ServiceRepository\Service;
60
            $service = $repo->create($request->only([
61
                'name', 'description', 'file',
62
                'executable', 'startup',
63
            ]));
64
            Alert::success('Successfully created new service!')->flash();
65
66
            return redirect()->route('admin.services.service', $service->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Pterodactyl\Models\Service>. 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...
67
        } catch (DisplayValidationException $ex) {
68
            return redirect()->route('admin.services.new')->withErrors(json_decode($ex->getMessage()))->withInput();
69
        } catch (DisplayException $ex) {
70
            Alert::danger($ex->getMessage())->flash();
71
        } catch (\Exception $ex) {
72
            Log::error($ex);
73
            Alert::danger('An error occured while attempting to add a new service.')->flash();
74
        }
75
76
        return redirect()->route('admin.services.new')->withInput();
77
    }
78
79
    public function getService(Request $request, $service)
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...
80
    {
81
        return view('admin.services.view', [
82
            'service' => Models\Service::with('options', 'options.servers')->findOrFail($service),
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...
83
        ]);
84
    }
85
86
    public function postService(Request $request, $service)
87
    {
88
        try {
89
            $repo = new ServiceRepository\Service;
90
            $repo->update($service, $request->only([
91
                'name', 'description', 'file',
92
                'executable', 'startup',
93
            ]));
94
            Alert::success('Successfully updated this service.')->flash();
95
        } catch (DisplayValidationException $ex) {
96
            return redirect()->route('admin.services.service', $service)->withErrors(json_decode($ex->getMessage()))->withInput();
97
        } catch (DisplayException $ex) {
98
            Alert::danger($ex->getMessage())->flash();
99
        } catch (\Exception $ex) {
100
            Log::error($ex);
101
            Alert::danger('An error occurred while attempting to update this service.')->flash();
102
        }
103
104
        return redirect()->route('admin.services.service', $service)->withInput();
105
    }
106
107 View Code Duplication
    public function deleteService(Request $request, $service)
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...
108
    {
109
        try {
110
            $repo = new ServiceRepository\Service;
111
            $repo->delete($service);
112
            Alert::success('Successfully deleted that service.')->flash();
113
114
            return redirect()->route('admin.services');
115
        } catch (DisplayException $ex) {
116
            Alert::danger($ex->getMessage())->flash();
117
        } catch (\Exception $ex) {
118
            Log::error($ex);
119
            Alert::danger('An error was encountered while attempting to delete that service.')->flash();
120
        }
121
122
        return redirect()->route('admin.services.service', $service);
123
    }
124
125
    public function getOption(Request $request, $service, $option)
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...
Unused Code introduced by
The parameter $service 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...
126
    {
127
        $option = Models\ServiceOption::with('service', 'variables')->findOrFail($option);
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...
128
        $option->setRelation('servers', $option->servers()->with('user')->paginate(25));
129
130
        return view('admin.services.options.view', ['option' => $option]);
131
    }
132
133
    public function postOption(Request $request, $service, $option)
134
    {
135
        try {
136
            $repo = new ServiceRepository\Option;
137
            $repo->update($option, $request->only([
138
                'name', 'description', 'tag',
139
                'executable', 'docker_image', 'startup',
140
            ]));
141
            Alert::success('Option settings successfully updated.')->flash();
142
        } catch (DisplayValidationException $ex) {
143
            return redirect()->route('admin.services.option', [$service, $option])->withErrors(json_decode($ex->getMessage()))->withInput();
144
        } catch (\Exception $ex) {
145
            Log::error($ex);
146
            Alert::danger('An error occured while attempting to modify this option.')->flash();
147
        }
148
149
        return redirect()->route('admin.services.option', [$service, $option])->withInput();
150
    }
151
152 View Code Duplication
    public function deleteOption(Request $request, $service, $option)
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...
153
    {
154
        try {
155
            $repo = new ServiceRepository\Option;
156
            $repo->delete($option);
157
158
            Alert::success('Successfully deleted that option.')->flash();
159
160
            return redirect()->route('admin.services.service', $service);
161
        } catch (DisplayException $ex) {
162
            Alert::danger($ex->getMessage())->flash();
163
        } catch (\Exception $ex) {
164
            Log::error($ex);
165
            Alert::danger('An error was encountered while attempting to delete this option.')->flash();
166
        }
167
168
        return redirect()->route('admin.services.option', [$service, $option]);
169
    }
170
171
    public function postOptionVariable(Request $request, $service, $option, $variable)
172
    {
173
        try {
174
            $repo = new ServiceRepository\Variable;
175
176
            // Because of the way old() works on the display side we prefix all of the variables with thier ID
177
            // We need to remove that prefix here since the repo doesn't want it.
178
            $data = [
179
                'user_viewable' => '0',
180
                'user_editable' => '0',
181
                'required' => '0',
182
            ];
183
            foreach ($request->except(['_token']) as $id => $val) {
184
                $data[str_replace($variable . '_', '', $id)] = $val;
185
            }
186
            $repo->update($variable, $data);
187
            Alert::success('Successfully updated variable.')->flash();
188
        } catch (DisplayValidationException $ex) {
189
            $data = [];
190
            foreach (json_decode($ex->getMessage(), true) as $id => $val) {
191
                $data[$variable . '_' . $id] = $val;
192
            }
193
194
            return redirect()->route('admin.services.option', [$service, $option])->withErrors((object) $data)->withInput();
195
        } catch (DisplayException $ex) {
196
            Alert::danger($ex->getMessage())->flash();
197
        } catch (\Exception $ex) {
198
            Log::error($ex);
199
            Alert::danger('An error occurred while attempting to update this service.')->flash();
200
        }
201
202
        return redirect()->route('admin.services.option', [$service, $option])->withInput();
203
    }
204
205
    public function getNewVariable(Request $request, $service, $option)
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...
Unused Code introduced by
The parameter $service 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...
206
    {
207
        return view('admin.services.options.variable', [
208
            'option' => Models\ServiceOption::with('service')->findOrFail($option),
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...
209
        ]);
210
    }
211
212
    public function postNewVariable(Request $request, $service, $option)
213
    {
214
        try {
215
            $repo = new ServiceRepository\Variable;
216
            $repo->create($option, $request->only([
217
                'name', 'description', 'env_variable',
218
                'default_value', 'user_viewable',
219
                'user_editable', 'required', 'regex',
220
            ]));
221
            Alert::success('Successfully added new variable to this option.')->flash();
222
223
            return redirect()->route('admin.services.option', [$service, $option]);
224
        } catch (DisplayValidationException $ex) {
225
            return redirect()->route('admin.services.option.variable.new', [$service, $option])->withErrors(json_decode($ex->getMessage()))->withInput();
226
        } catch (DisplayException $ex) {
227
            Alert::danger($ex->getMessage())->flash();
228
        } catch (\Exception $ex) {
229
            Log::error($ex);
230
            Alert::danger('An error occurred while attempting to add this variable.')->flash();
231
        }
232
233
        return redirect()->route('admin.services.option.variable.new', [$service, $option])->withInput();
234
    }
235
236
    public function newOption(Request $request, $service)
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...
237
    {
238
        return view('admin.services.options.new', [
239
            'service' => Models\Service::findOrFail($service),
240
        ]);
241
    }
242
243
    public function postNewOption(Request $request, $service)
244
    {
245
        try {
246
            $repo = new ServiceRepository\Option;
247
            $id = $repo->create($service, $request->except([
248
                '_token',
249
            ]));
250
            Alert::success('Successfully created new service option.')->flash();
251
252
            return redirect()->route('admin.services.option', [$service, $id]);
253
        } catch (DisplayValidationException $ex) {
254
            return redirect()->route('admin.services.option.new', $service)->withErrors(json_decode($ex->getMessage()))->withInput();
255
        } catch (\Exception $ex) {
256
            Log::error($ex);
257
            Alert::danger('An error occured while attempting to add this service option.')->flash();
258
        }
259
260
        return redirect()->route('admin.services.option.new', $service)->withInput();
261
    }
262
263
    public function deleteVariable(Request $request, $service, $option, $variable)
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...
264
    {
265
        try {
266
            $repo = new ServiceRepository\Variable;
267
            $repo->delete($variable);
268
            Alert::success('Deleted variable.')->flash();
269
        } catch (DisplayException $ex) {
270
            Alert::danger($ex->getMessage())->flash();
271
        } catch (\Exception $ex) {
272
            Log::error($ex);
273
            Alert::danger('An error occured while attempting to delete that variable.')->flash();
274
        }
275
276
        return redirect()->route('admin.services.option', [$service, $option]);
277
    }
278
279
    public function getConfiguration(Request $request, $serviceId)
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...
280
    {
281
        $service = Models\Service::findOrFail($serviceId);
282
283
        return view('admin.services.config', [
284
            'service' => $service,
285
            'contents' => [
286
                'json' => Storage::get('services/' . $service->file . '/main.json'),
287
                'index' => Storage::get('services/' . $service->file . '/index.js'),
288
            ],
289
        ]);
290
    }
291
292
    public function postConfiguration(Request $request, $serviceId)
293
    {
294
        try {
295
            $repo = new ServiceRepository\Service;
296
            $repo->updateFile($serviceId, $request->only(['file', 'contents']));
297
298
            return response('', 204);
299
        } catch (DisplayException $ex) {
300
            return response()->json([
301
                'error' => $ex->getMessage(),
302
            ], 503);
303
        } catch (\Exception $ex) {
304
            Log::error($ex);
305
306
            return response()->json([
307
                'error' => 'An error occured while attempting to save the file.',
308
            ], 503);
309
        }
310
    }
311
}
312