Passed
Push — develop ( 3a01ff...c88867 )
by Nikita
05:53
created

Server   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Test Coverage

Coverage 96.08%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 19
eloc 71
c 5
b 0
f 1
dl 0
loc 171
ccs 49
cts 51
cp 0.9608
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getFullPathAttribute() 0 3 1
A gameMod() 0 3 1
A users() 0 3 1
A getFileManagerDisksAttribute() 0 21 3
A getAliasesAttribute() 0 22 5
A settings() 0 3 1
A processActive() 0 18 4
A tasks() 0 3 1
A game() 0 3 1
A dedicatedServer() 0 3 1
1
<?php
2
3
namespace Gameap\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\BelongsTo;
7
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
8
use Illuminate\Database\Eloquent\SoftDeletes;
9
use Carbon\Carbon;
10
11
/**
12
 * Server model
13
 * @package Gameap\Models
14
 *
15
 * @property integer $id
16
 * @property boolean $enabled
17
 * @property boolean $installed
18
 * @property boolean $blocked
19
 * @property string $name
20
 * @property string $uuid
21
 * @property string $uuid_short
22
 * @property string $game_id
23
 * @property integer $ds_id
24
 * @property integer $game_mod_id
25
 * @property string $expires
26
 * @property string $server_ip
27
 * @property integer $server_port
28
 * @property integer $query_port
29
 * @property integer $rcon_port
30
 * @property string $rcon RCON password
31
 * @property string $dir
32
 * @property string $su_user
33
 * @property integer $cpu_limit
34
 * @property integer $ram_limit
35
 * @property integer $net_limit
36
 * @property string $start_command
37
 * @property string $stop_command
38
 * @property string $force_stop_command
39
 * @property string $restart_command
40
 * @property bool $process_active
41
 * @property string $last_process_check
42
 * @property array $vars
43
 * @property string $created_at
44
 * @property string $updated_at
45
 * @property string $deleted_at
46
 *
47
 * @property string $full_path
48
 *
49
 * @property DedicatedServer $dedicatedServer
50
 * @property Game $game
51
 * @property GameMod $gameMod
52
 * @property ServerSetting[] $settings
53
 * @property User[] $users
54
 * @property ServerTask[] $tasks
55
 */
56
class Server extends Model
57
{
58
    use SoftDeletes;
59
60
    const TIME_EXPIRE_PROCESS_CHECK = 120;
61
62
    // Installed statuses
63
    const NOT_INSTALLED              = 0;
64
    const INSTALLED                  = 1;
65
    const INSTALLATION_PROCESS       = 2;
66
67
    protected $fillable = [
68
        'uuid', 'uuid_short',
69
        'enabled', 'name', 'code_name', 'game_id',
70
        'ds_id', 'game_mod_id', 'expires',
71
        'installed', 'blocked', 'server_ip', 'server_port',
72
        'query_port', 'rcon_port',
73
        'rcon', 'dir', 'su_user',
74
        'cpu_limit', 'ram_limit', 'net_limit',
75
        'start_command', 'stop_command',
76
        'force_stop_command', 'restart_command',
77
        'vars',
78
    ];
79
80
    protected $casts = [
81
        'vars' => 'array',
82
        'enabled' => 'boolean',
83
        'installed' => 'integer',
84
        'blocked' => 'boolean',
85
        'ds_id' => 'integer',
86
        'game_mod_id' => 'integer',
87
        'server_port' => 'integer',
88
        'query_port' => 'integer',
89
        'rcon_port' => 'integer',
90
        'process_active' => 'boolean',
91
    ];
92
93
    /**
94
     * Get server status
95
     *
96
     * @return bool
97
     */
98 3
    public function processActive()
99
    {
100 3
        if (empty($this->last_process_check)) {
101 3
            return false;
102
        }
103
104 3
        $lastProcessCheck = Carbon::createFromDate(
105 3
            $this->last_process_check,
0 ignored issues
show
Bug introduced by
$this->last_process_check of type string is incompatible with the type integer|null expected by parameter $year of Carbon\Carbon::createFromDate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

105
            /** @scrutinizer ignore-type */ $this->last_process_check,
Loading history...
106 3
            null,
107 3
            null,
108 3
            'UTC'
109 3
        )->timestamp;
110
111 3
        if ($this->process_active && $lastProcessCheck >= Carbon::now()->timestamp - self::TIME_EXPIRE_PROCESS_CHECK) {
112 3
            return true;
113
        }
114
115 3
        return false;
116
    }
117
118
    /**
119
     * @return BelongsTo
120
     */
121 9
    public function dedicatedServer()
122
    {
123 9
        return $this->belongsTo(DedicatedServer::class, 'ds_id');
124
    }
125
126
    /**
127
     * @return BelongsTo
128
     */
129 6
    public function game()
130
    {
131 6
        return $this->belongsTo(Game::class, 'game_id', 'code');
132
    }
133
134
    /**
135
     * @return BelongsTo
136
     */
137 9
    public function gameMod()
138
    {
139 9
        return $this->belongsTo(GameMod::class, 'game_mod_id');
140
    }
141
142
    /**
143
     * One to many relation
144
     *
145
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
146
     */
147 9
    public function settings()
148
    {
149 9
        return $this->hasMany(ServerSetting::class);
150
    }
151
152
    /**
153
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
154
     */
155
    public function tasks()
156
    {
157
        return $this->hasMany(ServerTask::class);
158
    }
159
160
    /**
161
     * @return string
162
     */
163 6
    public function getFullPathAttribute()
164
    {
165 6
        return rtrim($this->dedicatedServer->work_path, '/') . '/' . ltrim($this->dir, '/');
166
    }
167
168
    /**
169
     * @return array
170
     */
171 3
    public function getFileManagerDisksAttribute()
172
    {
173
        $fileManagerDisks = [
174 3
            "server" => array_merge(
175 3
                $this->dedicatedServer->gdaemonSettings('local'),
176 3
                ['driver' => 'gameap', 'workDir' => $this->full_path, 'root' => $this->full_path]
177
            )
178
        ];
179
180 3
        $setting = $this->settings()->where('name', 'file-manager')->first();
181
182 3
        if (!empty($setting)) {
183 3
            $disks = json_decode($setting->value, true);
184
185 3
            if (!empty($disks)) {
186 3
                $fileManagerDisks = array_merge($fileManagerDisks, $disks);
187
            }
188
        }
189
190
191 3
        return $fileManagerDisks;
192
    }
193
194
    /**
195
     * @return BelongsToMany
196
     */
197 3
    public function users()
198
    {
199 3
        return $this->belongsToMany(User::class);
200
    }
201
202
    /**
203
     * @return array
204
     */
205 3
    public function getAliasesAttribute()
206
    {
207
        $aliases = [
208 3
            'ip' => $this->server_ip,
209 3
            'port' => $this->server_port,
210 3
            'query_port' => $this->query_port,
211 3
            'rcon_port' => $this->rcon_port,
212 3
            'rcon_password' => $this->rcon,
213 3
            'uuid' => $this->uuid,
214 3
            'uuid_short' => $this->uuid_short,
215
        ];
216
217 3
        if ($this->gameMod != null && is_array($this->gameMod->vars)) {
0 ignored issues
show
introduced by
The condition is_array($this->gameMod->vars) is always false.
Loading history...
218 3
            foreach ($this->gameMod->vars as $var) {
219 3
                $varname = $var['var'];
220 3
                $aliases[ $varname ] = isset($this->vars[$varname])
221 3
                    ? $this->vars[$varname]
222 3
                    : $var['default'];
223
            }
224
        }
225
226 3
        return $aliases;
227
    }
228
}