Passed
Push — 5.0.0 ( 196a2e...76aff9 )
by Fèvre
05:12
created

UserValidator::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 15
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A UserValidator::createWithProvider() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Models\Validators;
6
7
use Illuminate\Support\Facades\App;
8
use Illuminate\Support\Facades\Validator as FacadeValidator;
9
use Illuminate\Validation\Rule;
10
use Illuminate\Validation\Validator;
11
12
class UserValidator
13
{
14
    /**
15
     * Get the validator for an incoming registration request with a social provider.
16
     *
17
     * @param array $data The data to validate.
18
     *
19
     * @return Validator
20
     */
21
    public static function createWithProvider(array $data): Validator
22
    {
23
        $rules = [
24
            'username' => 'required|alpha_num|min:4|max:20|unique:users',
25
            'email' => 'required|email|max:50|unique:users'
26
        ];
27
28
        return FacadeValidator::make($data, $rules);
29
    }
30
31
    /**
32
     * Get a validator for an incoming update request. (Administration)
33
     *
34
     * @param array $data The data to validate.
35
     * @param int $id The actual user id to ignore the username rule.
36
     *
37
     * @return Validator
38
     */
39
    public static function update(array $data, int $id): Validator
40
    {
41
        $rules = [
42
            'username' => [
43
                'required',
44
                'alpha_num',
45
                'min:4',
46
                'max:20',
47
                Rule::unique('users')->ignore($id)
48
            ],
49
            'email' => [
50
                'required',
51
                'email',
52
                'max:50',
53
                Rule::unique('users')->ignore($id)
54
            ],
55
            'account.first_name' => 'max:100',
56
            'account.last_name' => 'max:100',
57
            'account.facebook' => 'max:50',
58
            'account.twitter' => 'max:50',
59
            'roles' => 'required'
60
        ];
61
62
        return FacadeValidator::make($data, $rules);
63
    }
64
65
    /**
66
     * Get the validator for an incoming email update request.
67
     *
68
     * @param array $data The data to validate.
69
     *
70
     * @return Validator
71
     */
72
    public static function updateEmail(array $data): Validator
73
    {
74
        $rules = [
75
            'email' => 'required|email|max:50|unique:users'
76
        ];
77
78
        return FacadeValidator::make($data, $rules);
79
    }
80
81
    /**
82
     * Get the validator for an incoming password update request.
83
     *
84
     * @param array $data The data to validate.
85
     *
86
     * @return Validator
87
     */
88
    public static function updatePassword(array $data): Validator
89
    {
90
        $rules = [
91
            'oldpassword' => 'required',
92
            'password' => 'required|min:6|confirmed'
93
        ];
94
95
        return FacadeValidator::make($data, $rules);
96
    }
97
98
    /**
99
     * Get the validator for an incoming password create request.
100
     *
101
     * @param array $data The data to validate.
102
     *
103
     * @return Validator
104
     */
105
    public static function createPassword(array $data): Validator
106
    {
107
        $rules = [
108
            'password' => 'required|min:6|confirmed'
109
        ];
110
111
        return FacadeValidator::make($data, $rules);
112
    }
113
}
114