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.

VariableRepository   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 16
lcom 0
cbo 3
dl 0
loc 138
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B create() 0 44 6
A delete() 0 12 2
C update() 0 50 8
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\Models\ServiceVariable;
31
use Pterodactyl\Exceptions\DisplayException;
32
use Pterodactyl\Exceptions\DisplayValidationException;
33
34
class VariableRepository
35
{
36
    /**
37
     * Create a new service variable.
38
     *
39
     * @param  int    $option
40
     * @param  array  $data
41
     * @return \Pterodactyl\Models\ServiceVariable
42
     *
43
     * @throws \Pterodactyl\Exceptions\DisplayException
44
     * @throws \Pterodactyl\Exceptions\DisplayValidationException
45
     */
46
    public function create($option, array $data)
47
    {
48
        $option = ServiceOption::select('id')->findOrFail($option);
49
50
        $validator = Validator::make($data, [
51
            'name' => 'required|string|min:1|max:255',
52
            'description' => 'sometimes|nullable|string',
53
            'env_variable' => 'required|regex:/^[\w]{1,255}$/',
54
            'default_value' => 'string',
55
            'options' => 'sometimes|required|array',
56
            'rules' => 'bail|required|string',
57
        ]);
58
59
        // Ensure the default value is allowed by the rules provided.
60
        $validator->sometimes('default_value', $data['rules'] ?? null, function ($input) {
61
            return $input->default_value;
62
        });
63
64
        if ($validator->fails()) {
65
            throw new DisplayValidationException(json_encode($validator->errors()));
66
        }
67
68
        if (in_array($data['env_variable'], ServiceVariable::reservedNames())) {
69
            throw new DisplayException('The environment variable name provided is a reserved keyword for the daemon.');
70
        }
71
72
        $search = ServiceVariable::where('env_variable', $data['env_variable'])->where('option_id', $option->id);
73
        if ($search->first()) {
74
            throw new DisplayException('The envionment variable name assigned to this variable must be unique for this service option.');
75
        }
76
77
        if (! isset($data['options']) || ! is_array($data['options'])) {
78
            $data['options'] = [];
79
        }
80
81
        $data['option_id'] = $option->id;
82
        $data['user_viewable'] = (in_array('user_viewable', $data['options']));
83
        $data['user_editable'] = (in_array('user_editable', $data['options']));
84
85
        // Remove field that isn't used.
86
        unset($data['options']);
87
88
        return ServiceVariable::create($data);
89
    }
90
91
    /**
92
     * Deletes a specified option variable as well as all server
93
     * variables currently assigned.
94
     *
95
     * @param  int  $id
96
     * @return void
97
     */
98
    public function delete($id)
99
    {
100
        $variable = ServiceVariable::with('serverVariable')->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...
101
102
        DB::transaction(function () use ($variable) {
103
            foreach ($variable->serverVariable as $v) {
104
                $v->delete();
105
            }
106
107
            $variable->delete();
108
        });
109
    }
110
111
    /**
112
     * Updates a given service variable.
113
     *
114
     * @param  int    $id
115
     * @param  array  $data
116
     * @return \Pterodactyl\Models\ServiceVariable
117
     *
118
     * @throws \Pterodactyl\Exceptions\DisplayException
119
     * @throws \Pterodactyl\Exceptions\DisplayValidationException
120
     */
121
    public function update($id, array $data)
122
    {
123
        $variable = ServiceVariable::findOrFail($id);
124
125
        $validator = Validator::make($data, [
126
            'name' => 'sometimes|required|string|min:1|max:255',
127
            'description' => 'sometimes|nullable|string',
128
            'env_variable' => 'sometimes|required|regex:/^[\w]{1,255}$/',
129
            'default_value' => 'string',
130
            'options' => 'sometimes|required|array',
131
            'rules' => 'bail|sometimes|required|string|min:1',
132
        ]);
133
134
        // Ensure the default value is allowed by the rules provided.
135
        $rules = (isset($data['rules'])) ? $data['rules'] : $variable->rules;
136
        $validator->sometimes('default_value', $rules, function ($input) {
137
            return $input->default_value;
138
        });
139
140
        if ($validator->fails()) {
141
            throw new DisplayValidationException(json_encode($validator->errors()));
142
        }
143
144
        if (isset($data['env_variable'])) {
145
            if (in_array($data['env_variable'], ServiceVariable::reservedNames())) {
146
                throw new DisplayException('The environment variable name provided is a reserved keyword for the daemon.');
147
            }
148
149
            $search = ServiceVariable::where('env_variable', $data['env_variable'])
150
                ->where('option_id', $variable->option_id)
151
                ->where('id', '!=', $variable->id);
152
            if ($search->first()) {
153
                throw new DisplayException('The envionment variable name assigned to this variable must be unique for this service option.');
154
            }
155
        }
156
157
        if (! isset($data['options']) || ! is_array($data['options'])) {
158
            $data['options'] = [];
159
        }
160
161
        $data['user_viewable'] = (in_array('user_viewable', $data['options']));
162
        $data['user_editable'] = (in_array('user_editable', $data['options']));
163
164
        // Remove field that isn't used.
165
        unset($data['options']);
166
167
        $variable->fill($data)->save();
168
169
        return $variable;
170
    }
171
}
172