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
|
|
|
public function create(array $data) |
42
|
|
|
{ |
43
|
|
|
$validator = Validator::make($data, [ |
44
|
|
|
'name' => 'required|string|min:1|max:255', |
45
|
|
|
'description' => 'required|nullable|string', |
46
|
|
|
'folder' => 'required|unique:services,folder|regex:/^[\w.-]{1,50}$/', |
47
|
|
|
'startup' => 'required|nullable|string', |
48
|
|
|
]); |
49
|
|
|
|
50
|
|
|
if ($validator->fails()) { |
51
|
|
|
throw new DisplayValidationException($validator->errors()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return DB::transaction(function () use ($data) { |
55
|
|
|
$service = new Service; |
56
|
|
|
$service->author = config('pterodactyl.service.author'); |
|
|
|
|
57
|
|
|
$service->fill([ |
58
|
|
|
'name' => $data['name'], |
59
|
|
|
'description' => (isset($data['description'])) ? $data['description'] : null, |
60
|
|
|
'folder' => $data['folder'], |
61
|
|
|
'startup' => (isset($data['startup'])) ? $data['startup'] : null, |
62
|
|
|
'index_file' => Service::defaultIndexFile(), |
63
|
|
|
])->save(); |
64
|
|
|
|
65
|
|
|
// It is possible for an event to return false or throw an exception |
66
|
|
|
// which won't necessarily be detected by this transaction. |
67
|
|
|
// |
68
|
|
|
// This check ensures the model was actually saved. |
69
|
|
|
if (! $service->exists) { |
70
|
|
|
throw new \Exception('Service model was created however the response appears to be invalid. Did an event fire wrongly?'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $service; |
74
|
|
|
}); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Updates a service. |
79
|
|
|
* |
80
|
|
|
* @param int $id |
81
|
|
|
* @param array $data |
82
|
|
|
* @return \Pterodactyl\Models\Service |
83
|
|
|
*/ |
84
|
|
|
public function update($id, array $data) |
85
|
|
|
{ |
86
|
|
|
$service = Service::findOrFail($id); |
87
|
|
|
|
88
|
|
|
$validator = Validator::make($data, [ |
89
|
|
|
'name' => 'sometimes|required|string|min:1|max:255', |
90
|
|
|
'description' => 'sometimes|required|nullable|string', |
91
|
|
|
'folder' => 'sometimes|required|regex:/^[\w.-]{1,50}$/', |
92
|
|
|
'startup' => 'sometimes|required|nullable|string', |
93
|
|
|
'index_file' => 'sometimes|required|string', |
94
|
|
|
]); |
95
|
|
|
|
96
|
|
|
if ($validator->fails()) { |
97
|
|
|
throw new DisplayValidationException($validator->errors()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return DB::transaction(function () use ($data, $service) { |
101
|
|
|
$service->fill($data)->save(); |
102
|
|
|
|
103
|
|
|
return $service; |
104
|
|
|
}); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Deletes a service and associated files and options. |
109
|
|
|
* |
110
|
|
|
* @param int $id |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
|
View Code Duplication |
public function delete($id) |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$service = Service::withCount('servers')->with('options')->findOrFail($id); |
116
|
|
|
|
117
|
|
|
if ($service->servers_count > 0) { |
118
|
|
|
throw new DisplayException('You cannot delete a service that has servers associated with it.'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
DB::transaction(function () use ($service) { |
122
|
|
|
foreach ($service->options as $option) { |
123
|
|
|
(new OptionRepository)->delete($option->id); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$service->delete(); |
127
|
|
|
}); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.