Completed
Push — master ( 720025...0e57e6 )
by ARCANEDEV
04:07
created

UserFormRequest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 68
c 0
b 0
f 0
ccs 0
cts 27
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 8 1
A sanitize() 0 6 1
A sanitizeUsername() 0 8 2
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
8
/**
9
 * Class     UserFormRequest
10
 *
11
 * @package  Arcanesoft\Auth\Http\Requests\Admin\Users
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
abstract class UserFormRequest extends FormRequest
15
{
16
    /* ------------------------------------------------------------------------------------------------
17
     |  Main Functions
18
     | ------------------------------------------------------------------------------------------------
19
     */
20
    /**
21
     * Get the validation rules that apply to the request.
22
     *
23
     * @return array
24
     */
25
    public function rules()
26
    {
27
        return [
28
            'first_name' => 'required|min:2',
29
            'last_name'  => 'required|min:2',
30
            'roles'      => $this->getRolesRule(),
31
        ];
32
    }
33
34
    /**
35
     * Sanitize all inputs.
36
     *
37
     * @param  array  $inputs
38
     *
39
     * @return array
40
     */
41
    public function sanitize(array $inputs)
42
    {
43
        $inputs['username'] = $this->sanitizeUsername($inputs);
44
45
        return $inputs;
46
    }
47
48
    /* ------------------------------------------------------------------------------------------------
49
     |  Other Functions
50
     | ------------------------------------------------------------------------------------------------
51
     */
52
    /**
53
     * Sanitize the username.
54
     *
55
     * @param  array  $inputs
56
     *
57
     * @return string
58
     */
59
    protected function sanitizeUsername(array $inputs)
60
    {
61
        $username = $this->has('username')
62
            ? $inputs['username']
63
            : Str::limit($inputs['first_name'], 1, '.') . ' ' . $inputs['last_name'];
64
65
        return Str::slug($username, config('arcanesoft.auth.slug-separator', '.'));
66
    }
67
68
    /**
69
     * Get the roles rule.
70
     *
71
     * @return string
72
     */
73
    protected function getRolesRule()
74
    {
75
        $rolesIds = Cache::remember('auth.roles.ids', 1, function () {
76
            return app(RoleContract::class)->pluck('id');
77
        });
78
79
        return 'required|array|min:1|in:' . $rolesIds->implode(',');
80
    }
81
}
82