Total Complexity | 24 |
Total Lines | 200 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
11 | class ApplicationValidator |
||
12 | { |
||
13 | |||
14 | public function validator(JobApplication $application) |
||
2 ignored issues
–
show
|
|||
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
|
|||
46 | { |
||
47 | $this->validator($application)->validate(); |
||
48 | } |
||
49 | |||
50 | public function validateComplete(JobApplication $application): bool |
||
1 ignored issue
–
show
|
|||
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. |
||
60 | * @return array |
||
61 | */ |
||
62 | protected function arrayMapKeys($fn, $array): array |
||
2 ignored issues
–
show
|
|||
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
|
|||
72 | { |
||
73 | // prepend the attribute name of each validator rule with the nested attribute name |
||
1 ignored issue
–
show
|
|||
74 | $newRules = $this->arrayMapKeys( |
||
75 | function ($key) use ($nestedAttribute) { |
||
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
|
|||
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
|
|||
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
|
|||
121 | { |
||
122 | $validator = $this->basicsValidator($application); |
||
123 | return $validator->passes(); |
||
124 | } |
||
125 | |||
126 | public $experienceRules = ['experience_saved' => 'required|boolean|accepted']; |
||
1 ignored issue
–
show
|
|||
127 | public function experienceValidator(JobApplication $application) |
||
2 ignored issues
–
show
|
|||
128 | { |
||
129 | return Validator::make($application->attributesToArray(), $this->experienceRules); |
||
130 | } |
||
131 | |||
132 | public function experienceComplete(JobApplication $application) |
||
2 ignored issues
–
show
|
|||
133 | { |
||
134 | return $this->experienceValidator($application)->passes(); |
||
135 | } |
||
136 | |||
137 | protected function skillsValidator(JobApplication $application, $criteria_type) |
||
2 ignored issues
–
show
|
|||
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
|
|||
172 | { |
||
173 | return $this->skillsValidator($application, 'essential'); |
||
174 | } |
||
175 | |||
176 | public function essentialSkillsComplete(JobApplication $application) |
||
2 ignored issues
–
show
|
|||
177 | { |
||
178 | return $this->essentialSkillsValidator($application)->passes(); |
||
179 | } |
||
180 | |||
181 | public function assetSkillsValidator(JobApplication $application) |
||
2 ignored issues
–
show
|
|||
182 | { |
||
183 | return $this->skillsValidator($application, 'asset'); |
||
184 | } |
||
185 | |||
186 | public function assetSkillsComplete(JobApplication $application) |
||
189 | } |
||
190 | |||
191 | public $affirmationRules = [ |
||
1 ignored issue
–
show
|
|||
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) |
||
206 | } |
||
207 | |||
208 | public function affirmationComplete(JobApplication $application) |
||
211 | } |
||
212 | } |
||
213 |