DedicatedServer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 5
eloc 79
dl 0
loc 108
ccs 18
cts 19
cp 0.9474
rs 10
c 1
b 1
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A clientCertificate() 0 3 1
A gdaemonSettings() 0 30 2
A servers() 0 3 1
A isLinux() 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\HasMany;
8
use Illuminate\Database\Eloquent\SoftDeletes;
9
use Illuminate\Support\Facades\Storage;
10
11
/**
12
 * Class DedicatedServer
13
 *
14
 * @property int $id
15
 * @property boolean $enabled
16
 * @property string $name
17
 * @property string $os
18
 * @property string $location
19
 * @property string $provider
20
 * @property string[] $ip
21
 * @property string $ram
22
 * @property string $cpu
23
 * @property string $work_path
24
 * @property string $steamcmd_path
25
 * @property string $gdaemon_host
26
 * @property integer $gdaemon_port
27
 * @property string $gdaemon_api_key
28
 * @property string $gdaemon_api_token
29
 * @property string $gdaemon_login
30
 * @property string $gdaemon_password
31
 * @property string $gdaemon_server_cert
32
 * @property integer $client_certificate_id
33
 * @property string $prefer_install_method
34
 * @property string $script_install
35
 * @property string $script_reinstall
36
 * @property string $script_update
37
 * @property string $script_start
38
 * @property string $script_pause
39
 * @property string $script_unpause
40
 * @property string $script_stop
41
 * @property string $script_kill
42
 * @property string $script_restart
43
 * @property string $script_status
44
 * @property string $script_stats
45
 * @property string $script_get_console
46
 * @property string $script_send_command
47
 * @property string $script_delete
48
 * @property string $created_at
49
 * @property string $updated_at
50
 *
51
 * @property Server[] $servers
52
 * @property ClientCertificate $clientCertificate
53
 */
54
class DedicatedServer extends Model
55
{
56
    use SoftDeletes;
57
58
    public const LINUX_DISTRIBUTIONS = ['linux', 'debian', 'ubuntu', 'centos', 'gentoo', 'opensuse'];
59
60
    protected $fillable = [
61
        'enabled',
62
        'name',
63
        'os',
64
        'location',
65
        'provider',
66
        'ip',
67
        'ram',
68
        'cpu',
69
        'work_path',
70
        'steamcmd_path',
71
        'gdaemon_host',
72
        'gdaemon_port',
73
        'gdaemon_login',
74
        'gdaemon_password',
75
        'gdaemon_server_cert',
76
        'gdaemon_api_key',
77
        'client_certificate_id',
78
        'prefer_install_method',
79
        'script_install',
80
        'script_reinstall',
81
        'script_update',
82
        'script_start',
83
        'script_pause',
84
        'script_unpause',
85
        'script_stop',
86
        'script_kill',
87
        'script_restart',
88
        'script_status',
89
        'script_stats',
90
        'script_get_console',
91
        'script_send_command',
92
        'script_delete',
93
    ];
94
95
    protected $casts = [
96
        'ip'                    => 'array',
97
        'enabled'               => 'boolean',
98
        'gdaemon_port'          => 'integer',
99
        'client_certificate_id' => 'integer',
100
    ];
101
102
    protected static $rules = [
103
        'name'                  => 'required|max:128',
104
        'location'              => 'required|max:128',
105
        'ip'                    => 'required',
106
        'work_path'             => 'required|max:128',
107
        'gdaemon_host'          => 'required|max:128',
108
        'gdaemon_port'          => 'required|numeric|digits_between:1,65535',
109
        'gdaemon_login'         => 'max:128',
110
        'gdaemon_password'      => 'max:128',
111
        'gdaemon_api_key'       => '',
112
        'gdaemon_server_cert'   => 'sometimes',
113
        'client_certificate_id' => 'numeric|exists:client_certificates,id',
114
    ];
115
116
    public function servers(): HasMany
117
    {
118
        return $this->hasMany(Server::class, 'ds_id');
119
    }
120
121
    public function clientCertificate(): BelongsTo
122
    {
123
        return $this->belongsTo(ClientCertificate::class);
124
    }
125 3
126
    public function gdaemonSettings($storageDisk = 'local'): array
127 3
    {
128
        $gdaemonHost = filter_var($this->gdaemon_host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)
129
            ? '[' . $this->gdaemon_host . ']'
130
            : $this->gdaemon_host;
131
132
        return [
133 9
            'host'     => $gdaemonHost,
134
            'port'     => $this->gdaemon_port,
135 9
            'username' => $this->gdaemon_login,
136
            'password' => $this->gdaemon_password,
137
138
            'serverCertificate' => Storage::disk($storageDisk)
139
                ->getDriver()
140
                ->getAdapter()
141
                ->applyPathPrefix($this->gdaemon_server_cert),
142 6
143
            'localCertificate' => Storage::disk($storageDisk)
144 6
                ->getDriver()
145
                ->getAdapter()
146 6
                ->applyPathPrefix($this->clientCertificate->certificate),
147
148
            'privateKey' => Storage::disk($storageDisk)
149 6
                ->getDriver()
150 6
                ->getAdapter()
151 6
                ->applyPathPrefix($this->clientCertificate->private_key),
152 6
153
            'privateKeyPass' => $this->clientCertificate->private_key_pass,
154 6
            'workDir'        => $this->work_path,
155 6
            'timeout'        => 1,
156 6
        ];
157 6
    }
158
159 6
    public function isLinux(): bool
160 6
    {
161 6
        return in_array(strtolower($this->os), self::LINUX_DISTRIBUTIONS);
162 6
    }
163
}
164