SaveServerRequest::rules()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 67
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 49
c 0
b 0
f 0
dl 0
loc 67
rs 9.1127
cc 4
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Gameap\Http\Requests\API;
4
5
use Gameap\Http\Requests\JsonRequest;
6
use Illuminate\Validation\Rule;
7
8
class SaveServerRequest extends JsonRequest
9
{
10
    public function rules(): array
11
    {
12
        $portRules = ['nullable', 'numeric', 'digits_between:1,65535',
13
            Rule::unique('servers', 'server_port')
14
                ->ignore($this->route('server'))
15
                ->where(function ($query) {
16
                    $q = $query
17
                        ->where('ds_id', $this->ds_id)
18
                        ->where('server_ip', $this->server_ip)
19
                        ->whereNull('deleted_at');
20
21
                    if ($this->id) {
22
                        $q->where('id', '!=', $this->id);
23
                    }
24
25
                    return $q;
26
                }),
27
            Rule::unique('servers', 'query_port')
28
                ->ignore($this->route('server'))
29
                ->where(function ($query) {
30
                    $q = $query
31
                        ->where('ds_id', $this->ds_id)
32
                        ->where('server_ip', $this->server_ip)
33
                        ->whereNull('deleted_at');
34
35
                    if ($this->id) {
36
                        $q->where('id', '!=', $this->id);
37
                    }
38
39
                    return $q;
40
                }),
41
            Rule::unique('servers', 'rcon_port')
42
                ->ignore($this->route('server'))
43
                ->where(function ($query) {
44
                    $q = $query
45
                        ->where('ds_id', $this->ds_id)
46
                        ->where('server_ip', $this->server_ip)
47
                        ->whereNull('deleted_at');
48
49
                    if ($this->id) {
50
                        $q->where('id', '!=', $this->id);
51
                    }
52
53
                    return $q;
54
                }),
55
        ];
56
57
        return [
58
            'id'          => '',
59
            'enabled'     => '',
60
            'blocked'     => '',
61
            'installed'   => '',
62
            'name'        => 'required|max:128',
63
            'game_id'     => 'required',
64
            'ds_id'       => 'required',
65
            'game_mod_id' => 'required|exists:game_mods,id',
66
            'server_ip'   => 'required',
67
68
            'server_port' => array_merge(['required'], $portRules),
69
            'query_port'  => array_merge(['nullable'], $portRules),
70
            'rcon_port'   => array_merge(['nullable'], $portRules),
71
72
            'dir' => ['nullable', 'string', Rule::unique('servers', 'dir')
73
                ->ignore($this->route('server'))
74
                ->where(function ($query) {
75
                    return $query->where('ds_id', $this->ds_id)
76
                        ->whereNull('deleted_at');
77
                }), ],
78
        ];
79
    }
80
}
81