Passed
Push — develop ( 662b7b...49c268 )
by Nikita
06:08 queued 01:03
created

ServersController::restart()   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
eloc 3
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 1
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Gameap\Http\Controllers\API;
4
5
use Gameap\Http\Controllers\AuthController;
6
use Gameap\Repositories\ServerRepository;
7
use Gameap\Repositories\GdaemonTaskRepository;
8
use Gameap\Models\Server;
9
use Gameap\Services\ServerService;
10
use Gameap\Http\Requests\API\ServerConsoleCommandRequest;
11
use Illuminate\Http\Request;
12
13
class ServersController extends AuthController
14
{
15
    /**
16
     * The ServerRepository instance.
17
     *
18
     * @var \Gameap\Repositories\ServerRepository
19
     */
20
    public $repository;
21
22
    /**
23
     * The GdaemonTaskRepository instance.
24
     *
25
     * @var GdaemonTaskRepository
26
     */
27
    public $gdaemonTaskRepository;
28
29
    /**
30
     * @var \Gameap\Services\ServerService
31
     */
32
    public $serverService;
33
34
    /**
35
     * ServersController constructor.
36
     * @param ServerRepository $repository
37
     */
38
    public function __construct(
39
        ServerRepository $repository,
40
        GdaemonTaskRepository $gdaemonTaskRepository,
41
        ServerService $serverService
42
    ) {
43
        parent::__construct();
44
45
        $this->repository = $repository;
46
        $this->gdaemonTaskRepository = $gdaemonTaskRepository;
47
        $this->serverService = $serverService;
48
    }
49
50
    /**
51
     * @param Server $server
52
     *
53
     * @return array
54
     *
55
     * @throws \Gameap\Exceptions\Repositories\RecordExistExceptions
56
     * @throws \Illuminate\Auth\Access\AuthorizationException
57
     */
58
    public function start(Server $server)
59
    {
60
        $this->authorize('server-control', $server);
61
62
        return [
63
            'gdaemonTaskId' => $this->gdaemonTaskRepository->addServerStart($server)
64
        ];
65
    }
66
67
    /**
68
     * @param Server $server
69
     *
70
     * @return array
71
     *
72
     * @throws \Gameap\Exceptions\Repositories\RecordExistExceptions
73
     * @throws \Illuminate\Auth\Access\AuthorizationException
74
     */
75
    public function stop(Server $server)
76
    {
77
        $this->authorize('server-control', $server);
78
79
        return [
80
            'gdaemonTaskId' => $this->gdaemonTaskRepository->addServerStop($server)
81
        ];
82
    }
83
84
    /**
85
     * @param Server $server
86
     * @return array
87
     *
88
     * @throws \Gameap\Exceptions\Repositories\RecordExistExceptions
89
     * @throws \Illuminate\Auth\Access\AuthorizationException
90
     */
91
    public function restart(Server $server)
92
    {
93
        $this->authorize('server-control', $server);
94
95
        return [
96
            'gdaemonTaskId' => $this->gdaemonTaskRepository->addServerRestart($server)
97
        ];
98
    }
99
100
    /**
101
     * @param Server $server
102
     * @return array
103
     *
104
     * @throws \Gameap\Exceptions\Repositories\RecordExistExceptions
105
     * @throws \Illuminate\Auth\Access\AuthorizationException
106
     */
107
    public function update(Server $server)
108
    {
109
        $this->authorize('server-control', $server);
110
111
        return [
112
            'gdaemonTaskId' => $this->gdaemonTaskRepository->addServerUpdate($server)
113
        ];
114
    }
115
116
    /**
117
     * @param Server $server
118
     * @return array
119
     *
120
     * @throws \Gameap\Exceptions\Repositories\RecordExistExceptions
121
     * @throws \Illuminate\Auth\Access\AuthorizationException
122
     */
123
    public function reinstall(Server $server)
124
    {
125
        $this->authorize('server-control', $server);
126
127
        $deleteTaskId = $this->gdaemonTaskRepository->addServerDelete($server);
128
129
        return [
130
            'gdaemonTaskId' => $this->gdaemonTaskRepository->addServerUpdate($server, $deleteTaskId)
131
        ];
132
    }
133
134
    /**
135
     * Get server status
136
     * @param Server $server
137
     * @return array
138
     *
139
     * @throws \Illuminate\Auth\Access\AuthorizationException
140
     */
141
    public function getStatus(Server $server)
142
    {
143
        $this->authorize('server-control', $server);
144
145
        return [
146
            'processActive' => $server->processActive()
147
        ];
148
    }
149
150
    /**
151
     * @param Server $server
152
     * @return array
153
     *
154
     * @throws \Illuminate\Auth\Access\AuthorizationException
155
     */
156
    public function query(Server $server)
157
    {
158
        $this->authorize('server-control', $server);
159
        $query = $this->serverService->query($server);
160
161
        return $query;
162
    }
163
164
    /**
165
     * @param Server $server
166
     * @return array
167
     *
168
     * @throws \Illuminate\Auth\Access\AuthorizationException
169
     */
170
    public function consoleLog(Server $server)
171
    {
172
        $this->authorize('server-control', $server);
173
        return [
174
            'console' => $this->serverService->getConsoleLog($server)
175
        ];
176
    }
177
178
    /**
179
     * @param ServerConsoleCommandRequest $request
180
     * @param Server $server
181
     * @return array
182
     *
183
     * @throws \Illuminate\Auth\Access\AuthorizationException
184
     */
185
    public function sendCommand(ServerConsoleCommandRequest $request, Server $server)
186
    {
187
        $this->authorize('server-control', $server);
188
189
        $command = $request->input('command');
190
        $this->serverService->sendConsoleCommand($server, $command);
191
192
        return ['message' => 'success'];
193
    }
194
195
    /**
196
     * @param Request $request
197
     *
198
     * TODO: Create admin part and move this
199
     *
200
     * @return mixed
201
     */
202
    public function search(Request $request)
203
    {
204
        $query = $request->input('q');
205
        return $this->repository->search($query);
206
    }
207
}