Passed
Push — develop ( 2f1f26...b139eb )
by Nikita
06:08
created

ServerRepository::getAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
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\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
        $attributes['enabled'] = true;
42
        $attributes['blocked'] = false;
43
44
        $addInstallTask = false;
45
        if (isset($attributes['install'])) {
46
            $attributes['installed'] = ! $attributes['install'];
47
            $addInstallTask = true;
48
49
            unset($attributes['install']);
50
        }
51
52
        if (empty($attributes['start_command'])) {
53
            $gameMod = GameMod::select('default_start_cmd')->where('id', '=', $attributes['game_mod_id'])->firstOrFail();
54
            $attributes['start_command'] = $gameMod->default_start_cmd;
55
        }
56
57
        $server = Server::create($attributes);
58
59
        if ($addInstallTask) {
60
            $this->gdaemonTaskRepository->addServerUpdate($server);
61
        }
62
    }
63
64
    /**
65
     * Get Servers list for Dedicated server
66
     *
67
     * @param int $dedicatedServerId
68
     */
69
    public function getServersListForDedicatedServer(int $dedicatedServerId)
70
    {
71
        return $this->model->select('*')
72
            ->where('ds_id', '=', $dedicatedServerId)
73
            ->get();
74
    }
75
76
77
    /**
78
     * Get Servers id list for Dedicated server
79
     *
80
     * @param int $dedicatedServerId
81
     * @return mixed
82
     */
83
    public function getServerIdsForDedicatedServer(int $dedicatedServerId)
84
    {
85
        return $this->model->select('id')
86
            ->where('ds_id', '=', $dedicatedServerId)
87
            ->get();
88
    }
89
90
    /**
91
     * @return mixed
92
     */
93
    public function getServersForAuth()
94
    {
95
        if (Auth::user()->can('admin roles & permissions')) {
96
            return $this->getAll();
97
        } else {
98
            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...
99
        }
100
    }
101
102
    /**
103
     * @param $query
104
     * @return mixed
105
     */
106
    public function search($query)
107
    {
108
        return $this->model->select(['id', 'name', 'server_ip', 'server_port', 'game_id', 'game_mod_id'])
109
            ->with(['game' => function($query) {
110
                $query->select('code','name');
111
            }])
112
            ->where('name', 'LIKE', '%' . $query . '%')
113
            ->get();
114
    }
115
116
    /**
117
     * @param Server $server
118
     * @param array  $attributes
119
     */
120
    public function update(Server $server, array $attributes)
121
    {
122
        $attributes['enabled'] = (bool)array_key_exists('enabled', $attributes);
123
        $server->update($attributes);
124
    }
125
126
    /**
127
     * @param Server            $server
128
     * @param ServerVarsRequest $request
129
     */
130
    public function updateVars(Server $server, ServerVarsRequest $request)
131
    {
132
        $only = [];
133
        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...
134
            if ($var['admin_var'] && Auth::user()->cannot('admin roles & permissions')) {
135
                continue;
136
            }
137
138
            $only[] = 'vars.' . $var['var'];
139
        }
140
141
        $server->update($request->only($only));
142
    }
143
}