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 (#371)
by Dane
05:00 queued 02:14
created

PackController::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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 Illuminate\Http\Request;
31
use Pterodactyl\Models\Pack;
32
use Pterodactyl\Models\Service;
33
use Pterodactyl\Exceptions\DisplayException;
34
use Pterodactyl\Http\Controllers\Controller;
35
use Pterodactyl\Repositories\PackRepository;
36
use Pterodactyl\Exceptions\DisplayValidationException;
37
38
class PackController extends Controller
39
{
40
    /**
41
     * Display listing of all packs on the system.
42
     *
43
     * @param  \Illuminate\Http\Request  $request
44
     * @return \Illuminate\View\View
45
     */
46 View Code Duplication
    public function index(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...
47
    {
48
        $packs = Pack::with('option')->withCount('servers');
0 ignored issues
show
Bug introduced by
The method withCount 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...
49
50
        if (! is_null($request->input('query'))) {
51
            $packs->search($request->input('query'));
52
        }
53
54
        return view('admin.packs.index', ['packs' => $packs->paginate(50)]);
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.packs.index'...$packs->paginate(50))); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 54 which is incompatible with the return type documented by Pterodactyl\Http\Control...n\PackController::index of type Illuminate\View\View.
Loading history...
55
    }
56
57
    /**
58
     * Display new pack creation form.
59
     *
60
     * @param  \Illuminate\Http\Request  $request
61
     * @return \Illuminate\View\View
62
     */
63
    public function create(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...
64
    {
65
        return view('admin.packs.new', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.packs.new', ...th('options')->get())); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 65 which is incompatible with the return type documented by Pterodactyl\Http\Control...\PackController::create of type Illuminate\View\View.
Loading history...
66
            'services' => Service::with('options')->get(),
0 ignored issues
show
Bug introduced by
The method get 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...
67
        ]);
68
    }
69
70
    /**
71
     * Display new pack creation modal for use with template upload.
72
     *
73
     * @param  \Illuminate\Http\Request  $request
74
     * @return \Illuminate\View\View
75
     */
76
    public function newTemplate(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...
77
    {
78
        return view('admin.packs.modal', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.packs.modal'...th('options')->get())); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 78 which is incompatible with the return type documented by Pterodactyl\Http\Control...Controller::newTemplate of type Illuminate\View\View.
Loading history...
79
            'services' => Service::with('options')->get(),
0 ignored issues
show
Bug introduced by
The method get 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...
80
        ]);
81
    }
82
83
    /**
84
     * Handle create pack request and route user to location.
85
     *
86
     * @param  \Illuminate\Http\Request  $request
87
     * @return \Illuminate\View\View
88
     */
89
    public function store(Request $request)
90
    {
91
        $repo = new PackRepository;
92
93
        try {
94
            if ($request->input('action') === 'from_template') {
95
                $pack = $repo->createWithTemplate($request->intersect(['option_id', 'file_upload']));
96
            } else {
97
                $pack = $repo->create($request->intersect([
98
                    'name', 'description', 'version', 'option_id',
99
                    'selectable', 'visible', 'locked', 'file_upload',
100
                ]));
101
            }
102
            Alert::success('Pack successfully created on the system.')->flash();
103
104
            return redirect()->route('admin.packs.view', $pack->id);
105
        } catch (DisplayValidationException $ex) {
106
            return redirect()->route('admin.packs.new')->withErrors(json_decode($ex->getMessage()))->withInput();
107
        } catch (DisplayException $ex) {
108
            Alert::danger($ex->getMessage())->flash();
109
        } catch (\Exception $ex) {
110
            Log::error($ex);
111
            Alert::danger('An error occured while attempting to add a new service pack. This error has been logged.')->flash();
112
        }
113
114
        return redirect()->route('admin.packs.new')->withInput();
115
    }
116
117
    /**
118
     * Display pack view template to user.
119
     *
120
     * @param  \Illuminate\Http\Request  $request
121
     * @param  int                       $id
122
     * @return \Illuminate\View\View
123
     */
124
    public function view(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...
125
    {
126
        return view('admin.packs.view', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.packs.view',...th('options')->get())); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 126 which is incompatible with the return type documented by Pterodactyl\Http\Control...in\PackController::view of type Illuminate\View\View.
Loading history...
127
            'pack' => Pack::with('servers.node', 'servers.user')->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...
128
            'services' => Service::with('options')->get(),
0 ignored issues
show
Bug introduced by
The method get 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...
129
        ]);
130
    }
131
132
    /**
133
     * Handle updating or deleting pack information.
134
     *
135
     * @param  \Illuminate\Http\Request  $request
136
     * @param  int                       $id
137
     * @return \Illuminate\Http\RedirectResponse
138
     */
139 View Code Duplication
    public function update(Request $request, $id)
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...
140
    {
141
        $repo = new PackRepository;
142
143
        try {
144
            if ($request->input('action') !== 'delete') {
145
                $pack = $repo->update($id, $request->intersect([
0 ignored issues
show
Unused Code introduced by
$pack is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
146
                    'name', 'description', 'version',
147
                    'option_id', 'selectable', 'visible', 'locked',
148
                ]));
149
                Alert::success('Pack successfully updated.')->flash();
150
            } else {
151
                $repo->delete($id);
152
                Alert::success('Pack was successfully deleted from the system.')->flash();
153
154
                return redirect()->route('admin.packs');
155
            }
156
        } catch (DisplayValidationException $ex) {
157
            return redirect()->route('admin.packs.view', $id)->withErrors(json_decode($ex->getMessage()));
0 ignored issues
show
Documentation introduced by
$id is of type integer, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
158
        } catch (DisplayException $ex) {
159
            Alert::danger($ex->getMessage())->flash();
160
        } catch (\Exception $ex) {
161
            Log::error($ex);
162
            Alert::danger('An error occured while attempting to edit this service pack. This error has been logged.')->flash();
163
        }
164
165
        return redirect()->route('admin.packs.view', $id);
0 ignored issues
show
Documentation introduced by
$id is of type integer, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
166
    }
167
168
    /**
169
     * Creates an archive of the pack and downloads it to the browser.
170
     *
171
     * @param  \Illuminate\Http\Request  $request
172
     * @param  int                       $id
173
     * @param  bool                      $files
174
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
175
     */
176
    public function export(Request $request, $id, $files = false)
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...
177
    {
178
        $pack = Pack::findOrFail($id);
179
        $json = [
180
            'name' => $pack->name,
181
            'version' => $pack->version,
182
            'description' => $pack->description,
183
            'selectable' => $pack->selectable,
184
            'visible' => $pack->visible,
185
            'locked' => $pack->locked,
186
        ];
187
188
        $filename = tempnam(sys_get_temp_dir(), 'pterodactyl_');
189
        if ($files === 'with-files') {
190
            $zip = new \ZipArchive;
191
            if (! $zip->open($filename, \ZipArchive::CREATE)) {
192
                abort(503, 'Unable to open file for writing.');
193
            }
194
195
            $files = Storage::files('packs/' . $pack->uuid);
0 ignored issues
show
Bug introduced by
The method files() does not seem to exist on object<Illuminate\Contracts\Filesystem\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
196
            foreach ($files as $file) {
197
                $zip->addFile(storage_path('app/' . $file), basename(storage_path('app/' . $file)));
198
            }
199
200
            $zip->addFromString('import.json', json_encode($json, JSON_PRETTY_PRINT));
201
            $zip->close();
202
203
            return response()->download($filename, 'pack-' . $pack->name . '.zip')->deleteFileAfterSend(true);
204
        } else {
205
            $fp = fopen($filename, 'a+');
206
            fwrite($fp, json_encode($json, JSON_PRETTY_PRINT));
207
            fclose($fp);
208
209
            return response()->download($filename, 'pack-' . $pack->name . '.json', [
210
                'Content-Type' => 'application/json',
211
            ])->deleteFileAfterSend(true);
212
        }
213
    }
214
}
215