Test Failed
Push — develop ( af7e45...e70623 )
by Nikita
08:39
created

Server::getFileManagerDisksAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 0
dl 0
loc 21
rs 9.9332
c 0
b 0
f 0
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
    protected $fillable = [
53
        'uuid', 'uuid_short',
54
        'enabled', 'name', 'code_name', 'game_id',
55
        'ds_id', 'game_mod_id', 'expires',
56
        'installed', 'server_ip', 'server_port',
57
        'query_port', 'rcon_port',
58
        'rcon', 'dir', 'su_user',
59
        'cpu_limit', 'ram_limit', 'net_limit',
60
        'start_command', 'stop_command',
61
        'force_stop_command', 'restart_command'
62
    ];
63
64
    /**
65
     * Get server status
66
     *
67
     * @return bool
68
     */
69
    public function processActive()
70
    {
71
        if (empty($this->last_process_check)) {
72
            return false;
73
        }
74
        
75
        $lastProcessCheck = Carbon::createFromFormat('Y-m-d H:i:s' , $this->last_process_check)->timestamp;
76
77
        if ($this->process_active && $lastProcessCheck >= Carbon::now()->timestamp - self::TIME_EXPIRE_PROCESS_CHECK) {
78
            return true;
79
        }
80
81
        return false;
82
    }
83
84
    public function dedicatedServer()
85
    {
86
        return $this->belongsTo(DedicatedServer::class, 'ds_id');
87
    }
88
89
    public function game()
90
    {
91
        return $this->belongsTo(Game::class, 'game_id', 'code');
92
    }
93
94
    public function gameMod()
95
    {
96
        return $this->belongsTo(GameMod::class, 'game_mod_id');
97
    }
98
99
    /**
100
     * One to many relation
101
     *
102
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
103
     */
104
    public function settings()
105
    {
106
        return $this->hasMany(ServerSetting::class);
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getFullPathAttribute()
113
    {
114
        return rtrim($this->dedicatedServer->work_path, '/') . '/' . ltrim($this->dir, '/');
115
    }
116
117
    /**
118
     * @return array
119
     */
120
    public function getFileManagerDisksAttribute()
121
    {
122
        $fileManagerDisks = [
123
            "server" => array_merge(
124
                $this->dedicatedServer->gdaemonSettings('local'),
125
                ['driver' => 'gameap', 'workDir' => $this->full_path]
126
            )
127
        ];
128
129
        $setting = $this->settings()->where('name', 'file-manager')->first();
130
131
        if (!empty($setting)) {
132
            $disks = json_decode($setting->value, true);
133
134
            if (!empty($disks)) {
135
                $fileManagerDisks = array_merge($fileManagerDisks, $disks);
136
            }
137
        }
138
139
140
        return $fileManagerDisks;
141
    }
142
}