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.

ServiceRepository::delete()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 2
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\Repositories;
26
27
use DB;
28
use Validator;
29
use Pterodactyl\Models\Service;
30
use Pterodactyl\Exceptions\DisplayException;
31
use Pterodactyl\Exceptions\DisplayValidationException;
32
33
class ServiceRepository
34
{
35
    /**
36
     * Creates a new service on the system.
37
     *
38
     * @param  array  $data
39
     * @return \Pterodactyl\Models\Service
40
     *
41
     * @throws \Pterodactyl\Exceptions\DisplayValidationException
42
     */
43
    public function create(array $data)
44
    {
45
        $validator = Validator::make($data, [
46
            'name' => 'required|string|min:1|max:255',
47
            'description' => 'required|nullable|string',
48
            'folder' => 'required|unique:services,folder|regex:/^[\w.-]{1,50}$/',
49
            'startup' => 'required|nullable|string',
50
        ]);
51
52
        if ($validator->fails()) {
53
            throw new DisplayValidationException(json_encode($validator->errors()));
54
        }
55
56
        return DB::transaction(function () use ($data) {
57
            $service = new Service;
58
            $service->author = config('pterodactyl.service.author');
59
            $service->fill([
60
                'name' => $data['name'],
61
                'description' => (isset($data['description'])) ? $data['description'] : null,
62
                'folder' => $data['folder'],
63
                'startup' => (isset($data['startup'])) ? $data['startup'] : null,
64
                'index_file' => Service::defaultIndexFile(),
65
            ])->save();
66
67
            // It is possible for an event to return false or throw an exception
68
            // which won't necessarily be detected by this transaction.
69
            //
70
            // This check ensures the model was actually saved.
71
            if (! $service->exists) {
72
                throw new \Exception('Service model was created however the response appears to be invalid. Did an event fire wrongly?');
73
            }
74
75
            return $service;
76
        });
77
    }
78
79
    /**
80
     * Updates a service.
81
     *
82
     * @param  int    $id
83
     * @param  array  $data
84
     * @return \Pterodactyl\Models\Service
85
     *
86
     * @throws \Pterodactyl\Exceptions\DisplayValidationException
87
     */
88
    public function update($id, array $data)
89
    {
90
        $service = Service::findOrFail($id);
91
92
        $validator = Validator::make($data, [
93
            'name' => 'sometimes|required|string|min:1|max:255',
94
            'description' => 'sometimes|required|nullable|string',
95
            'folder' => 'sometimes|required|regex:/^[\w.-]{1,50}$/',
96
            'startup' => 'sometimes|required|nullable|string',
97
            'index_file' => 'sometimes|required|string',
98
        ]);
99
100
        if ($validator->fails()) {
101
            throw new DisplayValidationException(json_encode($validator->errors()));
102
        }
103
104
        return DB::transaction(function () use ($data, $service) {
105
            $service->fill($data)->save();
106
107
            return $service;
108
        });
109
    }
110
111
    /**
112
     * Deletes a service and associated files and options.
113
     *
114
     * @param  int   $id
115
     * @return void
116
     *
117
     * @throws \Pterodactyl\Exceptions\DisplayException
118
     */
119 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...
120
    {
121
        $service = Service::withCount('servers')->with('options')->findOrFail($id);
122
123
        if ($service->servers_count > 0) {
124
            throw new DisplayException('You cannot delete a service that has servers associated with it.');
125
        }
126
127
        DB::transaction(function () use ($service) {
128
            foreach ($service->options as $option) {
129
                (new OptionRepository)->delete($option->id);
130
            }
131
132
            $service->delete();
133
        });
134
    }
135
}
136