22
33namespace Pterodactyl \Services \Servers ;
44
5+ use Illuminate \Support \Arr ;
6+ use Pterodactyl \Models \Egg ;
57use Pterodactyl \Models \User ;
68use Pterodactyl \Models \Server ;
9+ use Pterodactyl \Models \ServerVariable ;
710use Illuminate \Database \ConnectionInterface ;
811use Pterodactyl \Traits \Services \HasUserLevels ;
9- use Pterodactyl \Contracts \Repository \EggRepositoryInterface ;
10- use Pterodactyl \Contracts \Repository \ServerRepositoryInterface ;
11- use Pterodactyl \Contracts \Repository \ServerVariableRepositoryInterface ;
1212
1313class StartupModificationService
1414{
@@ -19,63 +19,21 @@ class StartupModificationService
1919 */
2020 private $ connection ;
2121
22- /**
23- * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
24- */
25- private $ eggRepository ;
26-
27- /**
28- * @var \Pterodactyl\Services\Servers\EnvironmentService
29- */
30- private $ environmentService ;
31-
32- /**
33- * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
34- */
35- private $ repository ;
36-
37- /**
38- * @var \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface
39- */
40- private $ serverVariableRepository ;
41-
4222 /**
4323 * @var \Pterodactyl\Services\Servers\VariableValidatorService
4424 */
4525 private $ validatorService ;
4626
47- /**
48- * @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService
49- */
50- private $ structureService ;
51-
5227 /**
5328 * StartupModificationService constructor.
5429 *
5530 * @param \Illuminate\Database\ConnectionInterface $connection
56- * @param \Pterodactyl\Contracts\Repository\EggRepositoryInterface $eggRepository
57- * @param \Pterodactyl\Services\Servers\EnvironmentService $environmentService
58- * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
59- * @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $structureService
60- * @param \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface $serverVariableRepository
6131 * @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService
6232 */
63- public function __construct (
64- ConnectionInterface $ connection ,
65- EggRepositoryInterface $ eggRepository ,
66- EnvironmentService $ environmentService ,
67- ServerRepositoryInterface $ repository ,
68- ServerConfigurationStructureService $ structureService ,
69- ServerVariableRepositoryInterface $ serverVariableRepository ,
70- VariableValidatorService $ validatorService
71- ) {
33+ public function __construct (ConnectionInterface $ connection , VariableValidatorService $ validatorService )
34+ {
7235 $ this ->connection = $ connection ;
73- $ this ->eggRepository = $ eggRepository ;
74- $ this ->environmentService = $ environmentService ;
75- $ this ->repository = $ repository ;
76- $ this ->serverVariableRepository = $ serverVariableRepository ;
7736 $ this ->validatorService = $ validatorService ;
78- $ this ->structureService = $ structureService ;
7937 }
8038
8139 /**
@@ -85,63 +43,70 @@ public function __construct(
8543 * @param array $data
8644 * @return \Pterodactyl\Models\Server
8745 *
88- * @throws \Illuminate\Validation\ValidationException
89- * @throws \Pterodactyl\Exceptions\Model\DataValidationException
90- * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
46+ * @throws \Throwable
9147 */
9248 public function handle (Server $ server , array $ data ): Server
9349 {
94- $ this ->connection ->beginTransaction ();
95- if (! is_null (array_get ($ data , 'environment ' ))) {
96- $ this ->validatorService ->setUserLevel ($ this ->getUserLevel ());
97- $ results = $ this ->validatorService ->handle (array_get ($ data , 'egg_id ' , $ server ->egg_id ), array_get ($ data , 'environment ' , []));
98-
99- $ results ->each (function ($ result ) use ($ server ) {
100- $ this ->serverVariableRepository ->withoutFreshModel ()->updateOrCreate ([
101- 'server_id ' => $ server ->id ,
102- 'variable_id ' => $ result ->id ,
103- ], [
104- 'variable_value ' => $ result ->value ?? '' ,
105- ]);
106- });
107- }
108-
109- if ($ this ->isUserLevel (User::USER_LEVEL_ADMIN )) {
110- $ this ->updateAdministrativeSettings ($ data , $ server );
111- }
112-
113- $ this ->connection ->commit ();
114-
115- return $ server ;
50+ return $ this ->connection ->transaction (function () use ($ server , $ data ) {
51+ if (! empty ($ data ['environment ' ])) {
52+ $ egg = $ this ->isUserLevel (User::USER_LEVEL_ADMIN ) ? ($ data ['egg_id ' ] ?? $ server ->egg_id ) : $ server ->egg_id ;
53+
54+ $ results = $ this ->validatorService
55+ ->setUserLevel ($ this ->getUserLevel ())
56+ ->handle ($ egg , $ data ['environment ' ]);
57+
58+ foreach ($ results as $ result ) {
59+ ServerVariable::query ()->updateOrCreate (
60+ [
61+ 'server_id ' => $ server ->id ,
62+ 'variable_id ' => $ result ->id ,
63+ ],
64+ ['variable_value ' => $ result ->value ?? '' ]
65+ );
66+ }
67+ }
68+
69+ if ($ this ->isUserLevel (User::USER_LEVEL_ADMIN )) {
70+ $ this ->updateAdministrativeSettings ($ data , $ server );
71+ }
72+
73+ // Calling ->refresh() rather than ->fresh() here causes it to return the
74+ // variables as triplicates for some reason? Not entirely sure, should dig
75+ // in more to figure it out, but luckily we have a test case covering this
76+ // specific call so we can be assured we're not breaking it _here_ at least.
77+ //
78+ // TODO(dane): this seems like a red-flag for the code powering the relationship
79+ // that should be looked into more.
80+ return $ server ->fresh ();
81+ });
11682 }
11783
11884 /**
11985 * Update certain administrative settings for a server in the DB.
12086 *
12187 * @param array $data
12288 * @param \Pterodactyl\Models\Server $server
123- *
124- * @throws \Pterodactyl\Exceptions\Model\DataValidationException
125- * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
12689 */
127- private function updateAdministrativeSettings (array $ data , Server &$ server )
90+ protected function updateAdministrativeSettings (array $ data , Server &$ server )
12891 {
12992 if (
130- is_digit (array_get ($ data , 'egg_id ' ))
93+ is_digit (Arr:: get ($ data , 'egg_id ' ))
13194 && $ data ['egg_id ' ] != $ server ->egg_id
132- && is_null (array_get ($ data , 'nest_id ' ))
95+ && is_null (Arr:: get ($ data , 'nest_id ' ))
13396 ) {
134- $ egg = $ this ->eggRepository ->setColumns (['id ' , 'nest_id ' ])->find ($ data ['egg_id ' ]);
97+ /** @var \Pterodactyl\Models\Egg $egg */
98+ $ egg = Egg::query ()->select (['nest_id ' ])->findOrFail ($ data ['egg_id ' ]);
99+
135100 $ data ['nest_id ' ] = $ egg ->nest_id ;
136101 }
137102
138- $ server = $ this -> repository -> update ( $ server -> id , [
103+ $ server-> forceFill ( [
139104 'installed ' => 0 ,
140- 'startup ' => array_get ( $ data, 'startup ' , $ server ->startup ) ,
141- 'nest_id ' => array_get ( $ data, 'nest_id ' , $ server ->nest_id ) ,
142- 'egg_id ' => array_get ( $ data, 'egg_id ' , $ server ->egg_id ) ,
143- 'skip_scripts ' => array_get ( $ data, 'skip_scripts ' ) ?? isset ($ data ['skip_scripts ' ]),
144- 'image ' => array_get ( $ data, 'docker_image ' , $ server ->image ) ,
145- ]);
105+ 'startup ' => $ data[ 'startup ' ] ?? $ server ->startup ,
106+ 'nest_id ' => $ data[ 'nest_id ' ] ?? $ server ->nest_id ,
107+ 'egg_id ' => $ data[ 'egg_id ' ] ?? $ server ->egg_id ,
108+ 'skip_scripts ' => $ data[ 'skip_scripts ' ] ?? isset ($ data ['skip_scripts ' ]),
109+ 'image ' => $ data[ 'docker_image ' ] ?? $ server ->image ,
110+ ])-> save () ;
146111 }
147112}
0 commit comments