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

UserFormRequest::sanitize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
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