Passed
Push — develop ( 7d6347...bbcebd )
by Nikita
06:10
created

Server::settings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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($this->last_process_check)->timestamp;
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

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