2626
2727use Log ;
2828use Alert ;
29+ use Prologue \Alerts \AlertsMessageBag ;
30+ use Pterodactyl \Contracts \Repository \ServiceRepositoryInterface ;
31+ use Pterodactyl \Http \Requests \Admin \OptionVariableFormRequest ;
32+ use Pterodactyl \Http \Requests \Admin \ServiceOptionFormRequest ;
33+ use Pterodactyl \Services \Services \Options \CreationService ;
34+ use Pterodactyl \Services \Services \Variables \VariableCreationService ;
2935use Route ;
3036use Javascript ;
3137use Illuminate \Http \Request ;
4248class OptionController extends Controller
4349{
4450 /**
45- * Store the repository instance.
46- *
47- * @var \Pterodactyl\Repositories\OptionRepository
51+ * @var \Prologue\Alerts\AlertsMessageBag
4852 */
49- protected $ repository ;
53+ protected $ alert ;
54+
55+ /**
56+ * @var \Pterodactyl\Services\Services\Options\CreationService
57+ */
58+ protected $ creationService ;
59+
60+ /**
61+ * @var \Pterodactyl\Contracts\Repository\ServiceRepositoryInterface
62+ */
63+ protected $ serviceRepository ;
64+
65+ /**
66+ * @var \Pterodactyl\Services\Services\Variables\VariableCreationService
67+ */
68+ protected $ variableCreationService ;
5069
5170 /**
5271 * OptionController constructor.
72+ *
73+ * @param \Prologue\Alerts\AlertsMessageBag $alert
74+ * @param \Pterodactyl\Contracts\Repository\ServiceRepositoryInterface $serviceRepository
75+ * @param \Pterodactyl\Services\Services\Options\CreationService $creationService
76+ * @param \Pterodactyl\Services\Services\Variables\VariableCreationService $variableCreationService
5377 */
54- public function __construct ()
55- {
56- $ this ->repository = new OptionRepository (Route::current ()->parameter ('option ' ));
78+ public function __construct (
79+ AlertsMessageBag $ alert ,
80+ ServiceRepositoryInterface $ serviceRepository ,
81+ CreationService $ creationService ,
82+ VariableCreationService $ variableCreationService
83+ ) {
84+ $ this ->alert = $ alert ;
85+ $ this ->creationService = $ creationService ;
86+ $ this ->serviceRepository = $ serviceRepository ;
87+ $ this ->variableCreationService = $ variableCreationService ;
5788 }
5889
5990 /**
6091 * Handles request to view page for adding new option.
6192 *
62- * @param \Illuminate\Http\Request $request
6393 * @return \Illuminate\View\View
6494 */
65- public function create (Request $ request )
95+ public function create ()
6696 {
67- $ services = Service:: with ( ' options ' )-> get ();
97+ $ services = $ this -> serviceRepository -> getWithOptions ();
6898 Javascript::put (['services ' => $ services ->keyBy ('id ' )]);
6999
70100 return view ('admin.services.options.new ' , ['services ' => $ services ]);
71101 }
72102
73103 /**
74- * Handles POST request to create a new option.
104+ * Handle adding a new service option.
105+ *
106+ * @param \Pterodactyl\Http\Requests\Admin\ServiceOptionFormRequest $request
107+ * @return \Illuminate\Http\RedirectResponse
75108 *
76- * @param \Illuminate\Http\Request $request
77- * @return \Illuminate\Response\RedirectResponse
109+ * @throws \Pterodactyl\Exceptions\DisplayException
110+ * @throws \Pterodactyl\Exceptions\Model\DataValidationException
78111 */
79- public function store (Request $ request )
112+ public function store (ServiceOptionFormRequest $ request )
80113 {
81- $ repo = new OptionRepository ;
82-
83- try {
84- $ option = $ repo ->create ($ request ->intersect ([
85- 'service_id ' , 'name ' , 'description ' , 'tag ' ,
86- 'docker_image ' , 'startup ' , 'config_from ' , 'config_startup ' ,
87- 'config_logs ' , 'config_files ' , 'config_stop ' ,
88- ]));
89- Alert::success ('Successfully created new service option. ' )->flash ();
114+ $ option = $ this ->creationService ->handle ($ request ->normalize ());
115+ $ this ->alert ->success (trans ('admin/services.options.notices.option_created ' ))->flash ();
90116
91- return redirect ()->route ('admin.services.option.view ' , $ option ->id );
92- } catch (DisplayValidationException $ ex ) {
93- return redirect ()->route ('admin.services.option.new ' )->withErrors (json_decode ($ ex ->getMessage ()))->withInput ();
94- } catch (DisplayException $ ex ) {
95- Alert::danger ($ ex ->getMessage ())->flash ();
96- } catch (\Exception $ ex ) {
97- Log::error ($ ex );
98- Alert::danger ('An unhandled exception occurred while attempting to create this service. This error has been logged. ' )->flash ();
99- }
100-
101- return redirect ()->route ('admin.services.option.new ' )->withInput ();
117+ return redirect ()->route ('admin.services.option.view ' , $ option ->id );
102118 }
103119
104120 /**
105121 * Handles POST request to create a new option variable.
106122 *
107- * @param \Illuminate \Http\Request $request
108- * @param int $id
123+ * @param \Pterodactyl \Http\Requests\Admin\OptionVariableFormRequest $request
124+ * @param \Pterodactyl\Models\ServiceOption $option
109125 * @return \Illuminate\Http\RedirectResponse
110126 */
111- public function createVariable (Request $ request , $ id )
127+ public function createVariable (OptionVariableFormRequest $ request , ServiceOption $ option )
112128 {
113- $ repo = new VariableRepository ;
114-
115- try {
116- $ variable = $ repo ->create ($ id , $ request ->intersect ([
117- 'name ' , 'description ' , 'env_variable ' ,
118- 'default_value ' , 'options ' , 'rules ' ,
119- ]));
120-
121- Alert::success ('New variable successfully assigned to this service option. ' )->flash ();
122- } catch (DisplayValidationException $ ex ) {
123- return redirect ()->route ('admin.services.option.variables ' , $ id )->withErrors (json_decode ($ ex ->getMessage ()));
124- } catch (DisplayException $ ex ) {
125- Alert::danger ($ ex ->getMessage ())->flash ();
126- } catch (\Exception $ ex ) {
127- Log::error ($ ex );
128- Alert::danger ('An unhandled exception was encountered while attempting to process that request. This error has been logged. ' )->flash ();
129- }
129+ $ this ->variableCreationService ->handle ($ option ->id , $ request ->normalize ());
130+ $ this ->alert ->success (trans ('admin/services.variables.notices.variable_created ' ))->flash ();
130131
131- return redirect ()->route ('admin.services.option.variables ' , $ id );
132+ return redirect ()->route ('admin.services.option.variables ' , $ option -> id );
132133 }
133134
134135 /**
135136 * Display option overview page.
136137 *
137- * @param \Illuminate\Http\Request $request
138- * @param int $id
138+ * @param \Illuminate\Http\Request $request
139+ * @param int $id
139140 * @return \Illuminate\View\View
140141 */
141142 public function viewConfiguration (Request $ request , $ id )
@@ -146,13 +147,14 @@ public function viewConfiguration(Request $request, $id)
146147 /**
147148 * Display variable overview page for a service option.
148149 *
149- * @param \Illuminate\Http\Request $request
150- * @param int $id
150+ * @param \Illuminate\Http\Request $request
151+ * @param int $id
151152 * @return \Illuminate\View\View
152153 */
153154 public function viewVariables (Request $ request , $ id )
154155 {
155- return view ('admin.services.options.variables ' , ['option ' => ServiceOption::with ('variables ' )->findOrFail ($ id )]);
156+ return view ('admin.services.options.variables ' , ['option ' => ServiceOption::with ('variables ' )
157+ ->findOrFail ($ id ), ]);
156158 }
157159
158160 /**
@@ -179,8 +181,8 @@ public function viewScripts(Request $request, $id)
179181 /**
180182 * Handles POST when editing a configration for a service option.
181183 *
182- * @param \Illuminate\Http\Request $request
183- * @param int $id
184+ * @param \Illuminate\Http\Request $request
185+ * @param int $id
184186 * @return \Illuminate\Http\RedirectResponse
185187 */
186188 public function editConfiguration (Request $ request , $ id )
@@ -207,7 +209,8 @@ public function editConfiguration(Request $request, $id)
207209 Alert::danger ($ ex ->getMessage ())->flash ();
208210 } catch (\Exception $ ex ) {
209211 Log::error ($ ex );
210- Alert::danger ('An unhandled exception occurred while attempting to perform that action. This error has been logged. ' )->flash ();
212+ Alert::danger ('An unhandled exception occurred while attempting to perform that action. This error has been logged. ' )
213+ ->flash ();
211214 }
212215
213216 return redirect ()->route ('admin.services.option.view ' , $ id );
@@ -216,9 +219,9 @@ public function editConfiguration(Request $request, $id)
216219 /**
217220 * Handles POST when editing a configration for a service option.
218221 *
219- * @param \Pterodactyl\Http\Requests\Admin\Service\StoreOptionVariable $request
220- * @param int $option
221- * @param int $variable
222+ * @param \Pterodactyl\Http\Requests\Admin\Service\StoreOptionVariable $request
223+ * @param int $option
224+ * @param int $variable
222225 * @return \Illuminate\Http\RedirectResponse
223226 */
224227 public function editVariable (StoreOptionVariable $ request , $ option , $ variable )
@@ -237,7 +240,8 @@ public function editVariable(StoreOptionVariable $request, $option, $variable)
237240 Alert::danger ($ ex ->getMessage ())->flash ();
238241 } catch (\Exception $ ex ) {
239242 Log::error ($ ex );
240- Alert::danger ('An unhandled exception was encountered while attempting to process that request. This error has been logged. ' )->flash ();
243+ Alert::danger ('An unhandled exception was encountered while attempting to process that request. This error has been logged. ' )
244+ ->flash ();
241245 }
242246
243247 return redirect ()->route ('admin.services.option.variables ' , $ option );
@@ -259,7 +263,8 @@ public function updateScripts(EditOptionScript $request)
259263 Alert::danger ($ ex ->getMessage ())->flash ();
260264 } catch (\Exception $ ex ) {
261265 Log::error ($ ex );
262- Alert::danger ('An unhandled exception was encountered while attempting to process that request. This error has been logged. ' )->flash ();
266+ Alert::danger ('An unhandled exception was encountered while attempting to process that request. This error has been logged. ' )
267+ ->flash ();
263268 }
264269
265270 return redirect ()->route ('admin.services.option.scripts ' , $ id );
0 commit comments