Passed
Push — develop ( 2a02d4...7461cb )
by Nikita
05:45
created

ServersController::search()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
    public function start(Server $server)
56
    {
57
        $this->authorize('server-control', $server);
58
59
        return [
60
            'gdaemonTaskId' => $this->gdaemonTaskRepository->addServerStart($server)
61
        ];
62
    }
63
64
    /**
65
     * @param Server $server
66
     *
67
     * @return array
68
     */
69
    public function stop(Server $server)
70
    {
71
        $this->authorize('server-control', $server);
72
73
        return [
74
            'gdaemonTaskId' => $this->gdaemonTaskRepository->addServerStop($server)
75
        ];
76
    }
77
78
    /**
79
     * @param Server $server
80
     * @return array
81
     */
82
    public function restart(Server $server)
83
    {
84
        $this->authorize('server-control', $server);
85
86
        return [
87
            'gdaemonTaskId' => $this->gdaemonTaskRepository->addServerRestart($server)
88
        ];
89
    }
90
91
    /**
92
     * @param Server $server
93
     * @return array
94
     */
95
    public function update(Server $server)
96
    {
97
        $this->authorize('server-control', $server);
98
99
        return [
100
            'gdaemonTaskId' => $this->gdaemonTaskRepository->addServerUpdate($server)
101
        ];
102
    }
103
104
    /**
105
     * Get server status
106
     * @param Server $server
107
     * @return array
108
     */
109
    public function getStatus(Server $server)
110
    {
111
        $this->authorize('server-control', $server);
112
113
        return [
114
            'processActive' => $server->processActive()
115
        ];
116
    }
117
118
    /**
119
     * @param Server $server
120
     * @return array
121
     */
122
    public function query(Server $server)
123
    {
124
        $this->authorize('server-control', $server);
125
        $query = $this->serverService->query($server);
126
127
        return $query;
128
    }
129
130
    /**
131
     * @param Server $server
132
     * @return array
133
     */
134
    public function consoleLog(Server $server)
135
    {
136
        $this->authorize('server-control', $server);
137
        return [
138
            'console' => $this->serverService->getConsoleLog($server)
139
        ];
140
    }
141
142
    /**
143
     * @param ServerConsoleCommandRequest $request
144
     * @param Server $server
145
     * @return array
146
     */
147
    public function sendCommand(ServerConsoleCommandRequest $request, Server $server)
148
    {
149
        $this->authorize('server-control', $server);
150
151
        $command = $request->input('command');
152
        $this->serverService->sendConsoleCommand($server, $command);
153
154
        return ['message' => 'success'];
155
    }
156
157
    /**
158
     * @param Request $request
159
     *
160
     * TODO: Create admin part and move this
161
     */
162
    public function search(Request $request)
163
    {
164
        $query = $request->input('q');
165
        return $this->repository->search($query);
166
    }
167
}