Completed
Push — develop ( 5abd4c...dae2aa )
by Abdelrahman
09:07
created

AccountAttributesRequest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 43
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A authorize() 0 4 1
B rules() 0 25 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Requests\Adminarea;
6
7
use Illuminate\Foundation\Http\FormRequest;
8
9
class AccountAttributesRequest extends FormRequest
10
{
11
    /**
12
     * Determine if the user is authorized to make this request.
13
     *
14
     * @return bool
15
     */
16
    public function authorize(): bool
17
    {
18
        return true;
19
    }
20
21
    /**
22
     * Get the validation rules that apply to the request.
23
     *
24
     * @return array
25
     */
26
    public function rules(): array
27
    {
28
        $user = $this->user($this->get('guard'));
29
30
        // Attach attribute rules
31
        $user->getEntityAttributes()->each(function ($attribute, $attributeSlug) use (&$rules) {
32
            switch ($attribute->type) {
33
                case 'datetime':
34
                    $type = 'date';
35
                    break;
36
                case 'text':
37
                case 'varchar':
38
                    $type = 'string';
39
                    break;
40
                default:
41
                    $type = $attribute->type;
42
                    break;
43
            }
44
45
            $rule = ($attribute->is_required ? 'required|' : 'nullable|').$type;
46
            $rules[$attributeSlug.($attribute->is_collection ? '.*' : '')] = $rule;
47
        });
48
49
        return $rules ?? [];
50
    }
51
}
52