Passed
Push — task/laravel-breadcrumbs ( 3beccb...a96280 )
by Yonathan
10:46 queued 10s
created

ApplicationValidator::skillsValidator()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 32
rs 8.9777
c 0
b 0
f 0
cc 6
nc 24
nop 2
1
<?php
2
3
namespace App\Services\Validation;
4
5
use App\Models\JobApplication;
6
use Illuminate\Support\Facades\Validator;
7
use App\Models\Lookup\CriteriaType;
8
use App\Services\Validation\Rules\ContainsObjectWithAttributeRule;
9
use App\Services\Validation\JobApplicationAnswerValidator;
10
11
class ApplicationValidator
12
{
13
14
    public function validator(JobApplication $application)
2 ignored issues
show
introduced by
Method \App\Services\Validation\ApplicationValidator::validator() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function validator()
Loading history...
15
    {
16
        $backendRules = [
17
            'job_poster_id' => 'required',
18
            'application_status_id' => 'required',
19
            'applicant_id' => 'required',
20
        ];
21
        $data = $application->toArray();
22
23
        $rules = array_merge(
24
            $backendRules,
25
            $this->experienceRules,
26
            $this->affirmationRules
27
        );
28
29
        // Combining and simplifiying error messages
30
        $rules = array_merge(
31
            $rules,
32
            ['application_step_1' => 'required|boolean|accepted'],
33
            ['application_step_3' => 'required|boolean|accepted']
34
        );
35
        $data = array_merge(
36
            $data,
37
            ['application_step_1' => $this->basicsComplete($application)],
38
            ['application_step_3' => $this->essentialSkillsComplete($application)]
39
        );
40
41
        // Validate basic data is filled in
42
        return Validator::make($data, $rules);
43
    }
44
45
    public function validate(JobApplication $application)
1 ignored issue
show
introduced by
Method \App\Services\Validation\ApplicationValidator::validate() does not have void return type hint.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function validate()
Loading history...
46
    {
47
        $this->validator($application)->validate();
48
    }
49
50
    public function validateComplete(JobApplication $application): bool
1 ignored issue
show
Coding Style Documentation introduced by
Missing doc comment for function validateComplete()
Loading history...
51
    {
52
        return $this->validator($application)->passes();
53
    }
54
55
    /**
56
     * Return a copy of $array, with function $fn applied to each key, but values left unchanged.
57
     *
58
     * @param function $fn    Function applied to each key.
59
     * @param array    $array Array to operate on.
0 ignored issues
show
introduced by
@param annotation of method \App\Services\Validation\ApplicationValidator::arrayMapKeys() does not specify type hint for items of its traversable parameter $array.
Loading history...
60
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \App\Services\Validation\ApplicationValidator::arrayMapKeys() does not specify type hint for items of its traversable return value.
Loading history...
61
     */
62
    protected function arrayMapKeys($fn, $array): array
2 ignored issues
show
introduced by
Method \App\Services\Validation\ApplicationValidator::arrayMapKeys() does not have native type hint for its parameter $fn but it should be possible to add it based on @param annotation "function".
Loading history...
introduced by
Method \App\Services\Validation\ApplicationValidator::arrayMapKeys() does not have native type hint for its parameter $array but it should be possible to add it based on @param annotation "array".
Loading history...
Coding Style introduced by
Type hint "function" missing for $fn
Loading history...
Coding Style introduced by
Type hint "array" missing for $array
Loading history...
63
    {
64
        $newArray = [];
65
        foreach ($array as $key => $value) {
66
            $newArray[$fn($key)] = $value;
67
        }
68
        return $newArray;
69
    }
70
71
    protected function addNestedValidatorRules($nestedAttribute, $validatorRules, $rules = [])
2 ignored issues
show
introduced by
Method \App\Services\Validation\ApplicationValidator::addNestedValidatorRules() does not have parameter type hint nor @param annotation for its parameter $nestedAttribute.
Loading history...
introduced by
Method \App\Services\Validation\ApplicationValidator::addNestedValidatorRules() does not have parameter type hint nor @param annotation for its parameter $validatorRules.
Loading history...
introduced by
Method \App\Services\Validation\ApplicationValidator::addNestedValidatorRules() does not have parameter type hint nor @param annotation for its parameter $rules.
Loading history...
introduced by
Method \App\Services\Validation\ApplicationValidator::addNestedValidatorRules() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function addNestedValidatorRules()
Loading history...
72
    {
73
        // prepend the attribute name of each validator rule with the nested attribute name
1 ignored issue
show
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
74
        $newRules = $this->arrayMapKeys(
75
            function ($key) use ($nestedAttribute) {
0 ignored issues
show
Bug introduced by
function(...) { /* ... */ } of type callable is incompatible with the type App\Services\Validation\function expected by parameter $fn of App\Services\Validation\...lidator::arrayMapKeys(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
            /** @scrutinizer ignore-type */ function ($key) use ($nestedAttribute) {
Loading history...
76
                return implode('.', [$nestedAttribute, $key]);
77
            },
78
            $validatorRules
79
        );
80
        // Merge new rules with old rules
81
        $rules = array_merge($rules, $newRules);
82
        return $rules;
83
    }
84
85
    public function basicRules(JobApplication $application)
2 ignored issues
show
introduced by
Method \App\Services\Validation\ApplicationValidator::basicRules() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function basicRules()
Loading history...
86
    {
87
        // Validate the fields common to every application
88
        $rules = [
89
            'language_requirement_confirmed' => ['required', 'boolean'],
90
            'citizenship_declaration_id' => ['required', 'exists:citizenship_declarations,id'],
91
            'veteran_status_id' => ['required', 'exists:veteran_statuses,id'],
92
            'preferred_language_id' => ['required', 'exists:preferred_languages,id'],
93
        ];
94
95
        // Merge with Answer rules, that ensure each answer is complete
96
        $answerValidator = new JobApplicationAnswerValidator($application);
97
        $rules = $this->addNestedValidatorRules(
98
            'job_application_answers.*',
99
            $answerValidator->rules(),
100
            $rules
101
        );
102
103
        // Validate that each question has been answered
104
        $jobPosterQuestionRules = [];
105
        foreach ($application->job_poster->job_poster_questions as $question) {
106
            $jobPosterQuestionRules[] = new ContainsObjectWithAttributeRule('job_poster_question_id', $question->id);
107
        }
108
        $rules['job_application_answers'] = $jobPosterQuestionRules;
109
110
        return $rules;
111
    }
112
    public function basicsValidator(JobApplication $application)
2 ignored issues
show
introduced by
Method \App\Services\Validation\ApplicationValidator::basicsValidator() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function basicsValidator()
Loading history...
113
    {
114
        // Load application answers so they are included in application->toArray().
115
        $application->load('job_application_answers');
116
        $validator = Validator::make($application->toArray(), $this->basicRules($application));
117
        return $validator;
118
    }
119
120
    public function basicsComplete(JobApplication $application)
2 ignored issues
show
introduced by
Method \App\Services\Validation\ApplicationValidator::basicsComplete() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function basicsComplete()
Loading history...
121
    {
122
        $validator = $this->basicsValidator($application);
123
        return $validator->passes();
124
    }
125
126
    public $experienceRules = ['experience_saved' => 'required|boolean|accepted'];
1 ignored issue
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
127
    public function experienceValidator(JobApplication $application)
2 ignored issues
show
introduced by
Method \App\Services\Validation\ApplicationValidator::experienceValidator() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function experienceValidator()
Loading history...
128
    {
129
        return Validator::make($application->attributesToArray(), $this->experienceRules);
130
    }
131
132
    public function experienceComplete(JobApplication $application)
2 ignored issues
show
introduced by
Method \App\Services\Validation\ApplicationValidator::experienceComplete() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function experienceComplete()
Loading history...
133
    {
134
        return $this->experienceValidator($application)->passes();
135
    }
136
137
    protected function skillsValidator(JobApplication $application, $criteria_type)
2 ignored issues
show
introduced by
Method \App\Services\Validation\ApplicationValidator::skillsValidator() does not have parameter type hint nor @param annotation for its parameter $criteria_type.
Loading history...
introduced by
Method \App\Services\Validation\ApplicationValidator::skillsValidator() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function skillsValidator()
Loading history...
138
    {
139
        $rules = [];
140
141
        // If application is still a draft, check skills attached to applicant profile. If submitted, use application itself.
142
        $skillDeclarationsAttribute = $application->isDraft() ? 'applicant.skill_declarations' : 'skill_declarations';
143
        $application->load($skillDeclarationsAttribute);
144
        $skillDeclarations = $application->isDraft()
145
            ? $application->applicant->skill_declarations
146
            : $application->skill_declarations;
147
148
        $skillDeclarationRules = [];
149
        $criteriaTypeId = CriteriaType::where('name', $criteria_type)->firstOrFail()->id;
150
        foreach ($application->job_poster->criteria->where('criteria_type_id', $criteriaTypeId) as $criteria) {
151
            // Validate that every essential skill has a corresponding declaration
152
            $skillDeclarationRules[] = new ContainsObjectWithAttributeRule('skill_id', $criteria->skill_id);
153
        }
154
        $rules[$skillDeclarationsAttribute] = $skillDeclarationRules;
155
156
        // Validate that those declarations are complete
157
        $skillDeclarationValidatorFactory = new SkillDeclarationValidator();
158
        $relevantSkillIds = $application->job_poster->criteria->where('criteria_type_id', $criteriaTypeId)->pluck('skill_id');
159
        foreach ($skillDeclarations as $key => $declaration) {
160
            if ($relevantSkillIds->contains($declaration->skill_id)) {
161
                $attribute = implode('.', [$skillDeclarationsAttribute, $key]);
162
                $skillDeclarationValidator = $skillDeclarationValidatorFactory->validator($declaration);
163
                $rules = $this->addNestedValidatorRules($attribute, $skillDeclarationValidator->getRules(), $rules);
164
            }
165
        }
166
167
        $validator = Validator::make($application->toArray(), $rules);
168
        return $validator;
169
    }
170
171
    public function essentialSkillsValidator(JobApplication $application)
2 ignored issues
show
introduced by
Method \App\Services\Validation\ApplicationValidator::essentialSkillsValidator() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function essentialSkillsValidator()
Loading history...
172
    {
173
        return $this->skillsValidator($application, 'essential');
174
    }
175
176
    public function essentialSkillsComplete(JobApplication $application)
2 ignored issues
show
introduced by
Method \App\Services\Validation\ApplicationValidator::essentialSkillsComplete() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function essentialSkillsComplete()
Loading history...
177
    {
178
        return $this->essentialSkillsValidator($application)->passes();
179
    }
180
181
    public function assetSkillsValidator(JobApplication $application)
2 ignored issues
show
introduced by
Method \App\Services\Validation\ApplicationValidator::assetSkillsValidator() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function assetSkillsValidator()
Loading history...
182
    {
183
        return $this->skillsValidator($application, 'asset');
184
    }
185
186
    public function assetSkillsComplete(JobApplication $application)
2 ignored issues
show
introduced by
Method \App\Services\Validation\ApplicationValidator::assetSkillsComplete() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function assetSkillsComplete()
Loading history...
187
    {
188
        return $this->assetSkillsValidator($application)->passes();
189
    }
190
191
    public $affirmationRules = [
1 ignored issue
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
192
        'submission_signature' => [
193
            'required',
194
            'string',
195
            'max:191',
196
        ],
197
        'submission_date' => [
198
            'required',
199
            'string',
200
            'max:191',
201
        ]
202
    ];
203
    public function affirmationValidator(JobApplication $application)
2 ignored issues
show
introduced by
Method \App\Services\Validation\ApplicationValidator::affirmationValidator() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function affirmationValidator()
Loading history...
204
    {
205
        return Validator::make($application->toArray(), $this->affirmationRules);
206
    }
207
208
    public function affirmationComplete(JobApplication $application)
2 ignored issues
show
introduced by
Method \App\Services\Validation\ApplicationValidator::affirmationComplete() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function affirmationComplete()
Loading history...
209
    {
210
        return $this->affirmationValidator($application)->passes();
211
    }
212
}
213