Total Complexity | 57 |
Total Lines | 440 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like JobApplication often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use JobApplication, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
78 | class JobApplication extends BaseModel |
||
79 | { |
||
80 | // Trait for Backpack. |
||
81 | use CrudTrait; |
||
82 | |||
83 | use Notifiable; |
||
84 | |||
85 | protected $dispatchesEvents = [ |
||
86 | 'retrieved' => ApplicationRetrieved::class, |
||
87 | 'saved' => ApplicationSaved::class, |
||
88 | ]; |
||
89 | |||
90 | protected $casts = [ |
||
91 | 'job_poster_id' => 'int', |
||
92 | 'application_status_id' => 'int', |
||
93 | 'citizenship_declaration_id' => 'int', |
||
94 | 'veteran_status_id' => 'int', |
||
95 | 'preferred_language_id' => 'int', |
||
96 | 'applicant_id' => 'int', |
||
97 | 'applicant_snapshot_id' => 'int', |
||
98 | 'submission_signature' => 'string', |
||
99 | 'submission_date' => 'string', |
||
100 | 'experience_saved' => 'boolean', |
||
101 | 'language_requirement_confirmed' => 'boolean', |
||
102 | 'language_test_confirmed' => 'boolean', |
||
103 | 'education_requirement_confirmed' => 'boolean', |
||
104 | 'version_id' => 'int', |
||
105 | 'director_name' => 'string', |
||
106 | 'director_title' => 'string', |
||
107 | 'director_email' => 'string', |
||
108 | 'reference_name' => 'string', |
||
109 | 'reference_title' => 'string', |
||
110 | 'reference_email' => 'string', |
||
111 | 'gov_email' => 'string', |
||
112 | 'physical_office_willing' => 'boolean', |
||
113 | 'security_clearance_id' => 'int', |
||
114 | 'share_with_managers' => 'boolean', |
||
115 | ]; |
||
116 | protected $fillable = [ |
||
117 | 'citizenship_declaration_id', |
||
118 | 'veteran_status_id', |
||
119 | 'preferred_language_id', |
||
120 | 'language_requirement_confirmed', |
||
121 | 'language_test_confirmed', |
||
122 | 'education_requirement_confirmed', |
||
123 | 'veteran_status_id', |
||
124 | 'preferred_language_id', |
||
125 | 'submission_signature', |
||
126 | 'submission_date', |
||
127 | 'experience_saved', |
||
128 | 'director_name', |
||
129 | 'director_title', |
||
130 | 'director_email', |
||
131 | 'reference_name', |
||
132 | 'reference_title', |
||
133 | 'reference_email', |
||
134 | 'gov_email', |
||
135 | 'physical_office_willing', |
||
136 | 'security_clearance_id', |
||
137 | 'share_with_managers', |
||
138 | ]; |
||
139 | |||
140 | /** |
||
141 | * The accessors to append to the model's array/json form. |
||
142 | * |
||
143 | * @var array |
||
144 | */ |
||
145 | protected $appends = ['meets_essential_criteria']; |
||
146 | |||
147 | protected function createApplicantSnapshot($applicant_id) |
||
148 | { |
||
149 | $applicant = Applicant::where('id', $applicant_id)->firstOrFail(); |
||
150 | |||
151 | $snapshot = $applicant->replicate(); |
||
|
|||
152 | } |
||
153 | |||
154 | public function applicant() |
||
155 | { |
||
156 | return $this->belongsTo(\App\Models\Applicant::class); |
||
157 | } |
||
158 | |||
159 | public function applicant_snapshot() //phpcs:ignore |
||
160 | { |
||
161 | return $this->belongsTo(\App\Models\Applicant::class, 'applicant_snapshot_id'); |
||
162 | } |
||
163 | |||
164 | public function application_status() //phpcs:ignore |
||
165 | { |
||
166 | return $this->belongsTo(\App\Models\Lookup\ApplicationStatus::class); |
||
167 | } |
||
168 | |||
169 | public function citizenship_declaration() //phpcs:ignore |
||
170 | { |
||
171 | return $this->belongsTo(\App\Models\Lookup\CitizenshipDeclaration::class); |
||
172 | } |
||
173 | |||
174 | public function veteran_status() //phpcs:ignore |
||
175 | { |
||
176 | return $this->belongsTo(\App\Models\Lookup\VeteranStatus::class); |
||
177 | } |
||
178 | |||
179 | public function preferred_language() //phpcs:ignore |
||
180 | { |
||
181 | return $this->belongsTo(\App\Models\Lookup\PreferredLanguage::class); |
||
182 | } |
||
183 | |||
184 | public function job_poster() //phpcs:ignore |
||
185 | { |
||
186 | return $this->belongsTo(\App\Models\JobPoster::class); |
||
187 | } |
||
188 | |||
189 | public function job_application_answers() //phpcs:ignore |
||
190 | { |
||
191 | return $this->hasMany(\App\Models\JobApplicationAnswer::class); |
||
192 | } |
||
193 | |||
194 | public function skill_declarations() //phpcs:ignore |
||
195 | { |
||
196 | return $this->morphMany(\App\Models\SkillDeclaration::class, 'skillable'); |
||
197 | } |
||
198 | |||
199 | public function application_review() //phpcs:ignore |
||
200 | { |
||
201 | return $this->hasOne(ApplicationReview::class); |
||
202 | } |
||
203 | |||
204 | public function degrees() |
||
205 | { |
||
206 | return $this->morphMany(\App\Models\Degree::class, 'degreeable')->orderBy('end_date', 'desc'); |
||
207 | } |
||
208 | |||
209 | public function courses() |
||
210 | { |
||
211 | return $this->morphMany(\App\Models\Course::class, 'courseable')->orderBy('end_date', 'desc'); |
||
212 | } |
||
213 | |||
214 | public function work_experiences() //phpcs:ignore |
||
217 | } |
||
218 | |||
219 | public function references() |
||
220 | { |
||
221 | return $this->morphMany(\App\Models\Reference::class, 'referenceable'); |
||
222 | } |
||
223 | |||
224 | public function projects() |
||
227 | } |
||
228 | |||
229 | public function work_samples() //phpcs:ignore |
||
230 | { |
||
231 | return $this->morphMany(\App\Models\WorkSample::class, 'work_sampleable'); |
||
232 | } |
||
233 | |||
234 | public function job_application_version() //phpcs:ignore |
||
235 | { |
||
236 | return $this->hasOne(\App\Models\JobApplicationVersion::class); |
||
237 | } |
||
238 | |||
239 | public function security_clearance() //phpcs:ignore |
||
240 | { |
||
241 | return $this->belongsTo(\App\Models\Lookup\SecurityClearance::class); |
||
242 | } |
||
243 | |||
244 | // Version 2 application models. |
||
245 | public function experiences_work() //phpcs:ignore |
||
246 | { |
||
247 | return $this->morphMany(\App\Models\ExperienceWork::class, 'experienceable') |
||
248 | ->orderBy('end_date', 'desc'); |
||
249 | } |
||
250 | |||
251 | public function experiences_personal() //phpcs:ignore |
||
252 | { |
||
253 | return $this->morphMany(\App\Models\ExperiencePersonal::class, 'experienceable') |
||
254 | ->orderBy('end_date', 'desc'); |
||
255 | } |
||
256 | |||
257 | public function experiences_education() //phpcs:ignore |
||
258 | { |
||
259 | return $this->morphMany(\App\Models\ExperienceEducation::class, 'experienceable') |
||
260 | ->orderBy('end_date', 'desc'); |
||
261 | } |
||
262 | |||
263 | public function experiences_award() //phpcs:ignore |
||
264 | { |
||
265 | return $this->morphMany(\App\Models\ExperienceAward::class, 'experienceable'); |
||
266 | } |
||
267 | |||
268 | public function experiences_community() //phpcs:ignore |
||
269 | { |
||
270 | return $this->morphMany(\App\Models\ExperienceCommunity::class, 'experienceable') |
||
271 | ->orderBy('end_date', 'desc'); |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Return either 'complete', 'incomplete' or 'error', depending on the |
||
276 | * status of the requested section. |
||
277 | * |
||
278 | * @param string $section Should be one of: |
||
279 | * 'basics' |
||
280 | * 'experience' |
||
281 | * 'essential_skills' |
||
282 | * 'asset_skills' |
||
283 | * 'preview' |
||
284 | * |
||
285 | * @return string $status 'complete', 'incomplete' or 'error' |
||
286 | */ |
||
287 | public function getSectionStatus(string $section) |
||
288 | { |
||
289 | // TODO: determine whether sections are complete or invalid |
||
290 | $jobPoster = $this->job_poster; |
||
291 | $validator = $jobPoster->isInStrategicResponseDepartment() |
||
292 | ? new StrategicResponseApplicationValidator() |
||
293 | : new ApplicationValidator(); |
||
294 | $status = 'incomplete'; |
||
295 | switch ($section) { |
||
296 | case 'basics': |
||
297 | if ($validator->basicsComplete($this)) { |
||
298 | $status = 'complete'; |
||
299 | } |
||
300 | break; |
||
301 | case 'experience': |
||
302 | if ($validator->experienceComplete($this)) { |
||
303 | $status = 'complete'; |
||
304 | } |
||
305 | break; |
||
306 | case 'essential_skills': |
||
307 | if ($validator->essentialSkillsComplete($this)) { |
||
308 | $status = 'complete'; |
||
309 | } |
||
310 | break; |
||
311 | case 'asset_skills': |
||
312 | if ($validator->assetSkillsComplete($this)) { |
||
313 | $status = 'complete'; |
||
314 | } |
||
315 | break; |
||
316 | case 'preview': |
||
317 | if ($validator->basicsComplete($this) && |
||
318 | $validator->experienceComplete($this) && |
||
319 | $validator->essentialSkillsComplete($this) && |
||
320 | $validator->assetSkillsComplete($this) |
||
321 | ) { |
||
322 | $status = 'complete'; |
||
323 | } |
||
324 | break; |
||
325 | case 'confirm': |
||
326 | if ($validator->affirmationComplete($this)) { |
||
327 | $status = 'complete'; |
||
328 | } |
||
329 | break; |
||
330 | default: |
||
331 | $status = 'error'; |
||
332 | break; |
||
333 | } |
||
334 | return $status; |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * Check if the status of the application is 'draft' |
||
339 | * |
||
340 | * @return boolean |
||
341 | */ |
||
342 | public function isDraft(): bool |
||
343 | { |
||
344 | return $this->application_status->name === 'draft'; |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Returns true if this meets all the HARD SKILL essential criteria. |
||
349 | * That means it has attached an SkillDeclaration for each essential criterion, |
||
350 | * with a level at least as high as the required level. |
||
351 | * NOTE: If this application is in draft status, it will use |
||
352 | * SkillDeclarations from the the applicants profile for this check. |
||
353 | * |
||
354 | * @return boolean |
||
355 | */ |
||
356 | public function meetsEssentialCriteria(): bool |
||
357 | { |
||
358 | $essentialCriteria = $this->job_poster->criteria->filter( |
||
359 | function ($value, $key) { |
||
360 | return $value->criteria_type->name == 'essential' |
||
361 | && $value->skill->skill_type->name == 'hard'; |
||
362 | } |
||
363 | ); |
||
364 | $source = $this->isDraft() ? $this->applicant : $this; |
||
365 | foreach ($essentialCriteria as $criterion) { |
||
366 | $skillDeclaration = $source->skill_declarations->where('skill_id', $criterion->skill_id)->first(); |
||
367 | if ($skillDeclaration === null || |
||
368 | $skillDeclaration->skill_level_id < $criterion->skill_level_id |
||
369 | ) { |
||
370 | return false; |
||
371 | } |
||
372 | } |
||
373 | return true; |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * Accessor for meetsEssentialCriteria function, which |
||
378 | * allows this value to be automatically appended to array/json representation. |
||
379 | * |
||
380 | * @return boolean |
||
381 | */ |
||
382 | public function getMeetsEssentialCriteriaAttribute(): bool |
||
383 | { |
||
384 | return $this->meetsEssentialCriteria(); |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Save copies of all relevant profile data to this application. |
||
389 | * |
||
390 | * |
||
391 | * @return void |
||
392 | */ |
||
393 | public function saveProfileSnapshot(): void |
||
470 | } |
||
471 | } |
||
472 | |||
473 | /** |
||
474 | * Save copies of Experiences and its linked skills (ExperienceSkills) to this application. |
||
475 | * |
||
476 | * @return void |
||
477 | */ |
||
478 | public function saveProfileSnapshotTimeline(): void |
||
479 | { |
||
480 | $this->refresh(); |
||
481 | $applicant = $this->applicant->fresh(); |
||
482 | |||
483 | $this->user_name = $applicant->user->full_name; |
||
484 | $this->user_email = $applicant->user->email; |
||
485 | $this->save(); |
||
486 | |||
487 | $deleteExperiences = function ($experiences) { |
||
488 | foreach ($experiences as $experience) { |
||
489 | $experience->delete(); |
||
490 | } |
||
491 | }; |
||
492 | |||
493 | // Delete experiences in previous snapshot. |
||
494 | $deleteExperiences($this->experiences_award); |
||
495 | $deleteExperiences($this->experiences_community); |
||
496 | $deleteExperiences($this->experiences_education); |
||
520 |