|
24 | 24 |
|
25 | 25 | namespace Pterodactyl\Http\Controllers\Admin; |
26 | 26 |
|
27 | | -use Log; |
28 | | -use Alert; |
29 | | -use Pterodactyl\Models; |
30 | | -use Illuminate\Http\Request; |
31 | | -use Pterodactyl\Exceptions\DisplayException; |
| 27 | +use Pterodactyl\Models\Service; |
| 28 | +use Prologue\Alerts\AlertsMessageBag; |
32 | 29 | use Pterodactyl\Http\Controllers\Controller; |
33 | | -use Pterodactyl\Repositories\ServiceRepository; |
34 | | -use Pterodactyl\Exceptions\DisplayValidationException; |
| 30 | +use Pterodactyl\Services\Services\ServiceUpdateService; |
| 31 | +use Pterodactyl\Services\Services\ServiceCreationService; |
| 32 | +use Pterodactyl\Services\Services\ServiceDeletionService; |
| 33 | +use Pterodactyl\Exceptions\Services\HasActiveServersException; |
| 34 | +use Pterodactyl\Http\Requests\Admin\Service\ServiceFormRequest; |
| 35 | +use Pterodactyl\Contracts\Repository\ServiceRepositoryInterface; |
| 36 | +use Pterodactyl\Http\Requests\Admin\Service\ServiceFunctionsFormRequest; |
35 | 37 |
|
36 | 38 | class ServiceController extends Controller |
37 | 39 | { |
| 40 | + /** |
| 41 | + * @var \Prologue\Alerts\AlertsMessageBag |
| 42 | + */ |
| 43 | + protected $alert; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var \Pterodactyl\Services\Services\ServiceCreationService |
| 47 | + */ |
| 48 | + protected $creationService; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var \Pterodactyl\Services\Services\ServiceDeletionService |
| 52 | + */ |
| 53 | + protected $deletionService; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var \Pterodactyl\Contracts\Repository\ServiceRepositoryInterface |
| 57 | + */ |
| 58 | + protected $repository; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var \Pterodactyl\Services\Services\ServiceUpdateService |
| 62 | + */ |
| 63 | + protected $updateService; |
| 64 | + |
| 65 | + public function __construct( |
| 66 | + AlertsMessageBag $alert, |
| 67 | + ServiceCreationService $creationService, |
| 68 | + ServiceDeletionService $deletionService, |
| 69 | + ServiceRepositoryInterface $repository, |
| 70 | + ServiceUpdateService $updateService |
| 71 | + ) { |
| 72 | + $this->alert = $alert; |
| 73 | + $this->creationService = $creationService; |
| 74 | + $this->deletionService = $deletionService; |
| 75 | + $this->repository = $repository; |
| 76 | + $this->updateService = $updateService; |
| 77 | + } |
| 78 | + |
38 | 79 | /** |
39 | 80 | * Display service overview page. |
40 | 81 | * |
41 | | - * @param \Illuminate\Http\Request $request |
42 | 82 | * @return \Illuminate\View\View |
43 | 83 | */ |
44 | | - public function index(Request $request) |
| 84 | + public function index() |
45 | 85 | { |
46 | 86 | return view('admin.services.index', [ |
47 | | - 'services' => Models\Service::withCount('servers', 'options', 'packs')->get(), |
| 87 | + 'services' => $this->repository->getWithOptions(), |
48 | 88 | ]); |
49 | 89 | } |
50 | 90 |
|
51 | 91 | /** |
52 | 92 | * Display create service page. |
53 | 93 | * |
54 | | - * @param \Illuminate\Http\Request $request |
55 | 94 | * @return \Illuminate\View\View |
56 | 95 | */ |
57 | | - public function create(Request $request) |
| 96 | + public function create() |
58 | 97 | { |
59 | 98 | return view('admin.services.new'); |
60 | 99 | } |
61 | 100 |
|
62 | 101 | /** |
63 | 102 | * Return base view for a service. |
64 | 103 | * |
65 | | - * @param \Illuminate\Http\Request $request |
66 | | - * @param int $id |
| 104 | + * @param int $service |
67 | 105 | * @return \Illuminate\View\View |
68 | 106 | */ |
69 | | - public function view(Request $request, $id) |
| 107 | + public function view($service) |
70 | 108 | { |
71 | 109 | return view('admin.services.view', [ |
72 | | - 'service' => Models\Service::with('options', 'options.servers')->findOrFail($id), |
| 110 | + 'service' => $this->repository->getWithOptionServers($service), |
73 | 111 | ]); |
74 | 112 | } |
75 | 113 |
|
76 | 114 | /** |
77 | 115 | * Return function editing view for a service. |
78 | 116 | * |
79 | | - * @param \Illuminate\Http\Request $request |
80 | | - * @param int $id |
| 117 | + * @param \Pterodactyl\Models\Service $service |
81 | 118 | * @return \Illuminate\View\View |
82 | 119 | */ |
83 | | - public function viewFunctions(Request $request, $id) |
| 120 | + public function viewFunctions(Service $service) |
84 | 121 | { |
85 | | - return view('admin.services.functions', ['service' => Models\Service::findOrFail($id)]); |
| 122 | + return view('admin.services.functions', ['service' => $service]); |
86 | 123 | } |
87 | 124 |
|
88 | 125 | /** |
89 | 126 | * Handle post action for new service. |
90 | 127 | * |
91 | | - * @param \Illuminate\Http\Request $request |
| 128 | + * @param \Pterodactyl\Http\Requests\Admin\Service\ServiceFormRequest $request |
92 | 129 | * @return \Illuminate\Http\RedirectResponse |
| 130 | + * |
| 131 | + * @throws \Pterodactyl\Exceptions\Model\DataValidationException |
93 | 132 | */ |
94 | | - public function store(Request $request) |
| 133 | + public function store(ServiceFormRequest $request) |
95 | 134 | { |
96 | | - $repo = new ServiceRepository; |
| 135 | + $service = $this->creationService->handle($request->normalize()); |
| 136 | + $this->alert->success(trans('admin/services.notices.service_created', ['name' => $service->name]))->flash(); |
97 | 137 |
|
98 | | - try { |
99 | | - $service = $repo->create($request->intersect([ |
100 | | - 'name', 'description', 'folder', 'startup', |
101 | | - ])); |
102 | | - Alert::success('Successfully created new service!')->flash(); |
103 | | - |
104 | | - return redirect()->route('admin.services.view', $service->id); |
105 | | - } catch (DisplayValidationException $ex) { |
106 | | - return redirect()->route('admin.services.new')->withErrors(json_decode($ex->getMessage()))->withInput(); |
107 | | - } catch (DisplayException $ex) { |
108 | | - Alert::danger($ex->getMessage())->flash(); |
109 | | - } catch (\Exception $ex) { |
110 | | - Log::error($ex); |
111 | | - Alert::danger('An error occured while attempting to add a new service. This error has been logged.')->flash(); |
112 | | - } |
113 | | - |
114 | | - return redirect()->route('admin.services.new')->withInput(); |
| 138 | + return redirect()->route('admin.services.view', $service->id); |
115 | 139 | } |
116 | 140 |
|
117 | 141 | /** |
118 | 142 | * Edits configuration for a specific service. |
119 | 143 | * |
120 | | - * @param \Illuminate\Http\Request $request |
121 | | - * @param int $id |
| 144 | + * @param \Pterodactyl\Http\Requests\Admin\Service\ServiceFormRequest $request |
| 145 | + * @param \Pterodactyl\Models\Service $service |
122 | 146 | * @return \Illuminate\Http\RedirectResponse |
| 147 | + * |
| 148 | + * @throws \Pterodactyl\Exceptions\Model\DataValidationException |
| 149 | + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException |
123 | 150 | */ |
124 | | - public function edit(Request $request, $id) |
| 151 | + public function update(ServiceFormRequest $request, Service $service) |
125 | 152 | { |
126 | | - $repo = new ServiceRepository; |
127 | | - $redirectTo = ($request->input('redirect_to')) ? 'admin.services.view.functions' : 'admin.services.view'; |
| 153 | + $this->updateService->handle($service->id, $request->normalize()); |
| 154 | + $this->alert->success(trans('admin/services.notices.service_updated'))->flash(); |
128 | 155 |
|
| 156 | + return redirect()->route('admin.services.view', $service); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Update the functions file for a service. |
| 161 | + * |
| 162 | + * @param \Pterodactyl\Http\Requests\Admin\Service\ServiceFunctionsFormRequest $request |
| 163 | + * @param \Pterodactyl\Models\Service $service |
| 164 | + * @return \Illuminate\Http\RedirectResponse |
| 165 | + * |
| 166 | + * @throws \Pterodactyl\Exceptions\Model\DataValidationException |
| 167 | + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException |
| 168 | + */ |
| 169 | + public function updateFunctions(ServiceFunctionsFormRequest $request, Service $service) |
| 170 | + { |
| 171 | + $this->updateService->handle($service->id, $request->normalize()); |
| 172 | + $this->alert->success(trans('admin/services.notices.functions_updated'))->flash(); |
| 173 | + |
| 174 | + return redirect()->route('admin.services.view.functions', $service->id); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Delete a service from the panel. |
| 179 | + * |
| 180 | + * @param \Pterodactyl\Models\Service $service |
| 181 | + * @return \Illuminate\Http\RedirectResponse |
| 182 | + */ |
| 183 | + public function destroy(Service $service) |
| 184 | + { |
129 | 185 | try { |
130 | | - if ($request->input('action') !== 'delete') { |
131 | | - $repo->update($id, $request->intersect([ |
132 | | - 'name', 'description', 'folder', 'startup', 'index_file', |
133 | | - ])); |
134 | | - Alert::success('Service has been updated successfully.')->flash(); |
135 | | - } else { |
136 | | - $repo->delete($id); |
137 | | - Alert::success('Successfully deleted service from the system.')->flash(); |
138 | | - |
139 | | - return redirect()->route('admin.services'); |
140 | | - } |
141 | | - } catch (DisplayValidationException $ex) { |
142 | | - return redirect()->route($redirectTo, $id)->withErrors(json_decode($ex->getMessage()))->withInput(); |
143 | | - } catch (DisplayException $ex) { |
144 | | - Alert::danger($ex->getMessage())->flash(); |
145 | | - } catch (\Exception $ex) { |
146 | | - Log::error($ex); |
147 | | - Alert::danger('An error occurred while attempting to update this service. This error has been logged.')->flash(); |
| 186 | + $this->deletionService->handle($service->id); |
| 187 | + $this->alert->success(trans('admin/services.notices.service_deleted'))->flash(); |
| 188 | + } catch (HasActiveServersException $exception) { |
| 189 | + $this->alert->danger($exception->getMessage())->flash(); |
| 190 | + |
| 191 | + return redirect()->back(); |
148 | 192 | } |
149 | 193 |
|
150 | | - return redirect()->route($redirectTo, $id); |
| 194 | + return redirect()->route('admin.services'); |
151 | 195 | } |
152 | 196 | } |
0 commit comments