Passed
Push — feature/delete_items ( abdbb3...c1e89b )
by Tristan
09:18
created

BulkSkillDeclarationValidator::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace App\Services\Validation;
4
5
use Illuminate\Support\Facades\Validator;
6
use Illuminate\Validation\Validator as BaseValidator;
7
use Illuminate\Validation\Rule;
8
use App\Models\Lookup\SkillLevel;
9
use App\Models\Skill;
10
use App\Models\Applicant;
11
use App\Services\Validation\Rules\UniqueApplicantSkillRule;
12
use App\Services\Validation\Rules\ApplicantHasRelationRule;
13
14
class BulkSkillDeclarationValidator {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class BulkSkillDeclarationValidator
Loading history...
Coding Style introduced by
Opening brace of a class must be on the line after the definition
Loading history...
15
16
    protected $applicant;
17
    protected $data;
18
    protected $rules = [];
19
    protected $messages = [];
20
    protected $customAttributes = [];
21
22
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $applicant should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $data should have a doc-comment as per coding-style.
Loading history...
23
     * Create a new validator instance.
24
     *
25
     * @param  App\Models\Applicant  $user
0 ignored issues
show
Bug introduced by
The type App\Services\Validation\App\Models\Applicant was not found. Did you mean App\Models\Applicant? If so, make sure to prefix the type with \.
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Doc comment for parameter $user does not match actual variable name $applicant
Loading history...
26
     * @return void
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
27
     */
28
    public function __construct(Applicant $applicant, $data)
29
    {
30
        $this->applicant = $applicant;
31
        $this->data = $data;
32
        $this->initializeRules($data, $applicant);
33
    }
34
35
    protected function initializeRules($data, $applicant) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function initializeRules()
Loading history...
36
        $rules = [];
37
        $customAttributes = [];
38
39
        $ages = ['new', 'old'];
40
        $types = ['soft', 'hard'];
41
42
        $skill_level_ids = SkillLevel::all()->pluck('id');
43
        $skill_ids = Skill::all()->pluck('id');
44
        $uniqueSkillRule = new UniqueApplicantSkillRule($applicant);
45
        $applicantHasSkillRule = new ApplicantHasRelationRule($applicant, 'skill_declarations');
46
47
        if (isset($data['skill_declarations'])) {
48
            $skill_data = $data['skill_declarations'];
49
            foreach($ages as $age) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
50
                if (isset($skill_data[$age])) {
51
                    $age_data = $skill_data[$age];
52
53
                    foreach($types as $type) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
54
                        if (isset($age_data[$type])) {
55
                            $type_data = $age_data[$type];
56
57
                            foreach($type_data as $id=>$declaration) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
58
59
                                //add description validation rule
60
                                $description_field = implode('.', ['skill_declarations', $age, $type,$id,'description']);
61
                                $description_rule = 'required|string';
62
                                $rules[$description_field] = $description_rule;
63
                                $customAttributes[$description_field] = 'description';
64
65
                                //add skill_level validation rule
66
                                $skill_level_field = implode('.', ['skill_declarations', $age, $type,$id,'skill_level_id']);
67
                                $skill_level_rule = [
68
                                    'required',
69
                                    Rule::in($skill_level_ids->toArray()),
70
                                ];
71
                                $rules[$skill_level_field] = $skill_level_rule;
72
                                $customAttributes[$skill_level_field] = 'skill level';
73
74
                                //If age is new, add skill_id validation rule
75
                                if ($age == 'new') {
76
                                    $skill_id_field = implode('.', ['skill_declarations', $age, $type,$id,'skill_id']);
77
                                    $skill_id_rule = [
78
                                        'required',
79
                                        Rule::in($skill_ids->toArray()),
80
                                        $uniqueSkillRule,
81
                                    ];
82
                                    $rules[$skill_id_field] = $skill_id_rule;
83
                                    $customAttributes[$skill_id_field] = 'skill id';
84
                                }
85
                                //If age is old, add skill_declaration_id validation rule
86
                                else if ($age == 'old') {
0 ignored issues
show
Coding Style introduced by
Expected "} else if (...) \n"; found "\n //If age is old, add skill_declaration_id validation rule\n else if (...) {\n"
Loading history...
87
                                    $skill_declaration_field = implode('.', ['skill_declarations', $age, $type,$id,'skill_declaration_id']);
88
                                    $skill_declaration_rule = [
89
                                        'required',
90
                                        $applicantHasSkillRule,
91
                                    ];
92
                                    $rules[$skill_declaration_field] = $skill_declaration_rule;
93
                                    $customAttributes[$skill_declaration_field] = 'skill declaration id';
94
                                }
95
                            }
96
                        }
97
                    }
98
                }
99
            }
100
        }
101
102
        $this->rules = $rules;
103
        $this->customAttributes = $customAttributes;
104
    }
105
106
    public function getRules() {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getRules()
Loading history...
107
        return $this->rules;
108
    }
109
110
    public function validate($data) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function validate()
Loading history...
111
        Validator::make($data, $this->getRules(), $this->messages, $this->customAttributes)->validate();
112
    }
113
}
114