Passed
Push — develop ( 99647f...e3783d )
by Nikita
10:44
created

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