Passed
Push — develop ( d2ded6...15ac65 )
by Nikita
19:39
created

DedicatedServer::servers()   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 Sofa\Eloquence\Validable;
0 ignored issues
show
Bug introduced by
The type Sofa\Eloquence\Validable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Sofa\Eloquence\Contracts\Validable as ValidableContract;
0 ignored issues
show
Bug introduced by
The type Sofa\Eloquence\Contracts\Validable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use 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
    /**
56
     * The attributes that are mass assignable.
57
     *
58
     * @var array
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
    /**
103
     * Validation rules
104
     * @var array
105
     */
106
    protected static $rules = [
107
        'name' => 'required|max:128',
108
        'location' => 'required|max:128',
109
        'ip' => 'required',
110
        'work_path' => 'required|max:128',
111
        'gdaemon_host' => 'required|max:128',
112
        'gdaemon_port' => 'required|numeric|digits_between:1,65535',
113
        'gdaemon_login' => 'max:128',
114
        'gdaemon_password' => 'max:128',
115
        'gdaemon_api_key' => '',
116
        'gdaemon_server_cert' => 'sometimes',
117
        'client_certificate_id' => 'numeric|exists:client_certificates,id',
118
    ];
119
120
    /**
121
     * One to many relation
122
     *
123
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
124
     */
125 3
    public function servers()
126
    {
127 3
        return $this->hasMany(Server::class, 'ds_id');
128
    }
129
130
    /**
131
     * One to one relation
132
     */
133 9
    public function clientCertificate()
134
    {
135 9
        return $this->belongsTo(ClientCertificate::class);
136
    }
137
138
    /**
139
     * @param $storageDisk
140
     * @return array
141
     */
142 6
    public function gdaemonSettings($storageDisk = 'local')
143
    {
144 6
        $gdaemonHost = filter_var($this->gdaemon_host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)
145
            ? '[' . $this->gdaemon_host . ']'
146 6
            : $this->gdaemon_host;
147
148
        return [
149 6
            'host' => $gdaemonHost,
150 6
            'port' => $this->gdaemon_port,
151 6
            'username' => $this->gdaemon_login,
152 6
            'password' => $this->gdaemon_password,
153
154 6
            'serverCertificate' => Storage::disk($storageDisk)
155 6
                ->getDriver()
156 6
                ->getAdapter()
157 6
                ->applyPathPrefix($this->gdaemon_server_cert),
158
159 6
            'localCertificate' => Storage::disk($storageDisk)
160 6
                ->getDriver()
161 6
                ->getAdapter()
162 6
                ->applyPathPrefix($this->clientCertificate->certificate),
163
164 6
            'privateKey' => Storage::disk($storageDisk)
165 6
                ->getDriver()
166 6
                ->getAdapter()
167 6
                ->applyPathPrefix($this->clientCertificate->private_key),
168
169 6
            'privateKeyPass' => $this->clientCertificate->private_key_pass,
170 6
            'workDir' => $this->work_path,
171 6
            'timeout' => 10,
172
        ];
173
    }
174
175
    /**
176
     * @return bool
177
     */
178 3
    public function isLinux()
179
    {
180 3
        switch (strtolower($this->os)) {
181 3
            case 'linux':
182 3
            case 'debian':
183 3
            case 'ubuntu':
184 3
            case 'centos':
185 3
            case 'gentoo':
186 3
            case 'opensuse':
187 3
                return true;
188
        }
189
190 3
        return false;
191
    }
192
}
193