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.

PackRepository   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 204
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 4
dl 0
loc 204
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
D update() 0 34 9
A delete() 0 13 2
B create() 0 53 8
C createWithTemplate() 0 63 9
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 Uuid;
29
use Storage;
30
use Validator;
31
use Pterodactyl\Models\Pack;
32
use Pterodactyl\Services\UuidService;
33
use Pterodactyl\Exceptions\DisplayException;
34
use Pterodactyl\Exceptions\DisplayValidationException;
35
36
class PackRepository
37
{
38
    /**
39
     * Creates a new pack on the system.
40
     *
41
     * @param  array  $data
42
     * @return \Pterodactyl\Models\Pack
43
     *
44
     * @throws \Pterodactyl\Exceptions\DisplayException
45
     * @throws \Pterodactyl\Exceptions\DisplayValidationException
46
     */
47
    public function create(array $data)
48
    {
49
        $validator = Validator::make($data, [
50
            'name' => 'required|string',
51
            'version' => 'required|string',
52
            'description' => 'sometimes|nullable|string',
53
            'selectable' => 'sometimes|required|boolean',
54
            'visible' => 'sometimes|required|boolean',
55
            'locked' => 'sometimes|required|boolean',
56
            'option_id' => 'required|exists:service_options,id',
57
        ]);
58
59
        if ($validator->fails()) {
60
            throw new DisplayValidationException(json_encode($validator->errors()));
61
        }
62
63
        if (isset($data['file_upload'])) {
64
            if (! $data['file_upload']->isValid()) {
65
                throw new DisplayException('The file provided does not appear to be valid.');
66
            }
67
68
            if (! in_array($data['file_upload']->getMimeType(), ['application/gzip', 'application/x-gzip'])) {
69
                throw new DisplayException('The file provided (' . $data['file_upload']->getMimeType() . ') does not meet the required filetype of application/gzip.');
70
            }
71
        }
72
73
        return DB::transaction(function () use ($data) {
74
            $uuid = new UuidService();
75
76
            $pack = new Pack;
77
            $pack->uuid = $uuid->generate('packs', 'uuid');
78
            $pack->fill([
79
                'option_id' => $data['option_id'],
80
                'name' => $data['name'],
81
                'version' => $data['version'],
82
                'description' => (empty($data['description'])) ? null : $data['description'],
83
                'selectable' => isset($data['selectable']),
84
                'visible' => isset($data['visible']),
85
                'locked' => isset($data['locked']),
86
            ])->save();
87
88
            if (! $pack->exists) {
89
                throw new DisplayException('Model does not exist after creation. Did an event prevent it from saving?');
90
            }
91
92
            Storage::makeDirectory('packs/' . $pack->uuid);
0 ignored issues
show
Bug introduced by
The method makeDirectory() 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...
93
            if (isset($data['file_upload'])) {
94
                $data['file_upload']->storeAs('packs/' . $pack->uuid, 'archive.tar.gz');
95
            }
96
97
            return $pack;
98
        });
99
    }
100
101
    /**
102
     * Creates a new pack on the system given a template file.
103
     *
104
     * @param  array  $data
105
     * @return \Pterodactyl\Models\Pack
106
     *
107
     * @throws \Pterodactyl\Exceptions\DisplayException
108
     */
109
    public function createWithTemplate(array $data)
110
    {
111
        if (! isset($data['file_upload'])) {
112
            throw new DisplayException('No template file was found submitted with this request.');
113
        }
114
115
        if (! $data['file_upload']->isValid()) {
116
            throw new DisplayException('The file provided does not appear to be valid.');
117
        }
118
119
        if (! in_array($data['file_upload']->getMimeType(), [
120
            'application/zip',
121
            'text/plain',
122
            'application/json',
123
        ])) {
124
            throw new DisplayException('The file provided (' . $data['file_upload']->getMimeType() . ') does not meet the required filetypes of application/zip or application/json.');
125
        }
126
127
        if ($data['file_upload']->getMimeType() === 'application/zip') {
128
            $zip = new \ZipArchive;
129
            if (! $zip->open($data['file_upload']->path())) {
130
                throw new DisplayException('The uploaded archive was unable to be opened.');
131
            }
132
133
            $isTar = $zip->locateName('archive.tar.gz');
134
135
            if (! $zip->locateName('import.json') || ! $isTar) {
136
                throw new DisplayException('This contents of the provided archive were in an invalid format.');
137
            }
138
139
            $json = json_decode($zip->getFromName('import.json'));
140
            $pack = $this->create([
141
                'name' => $json->name,
142
                'version' => $json->version,
143
                'description' => $json->description,
144
                'option_id' => $data['option_id'],
145
                'selectable' => $json->selectable,
146
                'visible' => $json->visible,
147
                'locked' => $json->locked,
148
            ]);
149
150
            if (! $zip->extractTo(storage_path('app/packs/' . $pack->uuid), 'archive.tar.gz')) {
151
                $pack->delete();
152
                throw new DisplayException('Unable to extract the archive file to the correct location.');
153
            }
154
155
            $zip->close();
156
157
            return $pack;
158
        } else {
159
            $json = json_decode(file_get_contents($data['file_upload']->path()));
160
161
            return $this->create([
162
                'name' => $json->name,
163
                'version' => $json->version,
164
                'description' => $json->description,
165
                'option_id' => $data['option_id'],
166
                'selectable' => $json->selectable,
167
                'visible' => $json->visible,
168
                'locked' => $json->locked,
169
            ]);
170
        }
171
    }
172
173
    /**
174
     * Updates a pack on the system.
175
     *
176
     * @param  int    $id
177
     * @param  array  $data
178
     * @return \Pterodactyl\Models\Pack
179
     *
180
     * @throws \Pterodactyl\Exceptions\DisplayException
181
     * @throws \Pterodactyl\Exceptions\DisplayValidationException
182
     */
183
    public function update($id, array $data)
184
    {
185
        $validator = Validator::make($data, [
186
            'name' => 'sometimes|required|string',
187
            'option_id' => 'sometimes|required|exists:service_options,id',
188
            'version' => 'sometimes|required|string',
189
            'description' => 'sometimes|string',
190
            'selectable' => 'sometimes|required|boolean',
191
            'visible' => 'sometimes|required|boolean',
192
            'locked' => 'sometimes|required|boolean',
193
        ]);
194
195
        if ($validator->fails()) {
196
            throw new DisplayValidationException(json_encode($validator->errors()));
197
        }
198
199
        $pack = Pack::withCount('servers')->findOrFail($id);
200
201
        if ($pack->servers_count > 0 && (isset($data['option_id']) && (int) $data['option_id'] !== $pack->option_id)) {
202
            throw new DisplayException('You cannot modify the associated option if servers are attached to a pack.');
203
        }
204
205
        $pack->fill([
206
            'name' => isset($data['name']) ? $data['name'] : $pack->name,
207
            'option_id' => isset($data['option_id']) ? $data['option_id'] : $pack->option_id,
208
            'version' => isset($data['version']) ? $data['version'] : $pack->version,
209
            'description' => (empty($data['description'])) ? null : $data['description'],
210
            'selectable' => isset($data['selectable']),
211
            'visible' => isset($data['visible']),
212
            'locked' => isset($data['locked']),
213
        ])->save();
214
215
        return $pack;
216
    }
217
218
    /**
219
     * Deletes a pack and files from the system.
220
     *
221
     * @param  int  $id
222
     * @return void
223
     *
224
     * @throws \Pterodactyl\Exceptions\DisplayException
225
     */
226
    public function delete($id)
227
    {
228
        $pack = Pack::withCount('servers')->findOrFail($id);
229
230
        if ($pack->servers_count > 0) {
231
            throw new DisplayException('Cannot delete a pack from the system if servers are assocaited with it.');
232
        }
233
234
        DB::transaction(function () use ($pack) {
235
            $pack->delete();
236
            Storage::deleteDirectory('packs/' . $pack->uuid);
0 ignored issues
show
Bug introduced by
The method deleteDirectory() 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...
237
        });
238
    }
239
}
240