Passed
Push — develop ( 320e21...2f1f26 )
by Nikita
05:26
created

ServerRepository::updateVars()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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