GameMod::castAttribute()   A
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 6
c 1
b 1
f 0
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.2222
cc 6
nc 3
nop 2
crap 6
1
<?php
2
3
namespace Gameap\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
/**
8
 * Class GameMod
9
 * @package Gameap\Models
10
 *
11
 * @property integer $id
12
 * @property string $game_code
13
 * @property string $name
14
 * @property array $fast_rcon
15
 * @property array $vars
16
 * @property string $remote_repository_linux
17
 * @property string $remote_repository_windows
18
 * @property string $local_repository_linux
19
 * @property string $local_repository_windows
20
 * @property string $start_cmd_linux
21
 * @property string $start_cmd_windows
22
 * @property string $kick_cmd
23
 * @property string $ban_cmd
24
 * @property string $chname_cmd
25
 * @property string $srestart_cmd
26
 * @property string $chmap_cmd
27
 * @property string $sendmsg_cmd
28
 * @property string $passwd_cmd
29
 */
30
class GameMod extends Model
31
{
32
    /**
33
     * @var bool
34
     */
35
    public $timestamps = false;
36
37
    /**
38
     * The attributes that are mass assignable.
39
     *
40
     * @var array
41
     */
42
    protected $fillable = [
43
        'name', 'game_code',
44
        'fast_rcon', 'vars',
45
        'remote_repository_linux', 'remote_repository_windows',
46
        'local_repository_linux', 'local_repository_windows',
47
        'start_cmd_linux', 'start_cmd_windows',
48
        'kick_cmd', 'ban_cmd', 'chname_cmd', 'srestart_cmd', 'chmap_cmd', 'sendmsg_cmd', 'passwd_cmd',
49
    ];
50
51
    /**
52
     * Validation rules
53
     *
54
     * @var array
55
     */
56
    protected static $rules = [
57
        'name'      => 'required|string|max:255',
58
        'game_code' => 'sometimes|string|max:255|exists:games,code',
59
60
        'start_cmd_linux'   => 'nullable|string|max:1000',
61
        'start_cmd_windows' => 'nullable|string|max:1000',
62
63
        'vars.*.var'       => 'max:16',
64
        'vars.*.default'   => 'max:64',
65
        'vars.*.info'      => 'max:128',
66
        'vars.*.admin_var' => 'max:128',
67
68
        'fast_rcon.*.info'    => 'max:32',
69
        'fast_rcon.*.command' => 'max:128',
70
    ];
71
72
    /**
73
     * @var array
74
     */
75
    protected $casts = [
76
        'vars'      => 'array',
77
        'fast_rcon' => 'array',
78
    ];
79
80
    /**
81
     * One to one relation
82
     *
83
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
84 30
     */
85
    public function game()
86 30
    {
87
        return $this->belongsTo(Game::class, 'game_code', 'code');
88
    }
89
90
    /**
91
     * One to many relation
92
     *
93
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
94 3
     */
95
    public function servers()
96 3
    {
97
        return $this->hasMany(Server::class);
98
    }
99
100
    /**
101
     * Cast an attribute to a native PHP type.
102
     *
103
     * @param  string  $key
104
     * @param  mixed  $value
105
     * @return mixed
106 33
     */
107
    protected function castAttribute($key, $value)
108 33
    {
109 3
        if ($this->getCastType($key) == 'array' && (is_null($value) || empty($value))) {
110
            return [];
111
        }
112 33
113
        $castValue = parent::castAttribute($key, $value);
114 33
115 3
        if ($this->getCastType($key) == 'array' && !is_array($castValue)) {
116
            return [];
117
        }
118 33
119
        return $castValue;
120
    }
121
}
122