Test Setup Failed
Push — develop ( 3077e7...601b88 )
by Nikita
05:19
created

ServerRepository::search()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Gameap\Repositories;
4
5
use Gameap\Models\DedicatedServer;
6
use Gameap\Models\Server;
7
use Gameap\Models\GameMod;
8
use Illuminate\Support\Str;
9
use Gameap\Http\Requests\ServerRequest;
10
use Gameap\Http\Requests\ServerVarsRequest;
11
use Illuminate\Support\Facades\Auth;
12
13
class ServerRepository
14
{
15
    protected $model;
16
17
    protected $gdaemonTaskRepository;
18
19
    public function __construct(Server $server, GdaemonTaskRepository $gdaemonTaskRepository)
20
    {
21
        $this->model = $server;
22
        $this->gdaemonTaskRepository = $gdaemonTaskRepository;
23
    }
24
25
    public function getAll($perPage = 20)
26
    {
27
        $servers = Server::orderBy('id')->with('game')->paginate($perPage);
28
29
        return $servers;
30
    }
31
32
    /**
33
     * Store server
34
     * 
35
     * @param array $attributes
36
     */
37
    public function store(array $attributes)
38
    {
39
        $attributes['uuid'] = Str::orderedUuid()->toString();
40
        $attributes['uuid_short'] = Str::substr($attributes['uuid'], 0, 8);
41
        
42
        $attributes['enabled'] = true;
43
        $attributes['blocked'] = false;
44
45
        $addInstallTask = false;
46
        if (isset($attributes['install'])) {
47
            $attributes['installed'] = ! $attributes['install'];
48
            $addInstallTask = true;
49
50
            unset($attributes['install']);
51
        }
52
53
        $dedicatedServer = DedicatedServer::findOrFail($attributes['ds_id']);
54
55
        if (empty($attributes['start_command'])) {
56
            $gameMod = GameMod::select('default_start_cmd_linux', 'default_start_cmd_windows')->where('id', '=', $attributes['game_mod_id'])->firstOrFail();
57
58
            $attributes['start_command'] =
59
                $dedicatedServer->isLinux()
60
                    ? $gameMod->default_start_cmd_linux
61
                    : $gameMod->default_start_cmd_windows;
62
        }
63
64
        // Fix path. Remove absolute dedicated server path
65
        $attributes['dir'] = $this->fixPath($attributes['dir'], $dedicatedServer->work_path);
66
67
        $server = Server::create($attributes);
68
69
        if ($addInstallTask) {
70
            $this->gdaemonTaskRepository->addServerUpdate($server);
71
        }
72
    }
73
74
    /**
75
     * Get Servers list for Dedicated server
76
     *
77
     * @param int $dedicatedServerId
78
     */
79
    public function getServersListForDedicatedServer(int $dedicatedServerId)
80
    {
81
        return $this->model->select('*')
82
            ->where('ds_id', '=', $dedicatedServerId)
83
            ->get();
84
    }
85
86
87
    /**
88
     * Get Servers id list for Dedicated server
89
     *
90
     * @param int $dedicatedServerId
91
     * @return mixed
92
     */
93
    public function getServerIdsForDedicatedServer(int $dedicatedServerId)
94
    {
95
        return $this->model->select('id')
96
            ->where('ds_id', '=', $dedicatedServerId)
97
            ->get();
98
    }
99
100
    /**
101
     * @return mixed
102
     */
103
    public function getServersForAuth()
104
    {
105
        if (Auth::user()->can('admin roles & permissions')) {
106
            return $this->getAll();
107
        } else {
108
            return Auth::user()->servers;
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...
109
        }
110
    }
111
112
    /**
113
     * @param $query
114
     * @return mixed
115
     */
116
    public function search($query)
117
    {
118
        return $this->model->select(['id', 'name', 'server_ip', 'server_port', 'game_id', 'game_mod_id'])
119
            ->with(['game' => function($query) {
120
                $query->select('code','name');
121
            }])
122
            ->where('name', 'LIKE', '%' . $query . '%')
123
            ->get();
124
    }
125
126
    /**
127
     * @param Server $server
128
     * @param array  $attributes
129
     */
130
    public function update(Server $server, array $attributes)
131
    {
132
        $attributes['enabled'] = (bool)array_key_exists('enabled', $attributes);
133
        $attributes['blocked'] = (bool)array_key_exists('blocked', $attributes);
134
        $attributes['installed'] = (bool)array_key_exists('installed', $attributes);
135
136
        // Fix path. Remove absolute dedicated server path
137
        $attributes['dir'] = $this->fixPath($attributes['dir'], $server->dedicatedServer->work_path);
138
        
139
        $server->update($attributes);
140
    }
141
142
    /**
143
     * @param Server            $server
144
     * @param ServerVarsRequest $request
145
     */
146
    public function updateVars(Server $server, ServerVarsRequest $request)
147
    {
148
        $only = [];
149
        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...
150
            if (!empty($var['admin_var']) && Auth::user()->cannot('admin roles & permissions')) {
151
                continue;
152
            }
153
154
            $only[] = 'vars.' . $var['var'];
155
        }
156
157
        $server->update($request->only($only));
158
    }
159
160
    private function fixPath($path, $dsWorkPath)
161
    {
162
        if (substr($path, 0, strlen($dsWorkPath)) == $dsWorkPath) {
163
            $path = substr($path, strlen($dsWorkPath));
164
        }
165
166
        $path = ltrim($path, '/\\');
167
168
        return $path;
169
    }
170
}