Passed
Push — develop ( 19cdc7...7b30bc )
by Nikita
10:28
created

Server   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 125
rs 10
c 0
b 0
f 0
wmc 16

9 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 settings() 0 3 1
A processActive() 0 13 4
A game() 0 3 1
A dedicatedServer() 0 3 1
A getAliasesAttribute() 0 20 3
1
<?php
2
3
namespace Gameap\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Carbon\Carbon;
7
8
/**
9
 * Server model
10
 * @package Gameap\Models
11
 *
12
 * @property integer $id
13
 * @property boolean $enabled
14
 * @property boolean $installed
15
 * @property boolean $blocked
16
 * @property string $name
17
 * @property string $uuid
18
 * @property string $uuid_short
19
 * @property string $game_id
20
 * @property integer $ds_id
21
 * @property integer $game_mod_id
22
 * @property string $expires
23
 * @property string $server_ip
24
 * @property integer $server_port
25
 * @property integer $query_port
26
 * @property integer $rcon_port
27
 * @property string $rcon RCON password
28
 * @property string $dir
29
 * @property string $su_user
30
 * @property integer $cpu_limit
31
 * @property integer $ram_limit
32
 * @property integer $net_limit
33
 * @property string $start_command
34
 * @property string $stop_command
35
 * @property string $force_stop_command
36
 * @property string $restart_command
37
 * @property bool $process_active
38
 * @property string $last_process_check
39
 * @property string $vars
40
 * @property string $created_at
41
 * @property string $updated_at
42
 * @property string $deleted_at
43
 *
44
 * @property string $full_path
45
 *
46
 * @property DedicatedServer $dedicatedServer
47
 */
48
class Server extends Model
49
{
50
    const TIME_EXPIRE_PROCESS_CHECK = 120;
51
52
    // Installed statuses
53
    const NOT_INSTALLED              = 0;
54
    const INSTALLED                  = 1;
55
    const INSTALLATION_PROCESS       = 2;
56
57
    protected $fillable = [
58
        'uuid', 'uuid_short',
59
        'enabled', 'name', 'code_name', 'game_id',
60
        'ds_id', 'game_mod_id', 'expires',
61
        'installed', 'server_ip', 'server_port',
62
        'query_port', 'rcon_port',
63
        'rcon', 'dir', 'su_user',
64
        'cpu_limit', 'ram_limit', 'net_limit',
65
        'start_command', 'stop_command',
66
        'force_stop_command', 'restart_command'
67
    ];
68
69
    /**
70
     * Get server status
71
     *
72
     * @return bool
73
     */
74
    public function processActive()
75
    {
76
        if (empty($this->last_process_check)) {
77
            return false;
78
        }
79
        
80
        $lastProcessCheck = Carbon::createFromFormat('Y-m-d H:i:s' , $this->last_process_check)->timestamp;
81
82
        if ($this->process_active && $lastProcessCheck >= Carbon::now()->timestamp - self::TIME_EXPIRE_PROCESS_CHECK) {
83
            return true;
84
        }
85
86
        return false;
87
    }
88
89
    public function dedicatedServer()
90
    {
91
        return $this->belongsTo(DedicatedServer::class, 'ds_id');
92
    }
93
94
    public function game()
95
    {
96
        return $this->belongsTo(Game::class, 'game_id', 'code');
97
    }
98
99
    public function gameMod()
100
    {
101
        return $this->belongsTo(GameMod::class, 'game_mod_id');
102
    }
103
104
    /**
105
     * One to many relation
106
     *
107
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
108
     */
109
    public function settings()
110
    {
111
        return $this->hasMany(ServerSetting::class);
112
    }
113
114
    /**
115
     * @return string
116
     */
117
    public function getFullPathAttribute()
118
    {
119
        return rtrim($this->dedicatedServer->work_path, '/') . '/' . ltrim($this->dir, '/');
120
    }
121
122
    /**
123
     * @return array
124
     */
125
    public function getFileManagerDisksAttribute()
126
    {
127
        $fileManagerDisks = [
128
            "server" => array_merge(
129
                $this->dedicatedServer->gdaemonSettings('local'),
130
                ['driver' => 'gameap', 'workDir' => $this->full_path, 'root' => $this->full_path]
131
            )
132
        ];
133
134
        $setting = $this->settings()->where('name', 'file-manager')->first();
135
136
        if (!empty($setting)) {
137
            $disks = json_decode($setting->value, true);
138
139
            if (!empty($disks)) {
140
                $fileManagerDisks = array_merge($fileManagerDisks, $disks);
141
            }
142
        }
143
144
145
        return $fileManagerDisks;
146
    }
147
148
    public function users()
149
    {
150
        return $this->belongsToMany(User::class);
151
    }
152
153
    public function getAliasesAttribute()
154
    {
155
        $aliases = [
156
            'server_ip' => $this->server_ip,
157
            'server_port' => $this->server_port,
158
            'query_port' => $this->query_port,
159
            'rcon_port' => $this->rcon_port,
160
            'rcon_password' => $this->rcon,
161
            'uuid' => $this->uuid,
162
            'uuid_short' => $this->uuid_short,
163
        ];
164
165
        foreach ($this->gameMod->vars as $var) {
0 ignored issues
show
Bug introduced by
The expression $this->gameMod->vars of type string is not traversable.
Loading history...
166
            $varname = $var['var'];
167
            $aliases[ $varname ] = isset($this->vars[$varname])
168
                ? $this->vars[$varname]
169
                : $var['default'];
170
        }
171
172
        return $aliases;
173
    }
174
}