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 ( 77b1a2...30b493 )
by Dane
02:52
created

OptionRepository   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 171
Duplicated Lines 15.2 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 21
lcom 0
cbo 4
dl 26
loc 171
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B create() 5 28 4
A delete() 16 16 3
C update() 5 48 8
B scripts() 0 35 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Repositories;
26
27
use DB;
28
use Validator;
29
use Pterodactyl\Models\ServiceOption;
30
use Pterodactyl\Exceptions\DisplayException;
31
use Pterodactyl\Exceptions\DisplayValidationException;
32
33
class OptionRepository
34
{
35
    /**
36
     * Creates a new service option on the system.
37
     *
38
     * @param  array  $data
39
     * @return \Pterodactyl\Models\ServiceOption
40
     *
41
     * @throws \Pterodactyl\Exceptions\DisplayException
42
     * @throws \Pterodactyl\Exceptions\DisplayValidationException
43
     */
44
    public function create(array $data)
45
    {
46
        $validator = Validator::make($data, [
47
            'service_id' => 'required|numeric|exists:services,id',
48
            'name' => 'required|string|max:255',
49
            'description' => 'required|string',
50
            'tag' => 'required|string|max:255|unique:service_options,tag',
51
            'docker_image' => 'required|string|max:255',
52
            'startup' => 'sometimes|nullable|string',
53
            'config_from' => 'sometimes|required|numeric|exists:service_options,id',
54
            'config_startup' => 'required_without:config_from|json',
55
            'config_stop' => 'required_without:config_from|string|max:255',
56
            'config_logs' => 'required_without:config_from|json',
57
            'config_files' => 'required_without:config_from|json',
58
        ]);
59
60
        if ($validator->fails()) {
61
            throw new DisplayValidationException(json_encode($validator->errors()));
62
        }
63
64 View Code Duplication
        if (isset($data['config_from'])) {
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...
65
            if (! ServiceOption::where('service_id', $data['service_id'])->where('id', $data['config_from'])->first()) {
66
                throw new DisplayException('The `configuration from` directive must be a child of the assigned service.');
67
            }
68
        }
69
70
        return ServiceOption::create($data);
71
    }
72
73
    /**
74
     * Deletes a service option from the system.
75
     *
76
     * @param  int  $id
77
     * @return void
78
     *
79
     * @throws \Pterodactyl\Exceptions\DisplayException
80
     */
81 View Code Duplication
    public function delete($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...
82
    {
83
        $option = ServiceOption::with('variables')->withCount('servers')->findOrFail($id);
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...
84
85
        if ($option->servers_count > 0) {
86
            throw new DisplayException('You cannot delete a service option that has servers associated with it.');
87
        }
88
89
        DB::transaction(function () use ($option) {
90
            foreach ($option->variables as $variable) {
91
                (new VariableRepository)->delete($variable->id);
92
            }
93
94
            $option->delete();
95
        });
96
    }
97
98
    /**
99
     * Updates a service option in the database which can then be used
100
     * on nodes.
101
     *
102
     * @param  int    $id
103
     * @param  array  $data
104
     * @return \Pterodactyl\Models\ServiceOption
105
     *
106
     * @throws \Pterodactyl\Exceptions\DisplayException
107
     * @throws \Pterodactyl\Exceptions\DisplayValidationException
108
     */
109
    public function update($id, array $data)
110
    {
111
        $option = ServiceOption::findOrFail($id);
112
113
        // Due to code limitations (at least when I am writing this currently)
114
        // we have to make an assumption that if config_from is not passed
115
        // that we should be telling it that no config is wanted anymore.
116
        //
117
        // This really is only an issue if we open API access to this function,
118
        // in which case users will always need to pass `config_from` in order
119
        // to keep it assigned.
120
        if (! isset($data['config_from']) && ! is_null($option->config_from)) {
121
            $option->config_from = null;
122
        }
123
124
        $validator = Validator::make($data, [
125
            'name' => 'sometimes|required|string|max:255',
126
            'description' => 'sometimes|required|string',
127
            'tag' => 'sometimes|required|string|max:255|unique:service_options,tag,' . $option->id,
128
            'docker_image' => 'sometimes|required|string|max:255',
129
            'startup' => 'sometimes|required|string',
130
            'config_from' => 'sometimes|required|numeric|exists:service_options,id',
131
        ]);
132
133
        $validator->sometimes([
134
            'config_startup', 'config_logs', 'config_files',
135
        ], 'required_without:config_from|json', function ($input) use ($option) {
136
            return ! (! $input->config_from && ! is_null($option->config_from));
137
        });
138
139
        $validator->sometimes('config_stop', 'required_without:config_from|string|max:255', function ($input) use ($option) {
140
            return ! (! $input->config_from && ! is_null($option->config_from));
141
        });
142
143
        if ($validator->fails()) {
144
            throw new DisplayValidationException(json_encode($validator->errors()));
145
        }
146
147 View Code Duplication
        if (isset($data['config_from'])) {
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...
148
            if (! ServiceOption::where('service_id', $option->service_id)->where('id', $data['config_from'])->first()) {
149
                throw new DisplayException('The `configuration from` directive must be a child of the assigned service.');
150
            }
151
        }
152
153
        $option->fill($data)->save();
154
155
        return $option;
156
    }
157
158
    /**
159
     * Updates a service option's scripts in the database.
160
     *
161
     * @param  int    $id
162
     * @param  array  $data
163
     * @return \Pterodactyl\Models\ServiceOption
164
     *
165
     * @throws \Pterodactyl\Exceptions\DisplayException
166
     * @throws \Pterodactyl\Exceptions\DisplayValidationException
167
     */
168
    public function scripts($id, array $data)
169
    {
170
        $option = ServiceOption::findOrFail($id);
171
172
        $data['script_install'] = empty($data['script_install']) ? null : $data['script_install'];
173
174
        $validator = Validator::make($data, [
175
            'script_install' => 'sometimes|nullable|string',
176
            'script_is_privileged' => 'sometimes|required|boolean',
177
            'script_entry' => 'sometimes|required|string',
178
            'script_container' => 'sometimes|required|string',
179
            'copy_script_from' => 'sometimes|nullable|numeric',
180
        ]);
181
182
        if (isset($data['copy_script_from']) && ! empty($data['copy_script_from'])) {
183
            $select = ServiceOption::whereNull('copy_script_from')->where([
184
                ['id', $data['copy_script_from']],
185
                ['service_id', $option->service_id],
186
            ])->first();
187
188
            if (! $select) {
189
                throw new DisplayException('The service option selected to copy a script from either does not exist, or is copying from a higher level.');
190
            }
191
        } else {
192
            $data['copy_script_from'] = null;
193
        }
194
195
        if ($validator->fails()) {
196
            throw new DisplayValidationException(json_encode($validator->errors()));
197
        }
198
199
        $option->fill($data)->save();
200
201
        return $option;
202
    }
203
}
204