Completed
Push — master ( 1bcff9...3c7575 )
by ARCANEDEV
04:01
created

UserFormRequest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 110
ccs 0
cts 46
cp 0
rs 10
c 1
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 8 1
A sanitize() 0 6 1
A attributes() 0 6 1
A sanitizeUsername() 0 8 2
A getEmailRule() 0 4 1
A getUsernameRule() 0 4 1
A getUsersTable() 0 4 1
A getRolesRule() 0 8 1
1
<?php namespace Arcanesoft\Auth\Http\Requests\Admin\Users;
2
3
use Arcanesoft\Auth\Http\Requests\FormRequest;
4
use Arcanesoft\Contracts\Auth\Models\Role as RoleContract;
5
use Cache;
6
use Illuminate\Support\Str;
7
use Illuminate\Validation\Rule;
8
9
/**
10
 * Class     UserFormRequest
11
 *
12
 * @package  Arcanesoft\Auth\Http\Requests\Admin\Users
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
abstract class UserFormRequest extends FormRequest
16
{
17
    /* -----------------------------------------------------------------
18
     |  Main Methods
19
     | -----------------------------------------------------------------
20
     */
21
    /**
22
     * Get the validation rules that apply to the request.
23
     *
24
     * @return array
25
     */
26
    public function rules()
27
    {
28
        return [
29
            'first_name' => ['required', 'min:2'],
30
            'last_name'  => ['required', 'min:2'],
31
            'roles'      => $this->getRolesRule(),
32
        ];
33
    }
34
35
    /**
36
     * Sanitize all inputs.
37
     *
38
     * @return array
39
     */
40
    protected function sanitize()
41
    {
42
        return [
43
            'username' => $this->sanitizeUsername(),
44
        ];
45
    }
46
47
    /**
48
     * Get custom attributes for validator errors.
49
     *
50
     * @return array
51
     */
52
    public function attributes()
53
    {
54
        return [
55
            'roles' => trans('auth::roles.titles.roles')
56
        ];
57
    }
58
59
    /* -----------------------------------------------------------------
60
     |  Other Methods
61
     | -----------------------------------------------------------------
62
     */
63
    /**
64
     * Sanitize the username.
65
     *
66
     * @return string
67
     */
68
    protected function sanitizeUsername()
69
    {
70
        $username = $this->has('username')
71
            ? $this->get('username')
72
            : Str::limit($this->get('first_name'), 1, '.').' '.$this->get('last_name');
73
74
        return Str::slug($username, config('arcanesoft.auth.slug-separator', '.'));
75
    }
76
77
    /**
78
     * Get the email rule.
79
     *
80
     * @param  string  $column
81
     *
82
     * @return \Illuminate\Validation\Rules\Unique
83
     */
84
    protected function getEmailRule($column = 'email')
85
    {
86
        return Rule::unique($this->getUsersTable(), $column);
87
    }
88
89
    /**
90
     * Get the username rule.
91
     *
92
     * @param  string  $column
93
     *
94
     * @return \Illuminate\Validation\Rules\Unique
95
     */
96
    protected function getUsernameRule($column = 'username')
97
    {
98
        return Rule::unique($this->getUsersTable(), $column);
99
    }
100
101
    /**
102
     * Get the users table name.
103
     *
104
     * @return string
105
     */
106
    protected function getUsersTable()
107
    {
108
        return $this->getPrefixTable().config('arcanesoft.auth.users.table', 'users');
109
    }
110
111
    /**
112
     * Get the roles rule.
113
     *
114
     * @return string
115
     */
116
    protected function getRolesRule()
117
    {
118
        $rolesIds = Cache::remember('auth.roles.ids', 1, function () {
119
            return app(RoleContract::class)->pluck('id');
120
        });
121
122
        return ['required', 'array', 'min:1', 'in:'.$rolesIds->implode(',')];
123
    }
124
}
125