Passed
Push — master ( 03796d...557337 )
by Nikita
23:00 queued 11:26
created

ServersController::install()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 20
rs 9.8666
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Gameap\Http\Controllers\API;
4
5
use Exception;
6
use Gameap\Exceptions\Repositories\GdaemonTaskRepository\EmptyServerStartCommandException;
7
use Gameap\Exceptions\Repositories\GdaemonTaskRepository\GdaemonTaskRepositoryException;
8
use Gameap\Exceptions\Repositories\RecordExistExceptions;
9
use Gameap\Http\Controllers\AuthController;
10
use Gameap\Http\Requests\API\ServerConsoleCommandRequest;
11
use Gameap\Models\GdaemonTask;
12
use Gameap\Models\Server;
13
use Gameap\Repositories\GdaemonTaskRepository;
14
use Gameap\Repositories\ServerRepository;
15
use Gameap\Services\ServerControlService;
16
use Gameap\Services\ServerService;
17
use Illuminate\Http\Request;
18
use Illuminate\Http\Response;
19
use Illuminate\Support\Facades\Auth;
20
use Spatie\QueryBuilder\QueryBuilder;
21
22
class ServersController extends AuthController
23
{
24
    /**
25
     * The ServerRepository instance.
26
     *
27
     * @var \Gameap\Repositories\ServerRepository
28
     */
29
    public $repository;
30
31
    /**
32
     * The GdaemonTaskRepository instance.
33
     *
34
     * @var GdaemonTaskRepository
35
     */
36
    public $gdaemonTaskRepository;
37
38
    /** @var \Gameap\Services\ServerService  */
39
    public $serverService;
40
41
    /** @var \Gameap\Services\ServerControlService */
42
    public $serverControlService;
43
44
    /**
45
     * ServersController constructor.
46
     * @param ServerRepository $repository
47
     */
48
    public function __construct(
49
        ServerRepository $repository,
50
        GdaemonTaskRepository $gdaemonTaskRepository,
51
        ServerService $serverService,
52
        ServerControlService $serverControlService
53
    ) {
54
        parent::__construct();
55
56
        $this->repository            = $repository;
57
        $this->gdaemonTaskRepository = $gdaemonTaskRepository;
58
        $this->serverService         = $serverService;
59
        $this->serverControlService  = $serverControlService;
60
    }
61
62
    /**
63
     * @param Server $server
64
     *
65
     * @return array|\Illuminate\Http\JsonResponse
66
     *
67
     * @throws \Illuminate\Auth\Access\AuthorizationException
68
     */
69
    public function start(Server $server)
70
    {
71
        $this->authorize('server-control', $server);
72
        $this->authorize('server-start', $server);
73
74
        try {
75
            $gdaemonTaskId = $this->serverControlService->start($server);
76
        } catch (GdaemonTaskRepositoryException $exception) {
77
            return $this->handleException($exception);
78
        } catch (RecordExistExceptions $exception) {
79
            $gdaemonTaskId = $this->gdaemonTaskRepository->getOneWorkingTaskId(
80
                $server->id,
81
                GdaemonTask::TASK_SERVER_START
82
            );
83
84
            if (!$gdaemonTaskId) {
85
                return $this->makeErrorResponse($exception->getMessage());
86
            }
87
        }
88
89
        return [
90
            'gdaemonTaskId' => $gdaemonTaskId,
91
        ];
92
    }
93
94
    /**
95
     * @param Server $server
96
     *
97
     * @return array|\Illuminate\Http\JsonResponse
98
     *
99
     * @throws \Illuminate\Auth\Access\AuthorizationException
100
     */
101
    public function stop(Server $server)
102
    {
103
        $this->authorize('server-control', $server);
104
        $this->authorize('server-stop', $server);
105
106
        try {
107
            $gdaemonTaskId = $this->serverControlService->stop($server);
108
        } catch (RecordExistExceptions $exception) {
109
            $gdaemonTaskId = $this->gdaemonTaskRepository->getOneWorkingTaskId(
110
                $server->id,
111
                GdaemonTask::TASK_SERVER_STOP
112
            );
113
114
            if (!$gdaemonTaskId) {
115
                return $this->makeErrorResponse($exception->getMessage());
116
            }
117
        }
118
119
        return [
120
            'gdaemonTaskId' => $gdaemonTaskId,
121
        ];
122
    }
123
124
    /**
125
     * @param Server $server
126
     * @return array|\Illuminate\Http\JsonResponse
127
     *
128
     * @throws \Gameap\Exceptions\Repositories\RecordExistExceptions
129
     * @throws \Illuminate\Auth\Access\AuthorizationException
130
     */
131
    public function restart(Server $server)
132
    {
133
        $this->authorize('server-control', $server);
134
        $this->authorize('server-restart', $server);
135
136
        try {
137
            $gdaemonTaskId = $this->serverControlService->restart($server);
138
        } catch (GdaemonTaskRepositoryException $exception) {
139
            return $this->handleException($exception);
140
        } catch (RecordExistExceptions $exception) {
141
            $gdaemonTaskId = $this->gdaemonTaskRepository->getOneWorkingTaskId(
142
                $server->id,
143
                GdaemonTask::TASK_SERVER_RESTART
144
            );
145
146
            if (!$gdaemonTaskId) {
147
                return $this->makeErrorResponse($exception->getMessage());
148
            }
149
        }
150
151
        return [
152
            'gdaemonTaskId' => $gdaemonTaskId,
153
        ];
154
    }
155
156
    /**
157
     * @param Server $server
158
     * @return array|\Illuminate\Http\JsonResponse
159
     *
160
     * @throws \Illuminate\Auth\Access\AuthorizationException
161
     */
162
    public function update(Server $server)
163
    {
164
        $this->authorize('server-control', $server);
165
        $this->authorize('server-update', $server);
166
167
        try {
168
            $gdaemonTaskId = $this->serverControlService->update($server);
169
        } catch (RecordExistExceptions $exception) {
170
            $gdaemonTaskId = $this->gdaemonTaskRepository->getOneWorkingTaskId(
171
                $server->id,
172
                GdaemonTask::TASK_SERVER_UPDATE
173
            );
174
175
            if (!$gdaemonTaskId) {
176
                return $this->makeErrorResponse($exception->getMessage());
177
            }
178
        }
179
180
        return [
181
            'gdaemonTaskId' => $gdaemonTaskId,
182
        ];
183
    }
184
185
    /**
186
     * @param Server $server
187
     * @return array|\Illuminate\Http\JsonResponse
188
     *
189
     * @throws \Illuminate\Auth\Access\AuthorizationException
190
     */
191
    public function install(Server $server)
192
    {
193
        $this->authorize('server-control', $server);
194
        $this->authorize('server-update', $server);
195
196
        try {
197
            $gdaemonTaskId = $this->serverControlService->install($server);
198
        } catch (RecordExistExceptions $exception) {
199
            $gdaemonTaskId = $this->gdaemonTaskRepository->getOneWorkingTaskId(
200
                $server->id,
201
                GdaemonTask::TASK_SERVER_INSTALL
202
            );
203
204
            if (!$gdaemonTaskId) {
205
                return $this->makeErrorResponse($exception->getMessage());
206
            }
207
        }
208
209
        return [
210
            'gdaemonTaskId' => $gdaemonTaskId,
211
        ];
212
    }
213
214
    /**
215
     * @param Server $server
216
     * @return array|\Illuminate\Http\JsonResponse
217
     *
218
     * @throws \Gameap\Exceptions\Repositories\RecordExistExceptions
219
     * @throws \Illuminate\Auth\Access\AuthorizationException
220
     */
221
    public function reinstall(Server $server)
222
    {
223
        $this->authorize('server-control', $server);
224
        $this->authorize('server-update', $server);
225
226
        try {
227
            $deleteTaskId  = $this->gdaemonTaskRepository->addServerDelete($server);
228
            $gdaemonTaskId = $this->gdaemonTaskRepository->addServerInstall($server, $deleteTaskId);
229
        } catch (RecordExistExceptions $exception) {
230
            return $this->makeErrorResponse($exception->getMessage());
231
        }
232
233
        return [
234
            'gdaemonTaskId' => $gdaemonTaskId,
235
        ];
236
    }
237
238
    /**
239
     * Get server status
240
     * @param Server $server
241
     * @return array
242
     *
243
     * @throws \Illuminate\Auth\Access\AuthorizationException
244
     */
245
    public function getStatus(Server $server)
246
    {
247
        $this->authorize('server-control', $server);
248
249
        return [
250
            'processActive' => $server->processActive(),
251
        ];
252
    }
253
254
    /**
255
     * @param Server $server
256
     * @return array
257
     *
258
     * @throws \Illuminate\Auth\Access\AuthorizationException
259
     */
260
    public function query(Server $server)
261
    {
262
        $this->authorize('server-control', $server);
263
        $query = $this->serverService->query($server);
264
265
        return $query;
266
    }
267
268
    /**
269
     * @param Server $server
270
     * @return array
271
     *
272
     * @throws \Illuminate\Auth\Access\AuthorizationException
273
     */
274
    public function consoleLog(Server $server)
275
    {
276
        $this->authorize('server-control', $server);
277
        $this->authorize('server-console-view', $server);
278
279
        return [
280
            'console' => $this->serverService->getConsoleLog($server),
281
        ];
282
    }
283
284
    /**
285
     * @param ServerConsoleCommandRequest $request
286
     * @param Server $server
287
     * @return array
288
     *
289
     * @throws \Illuminate\Auth\Access\AuthorizationException
290
     */
291
    public function sendCommand(ServerConsoleCommandRequest $request, Server $server)
292
    {
293
        $this->authorize('server-control', $server);
294
        $this->authorize('server-console-send', $server);
295
296
        $command = $request->input('command');
297
        $this->serverService->sendConsoleCommand($server, $command);
298
299
        return ['message' => 'success'];
300
    }
301
302
    public function search(Request $request)
303
    {
304
        $query = $request->input('q');
305
        return $this->repository->search($query);
306
    }
307
308
    public function getList()
309
    {
310
        return QueryBuilder::for(Server::class)
311
            ->allowedFilters('ds_id')
312
            ->allowedAppends('full_path')
313
            ->get([
314
                'id',
315
                'uuid',
316
                'uuid_short',
317
                'enabled',
318
                'installed',
319
                'blocked',
320
                'name',
321
                'ds_id',
322
                'game_id',
323
                'game_mod_id',
324
                'server_ip',
325
                'server_port',
326
                'query_port',
327
                'rcon_port',
328
                'dir',
329
            ]);
330
    }
331
332
    /**
333
     * @param Exception $exception
334
     *
335
     * @return \Illuminate\Http\JsonResponse
336
     */
337
    private function handleException(\Throwable $exception)
338
    {
339
        if (Auth::user()->can('admin roles & permissions')) {
340
            $extraMessage = $this->getDocMessage($exception);
341
        } else {
342
            $extraMessage = (string)__('main.common_admin_error');
343
        }
344
345
        return $this->makeErrorResponse($exception->getMessage() . $extraMessage);
346
    }
347
348
    /**
349
     * @param Exception $exception
350
     * @return string
351
     */
352
    private function getDocMessage(\Throwable $exception)
353
    {
354
        $msg = '';
355
356
        if ($exception instanceof EmptyServerStartCommandException) {
357
            $msg = __('gdaemon_tasks.empty_server_start_command_doc');
358
        }
359
360
        return is_string($msg) ? $msg : '';
361
    }
362
363
    /**
364
     * @param $message
365
     * @param int $code
366
     * @return \Illuminate\Http\JsonResponse
367
     */
368
    private function makeErrorResponse($message, $code = Response::HTTP_UNPROCESSABLE_ENTITY)
369
    {
370
        return response()->json([
371
            'message'   => $message,
372
            'http_code' => $code,
373
        ], $code);
374
    }
375
}
376