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
03:00
created

ServiceController::postService()   B

Complexity

Conditions 4
Paths 10

Size

Total Lines 23
Code Lines 18

Duplication

Lines 23
Ratio 100 %

Importance

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