Completed
Push — master ( 5c2719...0841d5 )
by
unknown
03:14
created

LoginRequest::rules()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 15
nc 2
nop 0
1
<?php
2
3
namespace App\Containers\Authentication\UI\API\Requests;
4
5
use App\Ship\Parents\Requests\Request;
6
7
/**
8
 * Class LoginRequest.
9
 *
10
 * @author Mahmoud Zalt <[email protected]>
11
 */
12
class LoginRequest extends Request
13
{
14
15
    /**
16
     * Define which Roles and/or Permissions has access to this request.
17
     *
18
     * @var  array
19
     */
20
    protected $access = [
21
        'permissions' => null,
22
        'roles' => null,
23
    ];
24
25
    /**
26
     * Id's that needs decoding before applying the validation rules.
27
     *
28
     * @var  array
29
     */
30
    protected $decode = [
31
32
    ];
33
34
    /**
35
     * Defining the URL parameters (`/stores/999/items`) allows applying
36
     * validation rules on them and allows accessing them like request data.
37
     *
38
     * @var  array
39
     */
40
    protected $urlParameters = [
41
42
    ];
43
44
    /**
45
     * Get the validation rules that apply to the request.
46
     *
47
     * @return array
48
     */
49
    public function rules()
50
    {
51
        $prefix = config('authentication-container.login.prefix', '');
52
53
        $allowedLoginFields = config('authentication-container.login.allowed_login_attributes', ['email' => []]);
54
55
        $rules = [
56
            'password' => 'required|min:3|max:30',
57
        ];
58
59
        foreach ($allowedLoginFields as $key => $optionalValidators)
60
        {
61
            // build all other login fields together
62
            $allOtherLoginFields = array_except($allowedLoginFields, $key);
63
            $allOtherLoginFields = array_keys($allOtherLoginFields);
64
            $allOtherLoginFields = preg_filter('/^/', $prefix, $allOtherLoginFields);
65
            $allOtherLoginFields = implode(',', $allOtherLoginFields);
66
67
            $validators = implode('|', $optionalValidators);
68
69
            $keyname = $prefix . $key;
70
71
            $rules = array_merge($rules,
72
                [
73
                    $keyname => "required_without_all:{$allOtherLoginFields}|exists:users,{$key}|{$validators}",
74
                ]);
75
        }
76
77
        return $rules;
78
    }
79
80
    /**
81
     * Determine if the user is authorized to make this request.
82
     *
83
     * @return bool
84
     */
85
    public function authorize()
86
    {
87
        return $this->check([
88
            'hasAccess',
89
        ]);
90
    }
91
}
92