Passed
Push — master ( b8c666...25f75c )
by Nikita
20:49 queued 09:26
created

getServersListForDedicatedServer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Gameap\Repositories;
4
5
use Gameap\Helpers\ServerHelper;
0 ignored issues
show
Bug introduced by
The type Gameap\Helpers\ServerHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Gameap\Http\Requests\ServerVarsRequest;
7
use Gameap\Models\DedicatedServer;
8
use Gameap\Models\Game;
9
use Gameap\Models\GameMod;
10
use Gameap\Models\Server;
11
use Illuminate\Support\Facades\Auth;
12
use Illuminate\Support\Facades\DB;
13
use Illuminate\Support\Str;
14
use Mavinoo\Batch\Batch;
15
16
class ServerRepository
17
{
18
    public const DEFAULT_RCON_PASSWORD_LENGTH = 10;
19
20
    public const DEFAULT_PER_PAGE = 20;
21
22
    /** @var Server */
23
    protected $model;
24
25
    /** @var GdaemonTaskRepository */
26
    protected $gdaemonTaskRepository;
27
28
    /** @var Batch */
29
    protected $mavinooBatch;
30
31
    /**
32
     * ServerRepository constructor.
33
     * @param Server $server
34
     * @param GdaemonTaskRepository $gdaemonTaskRepository
35
     */
36
    public function __construct(
37
        Server $server,
38
        GdaemonTaskRepository $gdaemonTaskRepository,
39
        Batch $mavinooBatch
40
    ) {
41
        $this->model                 = $server;
42
        $this->gdaemonTaskRepository = $gdaemonTaskRepository;
43
        $this->mavinooBatch          = $mavinooBatch;
44
    }
45
46
    /**
47
     * @param int $perPage
48
     * @return mixed
49
     */
50
    public function getAll($perPage = self::DEFAULT_PER_PAGE)
51
    {
52
        $servers = Server::orderBy('id')->with('game')->paginate($perPage);
53
54
        return $servers;
55
    }
56
57
    /**
58
     * Get Servers list for Dedicated server
59
     *
60
     * @param int $dedicatedServerId
61
     * @return mixed
62
     */
63
    public function getServersListForDedicatedServer(int $dedicatedServerId)
64
    {
65
        return $this->model->select('*')
66
            ->where('ds_id', '=', $dedicatedServerId)
67
            ->get();
68
    }
69
70
71
    /**
72
     * Get Servers id list for Dedicated server
73
     *
74
     * @param int $dedicatedServerId
75
     * @return mixed
76
     */
77
    public function getServerIdsForDedicatedServer(int $dedicatedServerId)
78
    {
79
        return $this->model->select('id')
80
            ->where('ds_id', '=', $dedicatedServerId)
81
            ->get();
82
    }
83
84
    /**
85
     * @return mixed
86
     */
87
    public function getServersForAuth()
88
    {
89
        if (Auth::user()->can('admin roles & permissions')) {
90
            return $this->getAll();
91
        }
92
        return Auth::user()->servers->paginate(self::DEFAULT_PER_PAGE);
0 ignored issues
show
Bug introduced by
Accessing servers on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
93
    }
94
95
    /**
96
     * @param array $engines
97
     * @param int|array $dedicatedServers
98
     * @return \Illuminate\Support\Collection
99
     */
100
    public function getServersForEngine(array $engines, $dedicatedServers = [], $excludeIds = [])
101
    {
102
        if (is_int($dedicatedServers)) {
103
            $dedicatedServers = [$dedicatedServers];
104
        }
105
106
        $serversTable = $this->model->getTable();
107
        $gamesTable   = (new Game())->getTable();
108
109
        $query = DB::table($serversTable)
110
            ->selectRaw("{$serversTable}.*, {$gamesTable}.name as game_name")
111
            ->whereIn('game_id', function ($query) use ($engines, $serversTable, $gamesTable): void {
0 ignored issues
show
Unused Code introduced by
The import $serversTable is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
112
                $query->select('code')
113
                    ->from($gamesTable)
114
                    ->whereIn('engine', $engines);
115
            })
116
            ->where('deleted_at', null)
117
            ->join($gamesTable, "{$serversTable}.game_id", '=', "{$gamesTable}.code");
118
119
        if (!empty($dedicatedServers)) {
120
            $query->whereIn('ds_id', $dedicatedServers);
121
        }
122
123
        if (!empty($excludeIds)) {
124
            $query->whereNotIn('id', $excludeIds);
125
        }
126
127
        return $query->get();
128
    }
129
130
    /**
131
     * @param $query
132
     * @return mixed
133
     */
134
    public function search($query)
135
    {
136
        return $this->model->select(['id', 'name', 'server_ip', 'server_port', 'game_id', 'game_mod_id'])
137
            ->with(['game' => function ($query): void {
138
                $query->select('code', 'name');
139
            }])
140
            ->where('name', 'LIKE', '%' . $query . '%')
141
            ->get();
142
    }
143
144
    /**
145
     * @param Server $server
146
     * @param array  $attributes
147
     */
148
    public function update(Server $server, array $attributes): void
149
    {
150
        $attributes['enabled']   = (bool)array_key_exists('enabled', $attributes);
151
        $attributes['blocked']   = (bool)array_key_exists('blocked', $attributes);
152
        $attributes['installed'] = (bool)array_key_exists('installed', $attributes);
153
154
        if (isset($attributes['ds_id'])) {
155
            $server->ds_id = $attributes['ds_id'];
156
        }
157
        
158
        $server->update($attributes);
159
    }
160
161
    /**
162
     * @param Server            $server
163
     * @param ServerVarsRequest $request
164
     */
165
    public function updateVars(Server $server, ServerVarsRequest $request): void
166
    {
167
        $only = [];
168
        foreach ($server->gameMod->vars as $var) {
0 ignored issues
show
Bug introduced by
The expression $server->gameMod->vars of type string is not traversable.
Loading history...
169
            if (!empty($var['admin_var']) && Auth::user()->cannot('admin roles & permissions')) {
170
                continue;
171
            }
172
173
            $only[] = 'vars.' . $var['var'];
174
        }
175
176
        $server->update($request->only($only));
177
    }
178
179
    public function updateSettings(Server $server, ServerVarsRequest $request): void
180
    {
181
        $autostartSetting = $server->getSetting($server::AUTOSTART_SETTING_KEY);
182
        $autostartSetting->value = $request->autostart();
183
        $autostartSetting->save();
184
185
        $autostartCurrentSetting = $server->getSetting($server::AUTOSTART_CURRENT_SETTING_KEY);
186
        $autostartCurrentSetting->value = $request->autostart();
187
        $autostartCurrentSetting->save();
188
189
        $updateBeforeStartSetting = $server->getSetting($server::UPDATE_BEFORE_START_SETTING_KEY);
190
        $updateBeforeStartSetting->value = $request->updateBeforeStart();
191
        $updateBeforeStartSetting->save();
192
    }
193
194
    public function save(Server $server): void
195
    {
196
        $server->save();
197
    }
198
199
    public function saveBatch(array $serverValues): void
200
    {
201
        $this->mavinooBatch->update(new Server(), $serverValues, 'id');
202
    }
203
}
204