Test Setup Failed
Push — develop ( 217915...b69313 )
by Nikita
196:19 queued 120:06
created

ServersController::getStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 9
    public function __construct(
49
        ServerRepository $repository,
50
        GdaemonTaskRepository $gdaemonTaskRepository,
51
        ServerService $serverService,
52
        ServerControlService $serverControlService
53
    ) {
54 9
        parent::__construct();
55
56 9
        $this->repository            = $repository;
57 9
        $this->gdaemonTaskRepository = $gdaemonTaskRepository;
58 9
        $this->serverService         = $serverService;
59 9
        $this->serverControlService  = $serverControlService;
60 9
    }
61
62
    /**
63
     * @param Server $server
64
     *
65
     * @return array|\Illuminate\Http\JsonResponse
66
     *
67
     * @throws \Illuminate\Auth\Access\AuthorizationException
68
     */
69 9
    public function start(Server $server)
70
    {
71 9
        $this->authorize('server-control', $server);
72 9
        $this->authorize('server-start', $server);
73
74
        try {
75 9
            $gdaemonTaskId = $this->serverControlService->start($server);
76 6
        } catch (GdaemonTaskRepositoryException $exception) {
77 3
            return $this->handleException($exception);
78 3
        } catch (RecordExistExceptions $exception) {
79 3
            $gdaemonTaskId = $this->gdaemonTaskRepository->getOneWorkingTaskId(
80 3
                $server->id,
81 3
                GdaemonTask::TASK_SERVER_START
82
            );
83
84 3
            if (!$gdaemonTaskId) {
85
                return $this->makeErrorResponse($exception->getMessage());
86
            }
87
        }
88
89
        return [
90 6
            '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 \Gameap\Exceptions\Repositories\RecordExistExceptions
190
     * @throws \Illuminate\Auth\Access\AuthorizationException
191
     */
192
    public function reinstall(Server $server)
193
    {
194
        $this->authorize('server-control', $server);
195
        $this->authorize('server-update', $server);
196
197
        try {
198
            $deleteTaskId  = $this->gdaemonTaskRepository->addServerDelete($server);
199
            $gdaemonTaskId = $this->gdaemonTaskRepository->addServerUpdate($server, $deleteTaskId);
200
        } catch (RecordExistExceptions $exception) {
201
            return $this->makeErrorResponse($exception->getMessage());
202
        }
203
204
        return [
205
            'gdaemonTaskId' => $gdaemonTaskId,
206
        ];
207
    }
208
209
    /**
210
     * Get server status
211
     * @param Server $server
212
     * @return array
213
     *
214
     * @throws \Illuminate\Auth\Access\AuthorizationException
215
     */
216
    public function getStatus(Server $server)
217
    {
218
        $this->authorize('server-control', $server);
219
220
        return [
221
            'processActive' => $server->processActive(),
222
        ];
223
    }
224
225
    /**
226
     * @param Server $server
227
     * @return array
228
     *
229
     * @throws \Illuminate\Auth\Access\AuthorizationException
230
     */
231
    public function query(Server $server)
232
    {
233
        $this->authorize('server-control', $server);
234
        $query = $this->serverService->query($server);
235
236
        return $query;
237
    }
238
239
    /**
240
     * @param Server $server
241
     * @return array
242
     *
243
     * @throws \Illuminate\Auth\Access\AuthorizationException
244
     */
245
    public function consoleLog(Server $server)
246
    {
247
        $this->authorize('server-control', $server);
248
        $this->authorize('server-console-view', $server);
249
250
        return [
251
            'console' => $this->serverService->getConsoleLog($server),
252
        ];
253
    }
254
255
    /**
256
     * @param ServerConsoleCommandRequest $request
257
     * @param Server $server
258
     * @return array
259
     *
260
     * @throws \Illuminate\Auth\Access\AuthorizationException
261
     */
262
    public function sendCommand(ServerConsoleCommandRequest $request, Server $server)
263
    {
264
        $this->authorize('server-control', $server);
265
        $this->authorize('server-console-send', $server);
266
267
        $command = $request->input('command');
268
        $this->serverService->sendConsoleCommand($server, $command);
269
270
        return ['message' => 'success'];
271
    }
272
273
    public function search(Request $request)
274
    {
275
        $query = $request->input('q');
276
        return $this->repository->search($query);
277
    }
278
279
    public function getList()
280
    {
281
        return QueryBuilder::for(Server::class)
282
            ->allowedFilters('ds_id')
283
            ->allowedAppends('full_path')
284
            ->get([
285
                'id',
286
                'uuid',
287
                'uuid_short',
288
                'enabled',
289
                'installed',
290
                'blocked',
291 3
                'name',
292
                'ds_id',
293 3
                'game_id',
294 3
                'game_mod_id',
295
                'server_ip',
296
                'server_port',
297
                'query_port',
298
                'rcon_port',
299 3
                'dir',
300
            ]);
301
    }
302
303
    /**
304
     * @param Exception $exception
305
     *
306 3
     * @return \Illuminate\Http\JsonResponse
307
     */
308 3
    private function handleException(\Throwable $exception)
309
    {
310 3
        if (Auth::user()->can('admin roles & permissions')) {
311
            $extraMessage = $this->getDocMessage($exception);
312
        } else {
313
            $extraMessage = (string)__('main.common_admin_error');
314 3
        }
315
316
        return $this->makeErrorResponse($exception->getMessage() . $extraMessage);
317
    }
318
319
    /**
320
     * @param Exception $exception
321
     * @return string
322 3
     */
323
    private function getDocMessage(\Throwable $exception)
324 3
    {
325 3
        $msg = '';
326 3
327 1
        if ($exception instanceof EmptyServerStartCommandException) {
328
            $msg = __('gdaemon_tasks.empty_server_start_command_doc');
329
        }
330
331
        return is_string($msg) ? $msg : '';
332
    }
333
334
    /**
335
     * @param $message
336
     * @param int $code
337
     * @return \Illuminate\Http\JsonResponse
338
     */
339
    private function makeErrorResponse($message, $code = Response::HTTP_UNPROCESSABLE_ENTITY)
340
    {
341
        return response()->json([
342
            'message'   => $message,
343
            'http_code' => $code,
344
        ], $code);
345
    }
346
}
347