22
33namespace Pterodactyl \Console \Commands \Server ;
44
5+ use Pterodactyl \Models \Server ;
56use Illuminate \Console \Command ;
6- use GuzzleHttp \Exception \RequestException ;
77use Illuminate \Validation \ValidationException ;
88use Illuminate \Validation \Factory as ValidatorFactory ;
99use Pterodactyl \Repositories \Wings \DaemonPowerRepository ;
10- use Pterodactyl \Contracts \ Repository \ ServerRepositoryInterface ;
10+ use Pterodactyl \Exceptions \ Http \ Connection \ DaemonConnectionException ;
1111
1212class BulkPowerActionCommand extends Command
1313{
14- /**
15- * @var \Pterodactyl\Repositories\Wings\DaemonPowerRepository
16- */
17- private $ powerRepository ;
18-
19- /**
20- * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
21- */
22- private $ repository ;
23-
24- /**
25- * @var \Illuminate\Validation\Factory
26- */
27- private $ validator ;
28-
2914 /**
3015 * @var string
3116 */
@@ -40,36 +25,19 @@ class BulkPowerActionCommand extends Command
4025 protected $ description = 'Perform bulk power management on large groupings of servers or nodes at once. ' ;
4126
4227 /**
43- * BulkPowerActionCommand constructor .
28+ * Handle the bulk power request .
4429 *
4530 * @param \Pterodactyl\Repositories\Wings\DaemonPowerRepository $powerRepository
46- * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
4731 * @param \Illuminate\Validation\Factory $validator
48- */
49- public function __construct (
50- DaemonPowerRepository $ powerRepository ,
51- ServerRepositoryInterface $ repository ,
52- ValidatorFactory $ validator
53- ) {
54- parent ::__construct ();
55-
56- $ this ->repository = $ repository ;
57- $ this ->validator = $ validator ;
58- $ this ->powerRepository = $ powerRepository ;
59- }
60-
61- /**
62- * Handle the bulk power request.
63- *
6432 * @throws \Illuminate\Validation\ValidationException
6533 */
66- public function handle ()
34+ public function handle (DaemonPowerRepository $ powerRepository , ValidatorFactory $ validator )
6735 {
6836 $ action = $ this ->argument ('action ' );
6937 $ nodes = empty ($ this ->option ('nodes ' )) ? [] : explode (', ' , $ this ->option ('nodes ' ));
7038 $ servers = empty ($ this ->option ('servers ' )) ? [] : explode (', ' , $ this ->option ('servers ' ));
7139
72- $ validator = $ this -> validator ->make ([
40+ $ validator = $ validator ->make ([
7341 'action ' => $ action ,
7442 'nodes ' => $ nodes ,
7543 'servers ' => $ servers ,
@@ -89,23 +57,18 @@ public function handle()
8957 throw new ValidationException ($ validator );
9058 }
9159
92- $ count = $ this ->repository -> getServersForPowerActionCount ($ servers , $ nodes );
60+ $ count = $ this ->getQueryBuilder ($ servers , $ nodes)-> count ( );
9361 if (! $ this ->confirm (trans ('command/messages.server.power.confirm ' , ['action ' => $ action , 'count ' => $ count ])) && $ this ->input ->isInteractive ()) {
9462 return ;
9563 }
9664
9765 $ bar = $ this ->output ->createProgressBar ($ count );
98- $ servers = $ this ->repository ->getServersForPowerAction ($ servers , $ nodes );
99-
100- $ servers ->each (function ($ server ) use ($ action , &$ bar ) {
66+ $ this ->getQueryBuilder ($ servers , $ nodes )->each (function (Server $ server ) use ($ action , $ powerRepository , &$ bar ) {
10167 $ bar ->clear ();
10268
10369 try {
104- $ this ->powerRepository
105- ->setNode ($ server ->node )
106- ->setServer ($ server )
107- ->send ($ action );
108- } catch (RequestException $ exception ) {
70+ $ powerRepository ->setServer ($ server )->send ($ action );
71+ } catch (DaemonConnectionException $ exception ) {
10972 $ this ->output ->error (trans ('command/messages.server.power.action_failed ' , [
11073 'name ' => $ server ->name ,
11174 'id ' => $ server ->id ,
@@ -120,4 +83,28 @@ public function handle()
12083
12184 $ this ->line ('' );
12285 }
86+
87+ /**
88+ * Returns the query builder instance that will return the servers that should be affected.
89+ *
90+ * @param array $servers
91+ * @param array $nodes
92+ * @return \Illuminate\Database\Eloquent\Builder
93+ */
94+ protected function getQueryBuilder (array $ servers , array $ nodes )
95+ {
96+ $ instance = Server::query ()
97+ ->where ('suspended ' , false )
98+ ->where ('installed ' , Server::STATUS_INSTALLED );
99+
100+ if (! empty ($ nodes ) && ! empty ($ servers )) {
101+ $ instance ->whereIn ('id ' , $ servers )->orWhereIn ('node_id ' , $ nodes );
102+ } else if (empty ($ nodes ) && ! empty ($ servers )) {
103+ $ instance ->whereIn ('id ' , $ servers );
104+ } else if (! empty ($ nodes ) && empty ($ servers )) {
105+ $ instance ->whereIn ('node_id ' , $ nodes );
106+ }
107+
108+ return $ instance ->with ('node ' );
109+ }
123110}
0 commit comments