Conditions | 34 |
Paths | 9005 |
Total Lines | 261 |
Code Lines | 162 |
Lines | 14 |
Ratio | 5.36 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
85 | public function create(array $data) |
||
86 | { |
||
87 | |||
88 | // Validate Fields |
||
89 | $validator = Validator::make($data, [ |
||
90 | 'user_id' => 'required|exists:users,id', |
||
91 | 'name' => 'required|regex:/^([\w .-]{1,200})$/', |
||
92 | 'description' => 'sometimes|nullable|string', |
||
93 | 'memory' => 'required|numeric|min:0', |
||
94 | 'swap' => 'required|numeric|min:-1', |
||
95 | 'io' => 'required|numeric|min:10|max:1000', |
||
96 | 'cpu' => 'required|numeric|min:0', |
||
97 | 'disk' => 'required|numeric|min:0', |
||
98 | 'service_id' => 'required|numeric|min:1|exists:services,id', |
||
99 | 'option_id' => 'required|numeric|min:1|exists:service_options,id', |
||
100 | 'location_id' => 'required|numeric|min:1|exists:locations,id', |
||
101 | 'pack_id' => 'sometimes|nullable|numeric|min:0', |
||
102 | 'custom_container' => 'string', |
||
103 | 'startup' => 'string', |
||
104 | 'auto_deploy' => 'sometimes|required|accepted', |
||
105 | 'custom_id' => 'sometimes|required|numeric|unique:servers,id', |
||
106 | ]); |
||
107 | |||
108 | $validator->sometimes('node_id', 'required|numeric|min:1|exists:nodes,id', function ($input) { |
||
109 | return ! ($input->auto_deploy); |
||
110 | }); |
||
111 | |||
112 | $validator->sometimes('allocation_id', 'required|numeric|exists:allocations,id', function ($input) { |
||
113 | return ! ($input->auto_deploy); |
||
114 | }); |
||
115 | |||
116 | $validator->sometimes('allocation_additional.*', 'sometimes|required|numeric|exists:allocations,id', function ($input) { |
||
117 | return ! ($input->auto_deploy); |
||
118 | }); |
||
119 | |||
120 | // Run validator, throw catchable and displayable exception if it fails. |
||
121 | // Exception includes a JSON result of failed validation rules. |
||
122 | if ($validator->fails()) { |
||
123 | throw new DisplayValidationException(json_encode($validator->errors())); |
||
124 | } |
||
125 | |||
126 | $user = Models\User::findOrFail($data['user_id']); |
||
127 | |||
128 | $autoDeployed = false; |
||
129 | if (isset($data['auto_deploy']) && $data['auto_deploy']) { |
||
130 | // This is an auto-deployment situation |
||
131 | // Ignore any other passed node data |
||
132 | unset($data['node_id'], $data['allocation_id']); |
||
133 | |||
134 | $autoDeployed = true; |
||
135 | $node = DeploymentService::smartRandomNode($data['memory'], $data['disk'], $data['location_id']); |
||
136 | $allocation = DeploymentService::randomAllocation($node->id); |
||
137 | } else { |
||
138 | $node = Models\Node::findOrFail($data['node_id']); |
||
139 | } |
||
140 | |||
141 | // Verify IP & Port are a.) free and b.) assigned to the node. |
||
142 | // We know the node exists because of 'exists:nodes,id' in the validation |
||
143 | if (! $autoDeployed) { |
||
144 | $allocation = Models\Allocation::where('id', $data['allocation_id'])->where('node_id', $data['node_id'])->whereNull('server_id')->first(); |
||
145 | } |
||
146 | |||
147 | // Something failed in the query, either that combo doesn't exist, or it is in use. |
||
148 | if (! $allocation) { |
||
|
|||
149 | throw new DisplayException('The selected Allocation ID is either already in use, or unavaliable for this node.'); |
||
150 | } |
||
151 | |||
152 | // Validate those Service Option Variables |
||
153 | // We know the service and option exists because of the validation. |
||
154 | // We need to verify that the option exists for the service, and then check for |
||
155 | // any required variable fields. (fields are labeled env_<env_variable>) |
||
156 | $option = Models\ServiceOption::where('id', $data['option_id'])->where('service_id', $data['service_id'])->first(); |
||
157 | if (! $option) { |
||
158 | throw new DisplayException('The requested service option does not exist for the specified service.'); |
||
159 | } |
||
160 | |||
161 | // Validate the Pack |
||
162 | if (! isset($data['pack_id']) || (int) $data['pack_id'] < 1) { |
||
163 | $data['pack_id'] = null; |
||
164 | } else { |
||
165 | $pack = Models\Pack::where('id', $data['pack_id'])->where('option_id', $data['option_id'])->first(); |
||
166 | if (! $pack) { |
||
167 | throw new DisplayException('The requested service pack does not seem to exist for this combination.'); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | // Load up the Service Information |
||
172 | $service = Models\Service::find($option->service_id); |
||
173 | |||
174 | // Check those Variables |
||
175 | $variables = Models\ServiceVariable::where('option_id', $data['option_id'])->get(); |
||
176 | $variableList = []; |
||
177 | if ($variables) { |
||
178 | foreach ($variables as $variable) { |
||
179 | |||
180 | // Is the variable required? |
||
181 | if (! isset($data['env_' . $variable->env_variable])) { |
||
182 | if ($variable->required) { |
||
183 | throw new DisplayException('A required service option variable field (env_' . $variable->env_variable . ') was missing from the request.'); |
||
184 | } |
||
185 | $variableList[] = [ |
||
186 | 'id' => $variable->id, |
||
187 | 'env' => $variable->env_variable, |
||
188 | 'val' => $variable->default_value, |
||
189 | ]; |
||
190 | continue; |
||
191 | } |
||
192 | |||
193 | // Check aganist Regex Pattern |
||
194 | if (! is_null($variable->regex) && ! preg_match($variable->regex, $data['env_' . $variable->env_variable])) { |
||
195 | throw new DisplayException('Failed to validate service option variable field (env_' . $variable->env_variable . ') aganist regex (' . $variable->regex . ').'); |
||
196 | } |
||
197 | |||
198 | $variableList[] = [ |
||
199 | 'id' => $variable->id, |
||
200 | 'env' => $variable->env_variable, |
||
201 | 'val' => $data['env_' . $variable->env_variable], |
||
202 | ]; |
||
203 | continue; |
||
204 | } |
||
205 | } |
||
206 | |||
207 | // Check Overallocation |
||
208 | if (! $autoDeployed) { |
||
209 | if (is_numeric($node->memory_overallocate) || is_numeric($node->disk_overallocate)) { |
||
210 | $totals = Models\Server::select(DB::raw('SUM(memory) as memory, SUM(disk) as disk'))->where('node_id', $node->id)->first(); |
||
211 | |||
212 | // Check memory limits |
||
213 | View Code Duplication | if (is_numeric($node->memory_overallocate)) { |
|
214 | $newMemory = $totals->memory + $data['memory']; |
||
215 | $memoryLimit = ($node->memory * (1 + ($node->memory_overallocate / 100))); |
||
216 | if ($newMemory > $memoryLimit) { |
||
217 | throw new DisplayException('The amount of memory allocated to this server would put the node over its allocation limits. This node is allowed ' . ($node->memory_overallocate + 100) . '% of its assigned ' . $node->memory . 'Mb of memory (' . $memoryLimit . 'Mb) of which ' . (($totals->memory / $node->memory) * 100) . '% (' . $totals->memory . 'Mb) is in use already. By allocating this server the node would be at ' . (($newMemory / $node->memory) * 100) . '% (' . $newMemory . 'Mb) usage.'); |
||
218 | } |
||
219 | } |
||
220 | |||
221 | // Check Disk Limits |
||
222 | View Code Duplication | if (is_numeric($node->disk_overallocate)) { |
|
223 | $newDisk = $totals->disk + $data['disk']; |
||
224 | $diskLimit = ($node->disk * (1 + ($node->disk_overallocate / 100))); |
||
225 | if ($newDisk > $diskLimit) { |
||
226 | throw new DisplayException('The amount of disk allocated to this server would put the node over its allocation limits. This node is allowed ' . ($node->disk_overallocate + 100) . '% of its assigned ' . $node->disk . 'Mb of disk (' . $diskLimit . 'Mb) of which ' . (($totals->disk / $node->disk) * 100) . '% (' . $totals->disk . 'Mb) is in use already. By allocating this server the node would be at ' . (($newDisk / $node->disk) * 100) . '% (' . $newDisk . 'Mb) usage.'); |
||
227 | } |
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | |||
232 | DB::beginTransaction(); |
||
233 | |||
234 | try { |
||
235 | $uuid = new UuidService; |
||
236 | |||
237 | // Add Server to the Database |
||
238 | $server = new Models\Server; |
||
239 | $genUuid = $uuid->generate('servers', 'uuid'); |
||
240 | $genShortUuid = $uuid->generateShort('servers', 'uuidShort', $genUuid); |
||
241 | |||
242 | if (isset($data['custom_id'])) { |
||
243 | $server->id = $data['custom_id']; |
||
244 | } |
||
245 | |||
246 | $server->fill([ |
||
247 | 'uuid' => $genUuid, |
||
248 | 'uuidShort' => $genShortUuid, |
||
249 | 'node_id' => $node->id, |
||
250 | 'name' => $data['name'], |
||
251 | 'description' => $data['description'], |
||
252 | 'suspended' => 0, |
||
253 | 'owner_id' => $user->id, |
||
254 | 'memory' => $data['memory'], |
||
255 | 'swap' => $data['swap'], |
||
256 | 'disk' => $data['disk'], |
||
257 | 'io' => $data['io'], |
||
258 | 'cpu' => $data['cpu'], |
||
259 | 'oom_disabled' => (isset($data['oom_disabled'])) ? true : false, |
||
260 | 'allocation_id' => $allocation->id, |
||
261 | 'service_id' => $data['service_id'], |
||
262 | 'option_id' => $data['option_id'], |
||
263 | 'pack_id' => $data['pack_id'], |
||
264 | 'startup' => $data['startup'], |
||
265 | 'daemonSecret' => $uuid->generate('servers', 'daemonSecret'), |
||
266 | 'image' => (isset($data['custom_container']) && ! empty($data['custom_container'])) ? $data['custom_container'] : $option->docker_image, |
||
267 | 'username' => $this->generateSFTPUsername($data['name'], $genShortUuid), |
||
268 | 'sftp_password' => Crypt::encrypt('not set'), |
||
269 | ]); |
||
270 | $server->save(); |
||
271 | |||
272 | // Mark Allocation in Use |
||
273 | $allocation->server_id = $server->id; |
||
274 | $allocation->save(); |
||
275 | |||
276 | // Add Additional Allocations |
||
277 | if (isset($data['allocation_additional']) && is_array($data['allocation_additional'])) { |
||
278 | foreach ($data['allocation_additional'] as $allocation) { |
||
279 | $model = Models\Allocation::where('id', $allocation)->where('node_id', $data['node_id'])->whereNull('server_id')->first(); |
||
280 | if (! $model) { |
||
281 | continue; |
||
282 | } |
||
283 | |||
284 | $model->server_id = $server->id; |
||
285 | $model->save(); |
||
286 | } |
||
287 | } |
||
288 | |||
289 | // Add Variables |
||
290 | $environmentVariables = [ |
||
291 | 'STARTUP' => $data['startup'], |
||
292 | ]; |
||
293 | |||
294 | foreach ($variableList as $item) { |
||
295 | $environmentVariables[$item['env']] = $item['val']; |
||
296 | |||
297 | Models\ServerVariable::create([ |
||
298 | 'server_id' => $server->id, |
||
299 | 'variable_id' => $item['id'], |
||
300 | 'variable_value' => $item['val'], |
||
301 | ]); |
||
302 | } |
||
303 | |||
304 | $server->load('allocation', 'allocations'); |
||
305 | $node->guzzleClient(['X-Access-Token' => $node->daemonSecret])->request('POST', '/servers', [ |
||
306 | 'json' => [ |
||
307 | 'uuid' => (string) $server->uuid, |
||
308 | 'user' => $server->username, |
||
309 | 'build' => [ |
||
310 | 'default' => [ |
||
311 | 'ip' => $server->allocation->ip, |
||
312 | 'port' => $server->allocation->port, |
||
313 | ], |
||
314 | 'ports' => $server->allocations->groupBy('ip')->map(function ($item) { |
||
315 | return $item->pluck('port'); |
||
316 | })->toArray(), |
||
317 | 'env' => $environmentVariables, |
||
318 | 'memory' => (int) $server->memory, |
||
319 | 'swap' => (int) $server->swap, |
||
320 | 'io' => (int) $server->io, |
||
321 | 'cpu' => (int) $server->cpu, |
||
322 | 'disk' => (int) $server->disk, |
||
323 | 'image' => $server->image, |
||
324 | ], |
||
325 | 'service' => [ |
||
326 | 'type' => $service->folder, |
||
327 | 'option' => $option->tag, |
||
328 | 'pack' => (isset($pack)) ? $pack->uuid : null, |
||
329 | ], |
||
330 | 'keys' => [ |
||
331 | (string) $server->daemonSecret => $this->daemonPermissions, |
||
332 | ], |
||
333 | 'rebuild' => false, |
||
334 | 'start_on_completion' => isset($data['start_on_completion']), |
||
335 | ], |
||
336 | ]); |
||
337 | |||
338 | DB::commit(); |
||
339 | |||
340 | return $server; |
||
341 | } catch (\Exception $ex) { |
||
342 | DB::rollBack(); |
||
343 | throw $ex; |
||
344 | } |
||
345 | } |
||
346 | |||
858 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: